准备环境
IP | 主机名 |
---|---|
192.168.47.126 | server |
192.168.47.125 | client |
需求
下载源码包编译安装或者yum安装
服务端创建git用户,配置空仓库
创建无秘钥登录证书
客户端命令使用:克隆、修改git配置、 添加修改文件到暂存区、 提交修改到本地仓库、 提交修改到远程服务器。
1.yum安装git
yum -y install git
2.创建git用户
[root@server ~]# useradd -m git
[root@server ~]# passwd git
Changing password for user git.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
3.创建空仓库
[root@server ~]# su - git
[git@server ~]$ mkdir project.git
[git@server ~]$ cd project.git/
[git@server project.git]$ git init --bare
Initialized empty Git repository in /home/git/project.git/
4.仓库文件授权
[git@server project.git]$ chown -R git:git /home/git/project.git/
5.客户端访问方式
[root@server ~]# git clone file:///home/git/project.git/
Cloning into 'project'...
warning: You appear to have cloned an empty repository.
[root@client ~]# git clone git@192.168.47.126:/home/git/project.git
Cloning into 'project'...
The authenticity of host '192.168.47.126 (192.168.47.126)' can't be established.
ECDSA key fingerprint is 85:fd:a6:6f:4a:35:f3:9f:14:d9:1c:97:44:db:a3:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.47.126' (ECDSA) to the list of known hosts.
git@192.168.47.126's password:
warning: You appear to have cloned an empty repository.
6.创建无密钥登陆证书
(服务端、客户端都要执行)
[root@server ~]# su - git
[git@server ~]$ ll
total 0
[git@server ~]$ pwd
/home/git
[git@server ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa):
Created directory '/home/git/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/git/.ssh/id_rsa.
Your public key has been saved in /home/git/.ssh/id_rsa.pub.
The key fingerprint is:
2a:ca:14:e1:28:e5:7b:ca:8b:a4:6c:21:f4:8e:a1:72 git@git1
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| o |
| * . |
|+ = S |
|oo + . |
|o.B o . |
|*BE= . |
|=o*. |
+-----------------+
[git@server ~]$ cd .ssh/
[git@server .ssh]$
[git@server .ssh]$ ll
total 8
-rw-------. 1 git git 1679 Mar 17 12:29 id_rsa
-rw-r--r--. 1 git git 390 Mar 17 12:29 id_rsa.pub
[git@server .ssh]$ cat id_rsa.pub >> authorized_keys
[git@server .ssh]$ chmod 600 authorized_keys
[git@server .ssh]$ chmod 700 -R ~/.ssh/
[git@server .ssh]$ su - root
Password:
Last login: Tue Mar 17 12:25:02 CST 2020 from 192.168.47.1 on pts/0
[root@server ~]# scp /home/git/.ssh/authorized_keys 192.168.47.125:/root/.ssh
The authenticity of host '192.168.47.125 (192.168.47.125)' can't be established.
ECDSA key fingerprint is 85:fd:a6:6f:4a:35:f3:9f:14:d9:1c:97:44:db:a3:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.47.125' (ECDSA) to the list of known hosts.
root@192.168.47.125's password:
authorized_keys 100% 390 0.4KB/s 00:00
[root@client ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
76:d6:a9:a7:51:fd:0e:88:77:86:60:80:f0:89:dd:8b root@client
The key's randomart image is:
+--[ RSA 2048]----+
| . |
| = + |
| . = o |
| . o . o |
| E S = + . |
| . + = o . |
| + = + .|
| = o o |
| . .|
+-----------------+
[root@client ~]# ll
total 4
-rw-------. 1 root root 1172 Feb 28 13:27 anaconda-ks.cfg
7.将client的公钥复制到server的authorized_keys里
[root@git2 ~]# chmod 600 /root/.ssh/authorized_keys
[root@client ~]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDG9pqTzEGwztT5ATMIqnWO2RPtV9cHg8QgyZpIgjCuWZC5FfFK1NNP7rcsu067jVB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aP root@client
[root@client ~]# ssh git@192.168.47.126
The authenticity of host '192.168.47.126 (192.168.47.126)' can't be established.
ECDSA key fingerprint is 85:fd:a6:6f:4a:35:f3:9f:14:d9:1c:97:44:db:a3:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.47.126' (ECDSA) to the list of known hosts.
Last login: Tue Mar 17 12:28:51 2020
8.免密登陆测试
[root@client ~]# ssh git@192.168.47.126
The authenticity of host '192.168.47.126 (192.168.47.126)' can't be established.
ECDSA key fingerprint is 85:fd:a6:6f:4a:35:f3:9f:14:d9:1c:97:44:db:a3:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.47.126' (ECDSA) to the list of known hosts.
Last login: Tue Mar 17 12:28:51 2020
9.禁止shell登录
[root@git1 ~]# vim /etc/passwd
git:x:1000:1000::/home/git:/bin/git-shell
客户端命令工具
clone 将远程服务器仓库克隆到本地
config 修改git配置
add 添加修改文件到暂存区
commit 提交修改到本地仓库
push 提交修改到远程服务器
1.克隆服务端git仓库到客户端上
[root@git2 ~]# mkdir git
[root@git2 ~]# cd git/
[root@git2 git]# git clone git@192.168.47.126:/home/git/project.git
Cloning into 'project'...
Enter passphrase for key '/root/.ssh/id_rsa':
git@192.168.47.126's password:
warning: You appear to have cloned an empty repository.
[root@git2 git]# ll
total 0
drwxr-xr-x. 3 root root 17 Feb 28 14:17 project
2.对数据进行修改
[root@git2 git]# cd project/
[root@git2 project]# echo "NO.1" > 1.html
[root@git2 project]# git add .
[root@git2 project]# ll
total 4
-rw-r--r--. 1 root root 5 Feb 28 14:24 1.html
[root@git2 project]# git commit -m "NO.1"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@git2.(none)')
[root@git2 project]# git config --global user.name "users"
[root@git2 project]# git config --global user.email "2945294692@qq.com"
[root@git2 project]# git commit -m "NO.1"
[master (root-commit) 532eedc] NO.1
1 file changed, 1 insertion(+)
create mode 100644 1.html
[root@git2 project]# git remote add origin git@192.168.47.126:/home/git/project.git
fatal: remote origin already exists.
[root@git2 project]# git push origin git1
git@192.168.47.126's password:
error: src refspec git1 does not match any.
error: failed to push some refs to 'git@192.168.47.126:/home/git/project.git'
3.查看仓库数据状态
[root@client project]# git status
# On branch master
nothing to commit, working directory clean
[root@client project]# vim 1.html
NO.1
Hello
[root@client project]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: 1.html
#
no changes added to commit (use "git add" and/or "git commit -a")