Centos7 搭建git服务器,并利用Gitosis进行权限管理

来源:互联网 发布:业务流程优化研究现状 编辑:程序博客网 时间:2024/06/05 20:44

由于公司需要,让我搭个Git服务器,把之前用的SVN上代码迁移到git上去,所以就在阿里云主机上搭了一个,记录了下安装过程,留存文档以备查阅。本篇本章只涉及搭建部分的操作,更多git的使用可以参考文档。

系统环境

主机环境

1centos 7    192.168.1.117

系统版本信息

 

1

2

[root@localhost ~]# cat /etc/redhat-release

CentOS Linux release 7.0.1406 (Core)

安装依赖

1

2

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

Git安装

Centos系统会默认安装一个git-1.7.1,要移除一下。

 

1

2

3

4

[root@localhost ~]# git --version

git version 1.7.1

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

`

下载源码包

 

1

2

3

4

5

6

7

8

9

[root@localhost ~] # wget https://www.kernel.org/pub/software/scm/git/git-2.5.0.tar.gz

[root@localhost ~]# tar -zxf git-2.5.0.tar.gz

[root@localhost ~]# cd git-2.5.0

[root@localhost  git-2.5.0]# ./configure --prefix=/usr/local/git

[root@localhost  git-2.5.0]# make && make install

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

[root@localhost  git-2.5.0]# git --version   #显示版本号,安装成功

git version 2.5.0

[root@localhost git-2.5.0]#

Gitosis配置

Gitosis安装

权限管理工具gitosis的安装,需要用到python-setuptools

 

1

2

3

4

5

6

7

[root@localhost ~]# yum install python python-setuptools

[root@localhost ~]# git clone git://github.com/res0nat0r/gitosis.git

[root@localhost ~]# cd gitosis/

[root@localhost gitosis]# python setup.py install

……

Using /usr/lib/python2.6/site-packages

Finished processing dependencies for gitosis==0.2    #安装成功

公钥管理

管理git服务器需要一些管理者,可以通过上传开发者机器的公钥到服务器,添加成为git服务器的管理者。

以下是把windows机器作为git的管理者,在git bash生成公钥并上传至服务器的过程。

本步骤是在客户机上运行,不是服务器:

1

2

$ssh-keygen -t rsa   #一路回车,不需要设置密码

$scp ~/.ssh/id_rsa.pub root@192.168.1.117:~

 

登录服务器

 

1

2

[root@hadoop-slave ~]# ls id_rsa.pub

id_rsa.pub

Gitosis初始化

服务器上生成git用户,使用git用户并初始化gitosis

创建git版本管理用户 git

 

1

2

[root@localhost ~]# useradd -c "git version manager" -m -d /home/git -s /bin/bash git (本步骤是生成一个专门用于管理GIT的账户)

[root@localhost ~]# passwd git (设置git用户的登录密码为git)

 

初始化gitosis

 

1

2

3

4

5

6

7

[root@localhost ~]# mv id_rsa.pub /home/git/ (把公钥移动到/home/git目录中)

[root@localhost ~]# su git (切换到git账户中进行操作)

[git@localhost root]$ cd

[git@localhost ~]$ gitosis-init < ./id_rsa.pub (将该公匙导入gitosis里,这时客户机机已经有了git的管理权限了 )

Initialized empty Git repository in /home/git/repositories/gitosis-admin.git/

Reinitialized existing Git repository in /home/git/repositories/gitosis-admin.git/

[git@localhost ~]$ chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update  #添加权限

ok了,服务器端配置就ok了,下一步在客户机器上配置

Git项目管理

Clone项目管理仓库

服务端配置完毕,现在转到客户端,进入客户机器(上传公钥的机器),打开git bash

 

1

2

3

$mkdir ~/gitrepo

$cd gitrepo

$git clone git@192.168.1.117:gitosis-admin.git   #克隆项目管理仓库

问题1:

PS:如果clone报错了,密钥的问题和git用户密码的问题,(使用绝对路径/home/git/repositories/gitosis-admin.git可以下载,但是不推荐)

查看gitosis.conf中密钥的members的名称为是否是管理机器的主机名.pub(如果还是存在问题,可能需要修改一下你的公钥的邮箱地址,有的邮箱地址好像不识别,建议设置为:字母@字母.com.pub,不要带数字  例如:git@ceshi.com)

项目权限管理

管理文件clone下来后,可以对项目进行管理。若要先创建一个新项目,要在gitosis-admin.git的配置文件中添加项目,并提交到git服务器告诉服务器我有个新项目。

这是命令操作:

1

2

3

4

5

$cd ~/gitrepo/gitosis-admin

$vim gitosis.conf

[group test]            # 具有写权限的组名称

writable = test         # 该组可写的项目名称

members = liuyan@liuyan-pc #有写权限的组成员

你也可以直接编辑配置文件:
images

 

 

 

 

 

记得不能删除第一项的项目,删除后你就无法用git管理权限了

images

 

 

 

 

 

 

 

 

 

提交到服务器

 

1

2

3

$git add .

$git commit -a -m "add test repo"

$git push

创建新项目

管理文件提交后,本地创建的新项目test就可以提交到远程仓库了。

 

1

2

3

4

5

$cd ~/repo

$mkdir test

$cd test   #对于新的项目,需要先在本地初始化为 Git 项目,添加要管理的文件并作首次提交

$git init

$touch readme

提交到远程服务器

 

1

2

3

4

$git add .

$git commit -a -m "init test"

$git remote add origin git@192.168.1.117:test.git

$git push origin master

git push origin master的意思就是上传本地当前分支代码到master分支。git push是上传本地所有分支代码到远程对应的分支上

服务端查看

 

1

2

[git@localhost repositories]$ ls

gitosis-admin.git  test.git

test仓库已经存在,可以进行操作了。

项目添加协同开发

项目的开发人员一般不止一个,就要添加项目协同开发者。这里需要协同开发者的公钥,上传至git服务器。(这里是在客户端进行操作,如果是需要添加同事的公钥,就把同事的公钥复制过来,并并命名为秘钥中的用户名称即可)

可直接复制

images

 

 

 

 

 

 

 

在members后面加上 用户名

 

images

 

 

 

也可使用命令行进行操作

1

2

3

4

5

6

$cd ~/gitrepo/gitosis-admin/keydir

$ mv ~/id_rsa.pub liuyan@zizhuoy.pub   #修改公钥为`主机名.pub`

$vim gitosis.conf   #添加成员

[group test]

writable = test

members = liuyan@liuyan-pc liuyan@zizhuoy

然后将添加数据后的目录更新到git服务器

$git add keydir/liuyan@zizhuoy.pub
$git commit -am " granted liuyan@zizhuoy commit rights to test "
$git push

注解:gitosis实际上是从服务器端的/home/git/repositories/gitosis-admin/.gitosis.conf文件读取信息的,通过以上操作,会将新的权限信息写入到该文件中,如果搞错了配置,导致失去了推送权限,可以通过修改该文件来重新设定,如果你手工编辑该文件的话,它会一直保持到下次向 gitosis-admin 推送新版本的配置内容为止。

推送完成后,新加的协同开发者就可以进行项目的开发了。


本文转自:http://www.bloggeng.com/archives/285

原创粉丝点击