阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

163次阅读
没有评论

共计 5885 个字符,预计需要花费 15 分钟才能阅读完成。

一、配置环境

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 常用命令整理  http://www.linuxidc.com/Linux/2017-12/149316.htm

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm 

Git 使用图文详细教程  http://www.linuxidc.com/Linux/2016-11/136781.htm

Ubuntu Git 安装与使用 http://www.linuxidc.com/Linux/2016-11/136769.htm

分享实用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm 

Git 从入门到学会 http://www.linuxidc.com/Linux/2016-10/135872.htm

Git 基本操作详解 http://www.linuxidc.com/Linux/2016-10/135691.htm

Git 部署与常用基本命令详解   http://www.linuxidc.com/Linux/2017-06/144961.htm

分布式版本控制系统 Git 详细教程  http://www.linuxidc.com/Linux/2017-05/143747.htm

Git 命令和配置技巧  http://www.linuxidc.com/Linux/2017-11/148423.htm

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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-12/149803.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计5885字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中