参照:https://blog.csdn.net/qq_42176520/article/details/83541015
实验环境:
- centos7.6x64
- git-2.9.5.tar.gz
- TortoiseGit 2.7.0.0、git-window
一、下载git源码
浏览器访问:https://mirrors.edge.kernel.org/pub/software/scm/git/
将下载后的压缩包上传到centos的/soft目录
二、安装依赖
[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel gcc -y
此时输入git version
命令会发现已经存在git了:
那么把这个git移除掉:
[root@localhost ~]# yum remove git -y
三、编译安装git-2.9.5
3.1 解压压缩包
tar -zxf git-2.9.5.tar.gz
3.2 编译并安装
下面两条命令将git编译并安装到/usr/local/git目录
[root@localhost git-2.9.5]# make prefix=/usr/local/git all
[root@localhost git-2.9.5]# make prefix=/usr/local/git install
3.3 修改环境变量
编辑文件/etc/profile,在最后一行添加:
export PATH="/usr/local/git/bin:$PATH"
如下图:
3.3 使修改的环境变量生效
[root@localhost git-2.9.5]# source /etc/profile
3.4 测试git git version
到这里,git服务器的安装就完成了,下面是git使用的用户权限和仓库的配置测试。
四、准备访问git的用户和用户组
4.1 创建用户组:gitg-test、gitg-test2
[root@localhost git-2.9.5]# groupadd gitg-test
[root@localhost git-2.9.5]# groupadd gitg-test2
4.2 创建用户xiaoming、xiaohong、xiaogang
[root@localhost git-2.9.5]# useradd xiaoming
[root@localhost git-2.9.5]# passwd xiaoming
更改用户 xiaoming 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost git-2.9.5]# useradd xiaohong
[root@localhost git-2.9.5]# passwd xiaohong
更改用户 xiaohong 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost git-2.9.5]# useradd xiaogang
[root@localhost git-2.9.5]# passwd xiaogang
更改用户 xiaogang 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
注意:使用的密码均为123qweQWE
4.3 禁止xiaoming、xiaohong、xiaogang的shell登录
修改/etc/passwd文件内容如下:
此时你再用xiaoming、xiaohong、xiaogang远程shell登录是不行的。
4.4 用户和组的分属情况:
[root@localhost git-2.9.5]# gpasswd gitg-test -a xiaoming
正在将用户“xiaoming”加入到“gitg-test”组中
[root@localhost git-2.9.5]# gpasswd gitg-test2 -a xiaohong
正在将用户“xiaohong”加入到“gitg-test2”组中
[root@localhost git-2.9.5]# gpasswd gitg-test -a xiaogang
正在将用户“xiaogang”加入到“gitg-test”组中
[root@localhost git-2.9.5]# gpasswd gitg-test2 -a xiaogang
正在将用户“xiaogang”加入到“gitg-test2”组中
五、创建git仓库目录test.git和test2.git,并分别隶属到组gitg-test、gitg-test2
5.1 使用root创建/repo目录,这个目录是服务器所有仓库的存放位置
[root@localhost git-2.9.5]# mkdir /repo
5.2 初始化test.git和test2.git仓库
[root@localhost git-2.9.5]# cd /repo
[root@localhost repo]# git init --bare test.git
初始化空的 Git 仓库于 /repo/test.git/
[root@localhost repo]# git init --bare test2.git
初始化空的 Git 仓库于 /repo/test2.git/
5.3 分别对仓库赋予隶属关系和权限
[root@localhost repo]# chown -R xiaoming:gitg-test test.git
[root@localhost repo]# chown -R xiaohong:gitg-test2 test2.git
[root@localhost repo]# chmod -R 770 test.git
[root@localhost repo]# chmod -R 770 test2.git
chmod命令是指定拥有者和所属组都具有读、写、执行的权限,其他人没有任何权限
六、客户端克隆仓库
注意:
客户端的git环境(windows):
1. 只使用Git-2.23.0-64-bit.exe 然后在命令行中操作
2. 使用Git-2.23.0-64-bit.exe和TortoiseGit-2.8.0.0-64bit.msi然后可以在tortoise客户端中操作
测试用户对仓库的使用权限:
使用xiaoming用户克隆仓库test.git和test2.git,按提示输入密码:
git clone ssh://[email protected]/repo/test.git
git clone ssh://[email protected]/repo/test2.git
效果如下图:
可以看到用户xiaoming可以对仓库test.git进行操作,却无法访问test2.git
所以说:
xiaoming只能访问仓库test.git
xiaohong只能访问仓库test2.git
xiaogang可以同时访问仓库test.git和test2.git
总结:
git仓库中的权限控制依赖的是linux系统自带的权限机制,如果想新建仓库和控制仓库访问权限的话就使用linux自带的用户和组以及对目录的赋权就可以了。