centos7.0 下安装git(ssh方式)

最近刚弄了个阿里云,在上面弄个git服务器,这里只弄了ssh方式访问,http方式访问的可以看我另外一个随笔http://www.cnblogs.com/hz-cww/p/6077970.html

1、 安装依赖的库

[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel

2、 删除原本的安装的git

[root@localhost ~]# yum remove git -y

3、下载git-2.10.0.tar.gz 到 /usr/local/src

[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget https://www.kernel.org/pub/software/scm/git/git-2.10.0.tar.gz

4、 编译安装

[root@localhost src]# tar -zvxf git-2.10.0.tar.gz
[root@localhost src]# cd git-2.10.0
[root@localhost src]# make prefix=/usr/local/git all
[root@localhost src]# make prefix=/usr/local/git install

5、 增加软连接

[root@localhost src]# ln -s /usr/local/git/bin/* /usr/bin/
[root@localhost src]# git --version

如果能正常显示版本号,即表示成功

6、新建git用户和设置密码

[root@localhost ~]# useradd -m git
[root@localhost ~]# passwd git

7、新建git的仓库,并设置权限,我这边是建立repositories这个文件夹

[root@localhost ~]# mkdir -p /home/git/repositories
[root@localhost ~]# chown -R git:git /home/git/repositories
[root@localhost ~]# chmod 755 /home/git/repositories

8、切换到git用户下,新建仓库

[root@localhost ~]# su git
[git@localhost root]$ mkdir /home/git/repositories/test.git
[git@localhost root]$ cd /home/git/repositories/test.git
[git@localhost test.git]$ git --bare init

ok,到这里简单的git服务器已经建立好了,你可以去客户端机子上clone了

地址:ssh://git@这里写你的IP/home/git/repositories/test.git

9、禁用git用户的shell登陆,修改/etc/passwd文件

[root@localhost ~]# vi /etc/passwd

找到这一行

git:x:1001:1001:,,,:/home/git:/bin/bash
将最后一个冒号后改为:
git:x:1001:1001:,,,:/home/git:/usr/src/git-2.10.0/git-shell
这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。

10、附加:下一次新建git仓库时,shell只能用root登录

[root@localhost root]$ mkdir /home/git/repositories/test2.git
[root@localhost root]$ cd /home/git/repositories/test2.git
[root@localhost test.git]$ git --bare init

这里新建完之后,必须将新建的git仓库更改所有者为git用户,不然无法连接,执行下面命令,

注意这里-R必须加上,不然git用户clone会出现权限不够问题

[root@localhost ~]# chown -R git:git /home/git/repositories

ok,到这里就基本搞定。

 

原文地址:https://www.cnblogs.com/hz-cww/p/6065596.html