一、安装git
在Linux服务器和Windows客户端上分别安装git。
Linux服务器端:
[root@localhost ~]# yum install -y git
安装完成后查看git版本:
[root@localhost ~]# git --version
git version 1.8.3.1
客户端:
下载 git-for-window,地址:https://git-for-windows.github.io/
安装完成后查看git版本:
Administrator@ygzx-PC03 MINGW64 ~
$ git --version
git version 2.9.3.windows.2
二、Linux服务器和客户端配置
1.创建git用户,并设置用户密码
[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
更改用户 git 的密码 。
新的 密码:
2.Linux服务器端创建git仓库
设置 /home/data/git/gittest.git 为 Git 仓库
然后把 Git 仓库的 owner 修改为 git
[root@localhost home]# mkdir -p data/git/gittest.git
[root@localhost home]# git init --bare data/git/gittest.git
初始化空的 Git 版本库于 /home/data/git/gittest.git/
[root@localhost home]# cd data/git/
[root@localhost git]# chown -R git:git gittest.git/
3.客户端clone远程仓库
进入 Git Bash 命令行客户端,创建项目地址(d:/git)并进入:
Administrator@ygzx-PC03 MINGW64 ~
$ cd d:/git
Administrator@ygzx-PC03 MINGW64 /d/git
然后从 Linux Git 服务器上 clone 项目:
$ git clone [email protected]:/home/data/git/gittest.git
Cloning into 'gittest'...
The authenticity of host '192.168.1.75 (192.168.1.75)' can't be established.
ECDSA key fingerprint is SHA256:nYFUFGu7QOGRxL9+4bSx/8kspjzp60tIeFEaA9ca1UU.
Are you sure you want to continue connecting (yes/no)?
输入yes,然后再输入git的密码:
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.75' (ECDSA) to the list of known hosts.
[email protected]'s password:
Connection to 192.168.1.75 closed by remote host.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Administrator@ygzx-PC03 MINGW64 /d/git
$
三、客户端创建 SSH 公钥和私钥
XXXXXXXXd代表自己的QQ号:
ssh-keygen -t rsa -C "[email protected]"
执行上述命令后得到如下结果:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bfq1lz6O505WezZM1r0ROZJ/O0+7AycCbq9MskNQXFE [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| . .ooE |
| o . .|
| . o + |
| . o o =|
| . S + ==|
| . = . o+o*|
| ..o.. o *O+|
| .=. o ==+*|
| ..oo oB=++|
+----[SHA256]-----+
Administrator@ygzx-PC03 MINGW64 /d/git
$
此时C:\Users\Administrator\.ssh目录下面会多出两个文件,一个是私钥id_rsa,另一个是公钥id_rsa.pub
四、服务器端Git打开RSA认证
进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
保存并重启 sshd 服务:
[root@localhost ssh]# /etc/rc.d/init.d/sshd restart
由 AuthorizedKeysFile 得知公钥的存放路径是 .ssh/authorized_keys,实际上是 $Home/.ssh/authorized_keys,由于管理 Git 服务的用户是 git,所以实际存放公钥的路径是 /home/git/.ssh/authorized_keys
在 /home/git/ 下创建目录 .ssh
[root@localhost /]# cd /home/git
[root@localhost git]# pwd
/home/git
[root@localhost git]# mkdir .ssh
[root@localhost git]# ls -a
. .. .bash_logout .bash_profile .bashrc .kshrc .mozilla .ssh
然后把 .ssh 文件夹的 owner 修改为git
[root@localhost git]# ll -a
总用量 20
drwx------ 4 git git 98 8月 28 16:15 .
drwxr-xr-x. 7 root root 4096 8月 28 15:28 ..
-rw-r--r-- 1 git git 18 11月 20 2015 .bash_logout
-rw-r--r-- 1 git git 193 11月 20 2015 .bash_profile
-rw-r--r-- 1 git git 231 11月 20 2015 .bashrc
-rw-r--r-- 1 git git 172 11月 3 2015 .kshrc
drwxr-xr-x 4 git git 37 12月 9 2016 .mozilla
drwxr-xr-x 2 git git 6 8月 28 16:15 .ssh
[root@localhost git]#
五、将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件
回到 Git Bash 下,导入文件:Administrator@ygzx-PC03 MINGW64 /d/git
$ ssh [email protected] 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
[email protected]'s password:
Administrator@ygzx-PC03 MINGW64 /d/git
$
回到服务器端,查看 .ssh 下是否存在 authorized_keys 文件:
[root@localhost git]# cd .ssh
[root@localhost .ssh]# ll
总用量 4
-rw-rw-r-- 1 git git 398 8月 28 16:20 authorized_keys
修改 .ssh 目录的权限为 700
修改 .ssh/authorized_keys 文件的权限为 600
[root@localhost git]# chmod 700 .ssh
[root@localhost git]# cd .ssh
[root@localhost .ssh]# chmod 600 authorized_keys