git服务器搭建之后,用户希望拉取和提交代码的时候不需要输入密码,可以通过将
用户的ssh公钥(~/.ssh.id_rsa.pub)加入到git服务器的 ~/.ssh/authorized_keys 文件中

步骤一,从客户端获得 SSH 公钥

为了使客户端可以向 Git 服务器提供 SSH 公钥,首先要确认客户端拥有公钥。SSH 的密钥存储在 ~/.ssh/ 目录下,下面我们查看一下这里面都有哪些文件:

[user@local ~]$ ls .ssh/
id_rsa  id_rsa_osc  id_rsa_osc.pub  id_rsa.pub  known_hosts
[user@local ~]$

上面的 xxx 和 xxx.pub 分别是一个 SSH 私钥和公钥。

这里 id_rsa(私钥) 和 id_rsa.pub(公钥) 是一对儿,而 id_rsa_osc 和 id_rsa_osc.pub 又是一对儿私钥和公钥。由此可见,一个用户是可以拥有多个密钥的,但是这并不影响我们后面对 Git 服务器的配置,使用任何一个公钥都可以。

如果用户没有密钥文件,甚至连 .ssh 目录都没有,那么说明用户还没有创建 SSH 密钥,我们使用 ssh-keygen 命令可以为其生成密钥。

[user@local ~]$ ssh-keygen -t RSA -C "user@126.com"
Generating public/private RSA key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
e1:ec:5c:cd:89:8f:83:a2:aa:5d:8a:7f:93:12:90:f4 user@126.com
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
| .               |
|...     .        |
|o  E   o . + .   |
| .      S o +    |
|  .    o o o     |
|   .... + o .    |
| o.o+. .   .     |
|oo=+..           |
+-----------------+
[user@local ~]$ 

-t RSA 参数表示使用 RSA 算法。

-C 参数指定用户的电子邮箱地址。

接下来 ssh-keygen 命令会询问用户密钥文件的存储路径以及密码等,如果不设置密码直接键入回车即可。

密钥文件默认保存在 ~/.ssh/id_rsa 和 ~/.ssh/id_rsa.pub,公钥文件内容类似如下:

[user@local ~]$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVzaGljR5OjgA3VUPO/C/0eIBBhcUQ9v2glTmim1iJ2nOmqTg1lBUlgQCwaIw6f9qJk6J+ibypzJifnic90dfsItlPLBaiMd+/KqZmJymsPOsB2+aQhGXwbj3StTkA1S3KCbUFSRYj3M1CwCGBLxLSyG/wKS/wUeVXtkwAHfSfR7jzkcB5ZyZY6ioxHsMvkCA7ORPaw5zE4MJNw0K9o25sCrgC5RyPUIcEvEt4lo7weaifhNwp5Ql21lVHKknyTALXqETeAhkYrSgueH54srszYJ3A4l+JvpFhHWC/0lF+lZaWtD/VKzs9HSvyYKAs+ovGZAfZY+AC7Et+MWLtlsmf user@126.com
[user@local ~]$

至此,从客户端获得密钥的步骤就完成了,接下来需要把公钥发送个 Git 服务器仓库管理员。

步骤二,搭建 Git 服务器(已有 Git 服务器管理用户的可以跳过此步骤)

为了便于管理,需要在系统中建立一个单独的用户来管理所有的 Git 仓库。

[user@local ~]$ sudo adduser git
Adding user `git' ...
Adding new group `git' (1001) ...
Adding new user `git' (1001) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
    Full Name []: git
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n] y
[user@local ~]$

我们给这个用户取名为 git。

所有的远程 Git 仓库都可以在这个用户名下建立,所以以后建立新库或者将新用户的 SSH 公钥添加到服务器时,都使用这个用户操作就可以了。

步骤三,建立远程仓库(已有远程仓库的可以跳过此步骤)

切换到新建的 git 帐号,并建立一个空的远程仓库。

[user@local ~]$ su git
Password:
git@Linux:/home/user$ cd ~
git@Linux:~$ mkdir project.git
git@Linux:~$ cd project.git
git@Linux:~/project.git$ git init --bare
Initialized empty Git repository in /home/git/project.git/
git@Linux:~$

建立远程仓库使用 git init 命令,也可以增加 --bare 参数。

写不写 --bare 参数有什么区别呢?

我们知道,一般从远程 clone 下来的仓库会生成一个独立的目录,在这个目录下有当前分支最新版本的文件,同时还有一个 .git 文件夹,与 .git 同级的文件夹称为我们的“工作目录”,我们的修改都在这个目录中进行。而 .git 就是我们 Git 本地仓库的工作目录,我们 add 和 commit 的东西都会被提交到这个目录中。

对 git init 命令添加 --bare 参数就表示初始化 Git 仓库的时候不要创建本地工作目录,所以相当于 .git 文件夹下的所有内容直接创建到当前目录下,而不是被放到 .git 目录下。

在 Git 服务器上建立好仓库以后,用户就可以克隆这个仓库了。等等。。还没配置用户 SSH 公钥呢,这么就让用户去下载,肯定还是要输入密码才行的。

步骤四,在 Git 服务器上为用户配置 SSH 公钥

还是先在 Git 服务器上使用 authorized_keys 文件来管理所有用户的 SSH 公钥。

git@Linux:~$ mkdir .ssh
git@Linux:~$ touch .ssh/authorized_keys
git@Linux:~$ chmod 600 .ssh/authorized_keys
git@Linux:~$
ssh/authorized_keys git@Linux:~$ cat /tmp/id_rsa_user2.pub >> ~/.ssh/authorized_keys git@Linux:~$ cat /tmp/id_rsa_user3.pub >> ~/.ssh/authorized_keys git@Linux:~$

现在 user1、user2 和 user3 就可以通过 SSH 公钥来操作远程 Git 仓库了,快去试试吧。

11-02 17:05