CentOS更新Git新版本及自建Git服务器
查询系统是否有安装git,没有则安装
$ sudo rpm -qa|grep git
git-1.8.3.1-25.el7_9.x86_64
通常系统都会自带,不过版本都比较低,如果要使用git新功能的话,那就必须要更新版本,如果只是为了能够使用的话,那就使用系统自带的即可。
如果要更新版本的话,光靠系统的yum源是不够的,要找到提供git新版本的yum源添加到系统中,才能使用yum安装新版本,这里介绍一个我平时使用的yum源。
yum源地址:ius.io
打开网址,网站中有提供安装方法,打不开的继续往下看,我将方法拷贝下来了。
$ sudo yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
安装好yum源后,查看系统是否有新增yum源。
$ sudo yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirror.nyist.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13,788
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 518
ius/x86_64 IUS for Enterprise Linux 7 - x86_64 159
ius-testing/x86_64 IUS for Enterprise Linux 7 - Testing - x86_64 0
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 5,527
repolist: 30,064
显示列表中有ius就是新增的yum源成功了,使用以下指令查看ius源提供的最新git版本是多少。
$ sudo yum provides git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirror.01link.hk
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
git-1.8.3.1-23.el7_8.x86_64 : Fast Version Control System
Repo : base
git236-2.36.5-1.el7.ius.x86_64 : Fast Version Control System
Repo : ius
Matched from:
Provides : git = 2.36.5-1.el7.ius
从上面的显示列表中可知,ius源提供的版本为git-2.36.5,既然知道最新版本,那就开始安装新版git了。
$ sudo yum remove -y git
$ sudo yum install -y git236
通过以上两个指令就安装新版git了,第一个指令是将系统旧版本卸载,第二个指令就是安装新版本git。
安装git成功后,就要开始安装配置git服务器了。、
第一步,创建一个git用户,用来运行git服务。
$ sudo adduser -m -d /srv/git -s /usr/bin/git-shell
出于安装考虑,作一下温馨提示:
- 添加用户的指令中一定要加上-s参数,指定运行的shell,禁止git用户登录
- 指定git-shell,保证git在ssh中正常使用,但想远程登录就会每次一登录就自动退出
第二步,创建并收集所有需要登录git的用户公钥。
$ sudo ssh-keygen -t rsa -C "demo@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Documents and Settings/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Documents and Settings/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Documents and Settings/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ke+mndmGOa703Eq2CrddvxQ+9zWzkFCtWhsKv752OAo demo@163.com
The key's randomart image is:
+---[RSA 2048]----+
| . o... |
| . o . ... |
| o o o.* |
| ..+ o+o * |
| +o.+.oS . |
| o+ . . |
|E .o. . =o |
| . =..o O+*. |
| .+o+ ++X+o |
+----[SHA256]-----+
关于ssh-keygen的注意事项
输入指令ssh-keygen后要按三次回车键,因为是提示是否有设置密码;如果设置了,之后要使用git都需要输入密码,这里就直接回车键,设置密码为空。
当收集到所有需要登录的用户公钥后,将他们自己的id_rsa.pub文件里的公钥导入到~/git/.ssh/authorized_keys文件里,一行一个公钥。
注意与说明:
- 如果没有authorized_keys文件则创建它
- 创建公钥必须在客户端完成
- 导入公钥到authorized_keys文件必须在服务器端完成
第三步,初始化Git仓库。
先选定一个目录作为Git仓库,假定是/srv/gitrepo/sample.git,在/srv目录下输入命令:
$ sudo cd /srv
$ sudo mkdir gitrepo
$ sudo chown git:git gitrepo/
$ sudo cd gitrepo
$ sudo git init --bare sample.git
Initialized empty Git repository in /srv/gitrepo/sample.git
说明:以上指令Git会创建一个空仓库(或叫裸仓库),空仓库是没有工作区的,因为服务器上的Git仓库纯粹是为了共享,所以是不让用户直接登录到服务器上去改工作区的,并且服务器上的Git仓库通常都以.git结尾。
此后将仓库所属用户和所属组均改为git。
$ sudo chown -R git:git sample.git
第四步,用户在本地克隆远程仓库,在各自的电脑上运行了
$ sudo git clone git@server:/srv/gitrepo/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
至此,git服务器搭建完成,剩下的就是各自在自己的电脑上完成项目进行推送而已了。
关于管理公钥说明
- 如果团队较小,把公钥收集起来放到服务器端的~/git/.ssh/authorized_keys文件里就OK了
- 如果团队有100+以上,可以参考用Gitosis来管理公钥