史上最详细的git服务器搭建步骤
- 首先看下我们准备搭建git的服务器的操作系统是什么,然后安装git
       
安装完成后,git --version 查看git版本
2.创建git用户
[root@VM_35_77_centos ~]# sudo useradd -m git
 [root@VM_35_77_centos ~]# sudo passwd git

从root用户切换到git用户,进入到home,然后创建.ssh目录,并在.ssh目录下创建文件authorized_keys
[root@VM_35_77_centos ~]# su git
 [git@VM_35_77_centos root]$ cd
 [git@VM_35_77_centos ~]$ ls -la
 total 28
 drwx------ 4 git  git  4096 May 21 11:35 .
 drwxr-xr-x 4 root root 4096 May 21 11:35 ..
 -rw-r--r-- 1 git  git    18 Dec  2  2011 .bash_logout
 -rw-r--r-- 1 git  git   176 Apr 29  2014 .bash_profile
 -rw-r--r-- 1 git  git   124 Apr 29  2014 .bashrc
 drwxr-xr-x 2 git  git  4096 Nov 12  2010 .gnome2
 drwxr-xr-x 4 git  git  4096 Oct 22  2014 .mozilla
 [git@VM_35_77_centos ~]$ mkdir .ssh
 [git@VM_35_77_centos ~]$ ls -la
 total 32
 drwx------ 5 git  git  4096 May 21 11:38 .
 drwxr-xr-x 4 root root 4096 May 21 11:35 ..
 -rw-r--r-- 1 git  git    18 Dec  2  2011 .bash_logout
 -rw-r--r-- 1 git  git   176 Apr 29  2014 .bash_profile
 -rw-r--r-- 1 git  git   124 Apr 29  2014 .bashrc
 drwxr-xr-x 2 git  git  4096 Nov 12  2010 .gnome2
 drwxr-xr-x 4 git  git  4096 Oct 22  2014 .mozilla
 drwxrwxr-x 2 git  git  4096 May 21 11:38 .ssh
 [git@VM_35_77_centos ~]$ cd .ssh && vi authorized_keys
 [git@VM_35_77_centos .ssh]$ ll
 total 4
 drwxrwxr-x 2 git git 4096 May 21 11:42 authorized_keys

1 。首先用密码登陆centos ,编辑 etc/ssh/sshd_config 
去掉以下几行的注释
RSAAuthentication yes
 PubkeyAuthentication yes
 AuthorizedKeysFile      .ssh/authorized_keys
 AuthorizedKeysCommand none
 AuthorizedKeysCommandRunAs nobody
这个意思是开启公钥认证登陆 。
然后service   sshd   restart ,重启ssh  。
然后添加个用户git ,设置密码 ,我们用用户git测试共钥登陆 。不建议用root测试,以防万一 。
二.客户端需要做的事情
1.将公钥写入服务器验证文件
以root登陆服务器 ,再切换到git用户 。
在git用户的家目录下面建立 .ssh/authorized_keys文件 。目录和文件都要新建 。
然后执行cat id_rsa.pub >>  ~/.ssh/authorized_keys
这个文件就是服务器和客户端验证公钥是否相同的文件 。
5 。公钥和私钥文件权限设置 。这一步很重要 。否则会验证失败 。
SSH对公钥、私钥的权限和所有权的要求是非常严格的,总结如下:
1、下面两个目录的所有权必须是user,所属组也应该是user,权限必须为700
/home/git
/home/git/.ssh

$chmod 700 git
$chmod 700 .ssh


2、下面公钥文件的所有权必须是user,所属组也应该是user,权限必须为644
/home/user/.ssh/authorized_keys
$chmod 644 /home/user/.ssh/authorized_keys



 
 