在linux(CentOS7.4)上搭建git服务器和配置gitolite权限管理 写在前面:网上出现了各种gitosis配置,总觉得不太完善。这里综合各种文档整理出来一个配置。目前centos7.4可用 一、Git服务端配置 1.安装git yum install git -y 安装之后顺便配置一下用户信息 git config --global user.name "yourname" git config --global user.email "your@email.com" 2.创建git用户,并运行git服务 adduser git I.网上有人说git用户的shell为/usr/bin/git-shell为git用户的登陆shell 使用git-shell可以作为简单的使用,即创建一个仓库,让其他人连接上,这是可以的。 但是设置这个是无法使用gitolite,因为这个shell不支持普通的命令   II.修改git密码  passwd git #两次输入你的密码 3.安装gitosis I.gitosis依赖于某些python工具库,所以先安装python工具库 yum install python-setuptools -y II.下载并安装gitosis git clone https://github.com/tv42/gitosis.git cd gitosis III.安装 我的python版本是2.7.5 sudo python setup.py install IV.上传管理员公钥到服务器,例如上传到/tmp/id_rsa.pub 初始化gitosis并添加管理员 sudo -H -u git gitosis-init < /tmp/id_rsa.pub V.权限 需要手工对该仓库中的 post-update 脚本加上可执行权限(我在实际中发现,权限已经是755了,这样的话可以跳过这一步) sudo chmod 755 /home/git/gitosis-admin.git/hooks/post-update VI.将gitosis-admin这个特殊的Git仓库克隆到本地中 git clone git@GITSERVER:gitosis-admin.git 其中 GITSERVER为你的服务器IP地址。不需要输入绝对路径,gitosis会自动帮助我们定位仓库基准地址。 这会得到一个名为 gitosis-admin 的工作目录,目录下包括 : .git/ keydir/ 用户公钥目录 gitosis.conf gitosis配置,用来设置用户、仓库和权限的控制文件 如此就gitosis就安装配置成功了 4.添加新用户 - 将用户的公钥文件 keydir/USER.pub - 添加他们的项目权限到gitosis.conf文件中 用户权限配置 gitosis.conf中分为三个对象:组、仓库、用户 组中的用户可以按照固定的读写规则访问组中的仓库 例如我们要给仓库project1配置两个组,一个可以读写,一个只读 用户test1,test2可以读写,test3只可以读,可以配置如下 [group mobile] writable = project1 members = test1 test2 [group mobile_ro] readonly = project1 members = test3 其中group代表一个组,writable是仓库名,members是此仓库的成员,可以有多个成员,用空格进行分割。 当然keydir要有三个pub文件 test1.pub test2.pub test3.pub 然后提交gitosis-admin项目 5.添加新仓库(*仓库名不要使用_下划线) - 上面是添加用户并分权。现在是介绍添加新项目 在本地(代码仓库服务器)创建一个仓库,并push到git@localhost:project1.git,gitosis会在/home/git/repositories自动创建test.git这个仓库: mkdir project1 cd project1 touch README git init git remote add origin git@localhost:project1.git git add . git commit -am '初始化' git push origin master 此时,添加了权限的用户可以正常克隆代码了,例如:git clone git@192.168.0.181:project1.git 附录:创建公钥和私钥 git bash中 ssh-keygen 一路回车下去