Linux服务器配置之Git服务器搭建

一、配置环境

1、服务器:阿里云CentOS 7.2(64位) + Git (version 1.8.3.1)
2、客户端:Windows 10 Pro(64位) + Git(version 2.11.0.windows.1)
3、服务器连接软件:Xshell 5


二、配置步骤

  • 1、安装git
    Linux作为服务器端系统,Windows作为客户端系统,分别安装Git
    <b>服务器端:</b>
    [admin@ceontOS ~]$ su root                  #切换到root用户名
    Password:                                   #输入root用户的密码
    [root@ceontOS ~]# yum install -y git        #执行该命令进行Git安装

安装完后,查看Git版本

[root@ceontOS ~]# git --version
git version 1.8.3.1

<b>客户端:</b>
下载 Git for Windows,地址:https://git-for-windows.github.io/
安装完之后,可以使用 Git Bash 作为命令行客户端。
安装完之后,查看 Git 版本

$ git --version
git version 2.11.0.windows.1

Git客户端安装具体可参考:Git安装及SSH Key管理之Windows篇

  • 2、服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码

    [root@ceontOS ~]# cd /home                                  #进入/home/目录
    [root@ceontOS home]# id git                                 #查看git用户是否存在
    id: git: no such user                                       #提示git用户不存在
    [root@ceontOS home]# useradd git                            #创建git用户
    [root@ceontOS home]# passwd git                             #为git用户创建密码
    Changing password for user git.
    New password:                                               #设置密码
    BAD PASSWORD: The password is shorter than 8 characters
    Retype new password:                                        #确认密码
    passwd: all authentication tokens updated successfully.
  • 3、服务器端创建 Git 仓库
    设置 /home/git/repository/gittest.git 为 Git 仓库

     

[root@ceontOS home]# mkdir -p ./git/repository/gittest.git #在git用户目录下创建仓库目录repositroy,并且创建gittest.git项目测试目录
[root@ceontOS home]# ls #查看/home/目录下有哪些用户目录
admin git
[root@ceontOS home]# cd git #进入git用户目录
[root@ceontOS git]# ls #查看git用户目录下有哪些目录/文件
repository
[root@ceontOS git]# cd repository/ #进入repository仓库目录
[root@ceontOS repository]# ls #查看仓库目录下的项目目录
gittest.git
[root@ceontOS repository]# git init --bare ./gittest.git #这步很重要,初始化项目测试目录
Initialized empty Git repository in /home/git/repository/gittest.git/

然后把 Git 仓库的 owner 修改为 git

[root@ceontOS git]# ll #查看gittest.git项目文件夹的拥有者
total 4
drwxr-xr-x 3 root root 4096 Jan 13 13:08 repository #拥有者是root用户名
[root@ceontOS git]# chown -R git:git repository #将拥有者改为git用户
[root@ceontOS git]# ll #再次查看gittest.git项目文件夹的拥有者
total 4
drwxr-xr-x 3 git git 4096 Jan 13 13:08 repository #拥有者是git用户

* **4、客户端 clone 远程仓库**
先在本地Windows系统上创建一个存放git项目的文件夹,例如我的设置在:【D:\gittest】
此处通过Git Bash来创建的,当然也可以手动创建该目录,然后进入该目录,启动Git Bash

JayYang@YJ-PC MINGW64 ~/Desktop #在桌面打开的git bash
$ cd /d #进入D盘
JayYang@YJ-PC MINGW64 /d
$ mkdir gittest #创建gittest文件夹
JayYang@YJ-PC MINGW64 /d
$ cd gittest/ #进入gittest文件夹
JayYang@YJ-PC MINGW64 /d/gittest #显示当前在D:\gittest路径下
$ git clone git@服务器公网IP地址:/home/git/repository/gittest.git #IP地址后面跟冒号,冒号后面是刚才初始化的项目文件夹的绝对路径

![](http://upload-images.jianshu.io/upload_images/2267589-7bedba534bdc9f04.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

当第一次连接到目标 Git 服务器时会得到一个提示:

The authenticity of host '118.178.142.77 (118.178.142.77)' can't be established.
ECDSA key fingerprint is SHA256:JwC9NxLIjBGqtLC2NUk8MulSc3XH3mM5AWMcFz0a5/8.
Are you sure you want to continue connecting (yes/no)? yes

选择 yes:

Warning: Permanently added '118.178.142.77' (ECDSA) to the list of known hosts.

此时 C:\Users\用户名\.ssh 下会多出一个文件 known_hosts,以后在这台电脑上再次连接目标 Git 服务器时不会再提示上面的语句。
【说明】如果你的服务器没有配置SSH连接,那么按照正常情况会让你输入git用户的密码,输入正确后就能进行项目克隆了。
![](http://upload-images.jianshu.io/upload_images/2267589-776f44a088bba653.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

如果不采用 SSH 公钥来进行验证,则每次都要输入密码,很麻烦,下面就来配置SSH公钥验证的方式来clone项目

* **5、客户端创建 SSH 公钥和私钥**

ssh-keygen -t rsa -C "695834706@qq.com"

此处我是管理了多个ssh_key,所以这边给公私钥起了个名字:id_rsa_git,生成后需要进行**ssh-add**操作,如何管理多个ssh_key可以参考:[Git安装及SSH Key管理之Windows篇](http://www.jianshu.com/p/a3b4f61d4747)
![](http://upload-images.jianshu.io/upload_images/2267589-f781abf1bff6be8f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

此时 C:\Users\用户名\.ssh 下会多出两个文件 id_rsa 和 id_rsa.pub
id_rsa_git 是私钥
id_rsa_git.pub 是公钥
![](http://upload-images.jianshu.io/upload_images/2267589-a5b6810e27a15c93.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

* **6、服务器端修改配置文件,打开 RSA 认证**
进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:

[root@ceontOS ~]# sudo vi /etc/ssh/sshd_config #root用户下,编辑/etc/ssh/sshd_config文件

按如下设置这三个配置,如果注释掉了,则去掉前面的#号

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

保存并重启 sshd 服务:

sudo service sshd restart #重启sshd服务

由 AuthorizedKeysFile 得知公钥的存放路径是 .ssh/authorized_keys,实际上是 $Home/.ssh/authorized_keys,由于管理 Git 服务的用户是 git,所以实际存放公钥的路径是 /home/git/.ssh/authorized_keys
在 /home/git/ 下创建目录 .ssh

[root@ceontOS ~]# cd /home/git
[root@ceontOS git]# pwd
/home/git
[root@ceontOS git]# mkdir .ssh
[root@ceontOS git]# ls -a
. .. .bash_logout .bash_profile .bashrc repository .ssh

然后把 .ssh 文件夹的 owner 修改为 git

[root@ceontOS git]# chown -R git:git .ssh
[root@ceontOS git]# ll -a | grep .ssh
drwxr-xr-x 2 git git 4096 Jan 13 14:54 .ssh

* **7、将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件**
回到客户端Git Bash 下,导入文件:

$ ssh git@服务器公网IP地址 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa_git.pub

![](http://upload-images.jianshu.io/upload_images/2267589-c0e6f9dc9b76225f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
回到服务器端,查看 .ssh 下是否存在 authorized_keys 文件:

[root@ceontOS ~]# cd /home/git/.ssh/
[root@ceontOS .ssh]# ll
total 4
-rw-rw-r-- 1 git git 398 Jan 13 15:03 authorized_keys
[root@ceontOS .ssh]# cat authorized_keys #查看客户端生成的公钥

**重要:**
**修改 .ssh 目录的权限为 700**
**修改 .ssh/authorized_keys 文件的权限为 600**

[root@ceontOS git]# chmod 700 .ssh
[root@ceontOS git]# cd .ssh/
[root@ceontOS .ssh]# chmod 600 authorized_keys

* **8、客户端再次 clone 远程仓库**

git clone git@118.178.142.77:/home/git/repository/gittest.git

![](http://upload-images.jianshu.io/upload_images/2267589-909ce2867f46f28c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

* **9、禁止 git 用户 ssh 登录服务器**
之前在服务器端创建的 git 用户不允许 ssh 登录服务器
编辑 /etc/passwd

[admin@ceontOS ~]$ su root #切换到root用户
Password:
[root@ceontOS admin]# sudo vi /etc/passwd #编辑/etc/passwd文件

找到:

git:x:1001:1001::/home/git:/bin/bash

修改为:

git:x:1001:1001::/home/git:/bin/git-shell

此时 git 用户可以正常通过 ssh 使用 git,但无法通过 ssh 登录系统。

更多Git 教程系列文章: 

Git 的详细介绍:请点这里
Git 的下载地址:请点这里

相关推荐