准备

一台拥有 linux 操作系统的服务器,虚拟机也得

连接其服务器的终端软件,例如 Xshell

步骤

步骤我不分步讲了,网上有教程

sh
1
yum install git -y

-y 代表默认同意后续的安装请求

shell
1
2
useradd git
passwd git
shell
1
2
mkdir -p /home/git/test
git init --bare /home/git/test

-p 代表递归创建文件夹

创建.ssh 文件夹:

shell
1
mkdir -p /home/git/.ssh

创建 authorized_keys 文件:

shell
1
touch /home/git/.ssh/authorized_keys

本地执行 ssh-keygen 生成一对密钥对(id_rsa.pubid_rsa):

powershell
1
ssh-keygen

id_rsa.pub 的内容复制到 /home/git/.ssh/authorized_keys 文件中

更改所有者:

shell
1
chown -R /home/git

-R 代表包含子目录授权

shell
1
vim /etc/ssh/sshd_config

找到以下三个配置,如被注释,取消注释:

plaintext
1
2
3
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

重启 sshd 服务

shell
1
systemctl restart sshd
powershell
1
git clone git@your_ip:/home/git/test

遇到的问题

please make sure you have the correct access rights and the repository exists.

please make sure you have the correct access rights and the repository exists.

有两种情况:

第一种情况是没将密钥放到指定位置或者没放。一般我们使用的用户为 git,所以要将 id_rsa.pub 的内容复制到 /home/git/.ssh/authorized_keys 文件中,并且要更改所有者为 git

第二种情况是 sshd_config 配置没有设置密钥登录。

需要找到以下三个配置,如被注释,取消注释:

plaintext
1
2
3
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys