欢迎访问www.showerlee.com, 您的支持就是我前进的动力.

CentOS6.3下Gitosis安装部署

showerlee 2014-08-11 19:04 Git 阅读 (11,519) 抢沙发

  Git作为一个分布式的版本控制系统使用git的时候,一般和服务器通讯使用的是ssh协议,用ssh的主要优点是速度快(传输前数据会先压缩,比HTTP快),安全,方便读写。 

  客户端通过ssh访问服务器端的验证方式一般有两种,一种是用户名密码的方式,一种是使用公私钥认证的方式. 使用公私钥的方式比较方便,无需每次登录输入密码。 

  某个受信任的客户端的公钥会被设置在服务器端的 ~/.ssh/authorized_keys文件中,有关此文件的格式可以参见 sshd的用户手册 man sshd . authorized_keys有个比较厉害的功能是 支持 command参数,使得每次用户使用此公钥进行验证的时候执行此后面的命令.这样就可以做一些逻辑处理了. 

  一般git库的管理需要权限控制,如何方便简单的进行库的权限管理呢? authorized_keys是一个思路,指定特定command参数,每次验证好用户后首先执行相关逻辑,检测当前用户是否具有某个权限。 所以便有了gitosis,与其说gitosis是一个git权限管理系统,还不如说它是一个authorized_keys文件管理器.


解决方案:


一.环境部署

操作系统:              centos6.3 x64

Git:                         git-1.7.1

Gitosis:                   Gitosis

Gitweb:                   1.7.1-3         

OpenSSH Server:     openssh-server-5.3p1

apache:                  httpd-2.4.4

python-setuptools:   python-setuptools-0.6.10-3

         

Git server(centos6.3 x64): node2.example.com

Git client(centos6.3 x64): node1.example.com

server端配置:

一.关闭iptables和SELINUX

# service iptables stop

# setenforce 0

# vi /etc/sysconfig/selinux

---------------

SELINUX=disabled

---------------

二.同步时间

# ntpdate cn.pool.ntp.org

三.安装apache

传送门:http://www.showerlee.com/archives/6

四.安装OpenSSH

1.yum安装OpenSSH:

# yum install openssh-server -y

2.修改ssh服务端配置:

# vi /etc/ssh/sshd_config

——————————————————————————————

   Port 22 # 修改成你想要的登陆端口

   PermitRootLogin no # 禁止root用户登陆

   StrictModes yes # 检查密钥的用户和权限是否正确,默认打开的

   RSAAuthentication yes # 启用 RSA 认证

   PubkeyAuthentication yes # 启用公钥认证

   PasswordAuthentication yes # 启用密码认证,默认是打开的

   ServerKeyBits 1024 # 修改后变为此状态,将ServerKey强度改为1024比特

  PermitEmptyPasswords no # 修改后变为此状态,禁止空密码进行登录

——————————————————————————————

3.重启服务:

# /etc/init.d/sshd restart


五.安装Git 

# yum install git-core -y

 

六.安装Gitosis

1.安装Gitosis依赖python-setuptools包

# yum install python-setuptools -y

2.安装Gitosis

# cd ~

# mkdir src

# cd src 

# git clone https://github.com/tv42/gitosis.git

# cd gitosis

# python setup.py install

 

3.为gitosis创建系统用户 

# useradd -m git

# passwd git

 

4. 运行gitosis

(1).将管理员生成的公钥上传或拷贝到服务器上。这里的公钥需要在git服务器管理员下使用ssh-keygen命令来创建

# su - git

保证web页面有权限显示该仓库内容

# chmod -R 755 /home/git

# ssh-keygen -t rsa

# cp ~/.ssh/id_rsa.pub /tmp

(2).初始化gitosis

进入到拷贝过来的id_rsa.pub所在目录

# cd /tmp

# gitosis-init < id_rsa.pub

此时,会在/home/git目录下生成gitosis仓库和配置目录

# cd /home/git

# ll

----------------------------------------------------------------

drwxr-xr-x 2 git git 4096 Aug 12 13:39 gitosis

drwxr-xr-x 4 git git 4096 Aug 12 13:39 repositories

---------------------------------------------------------------

(3).切换回当前(root)用户

# exit

(4).配置权限

如果想要别人能够clone gitosis-admin.git,需要执行以下操作:

# chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

至此,gitosis的安装工作已完成,其相关配置可以有管理员来操作,然后再提交到服务器上.

(5)现在可以试一下用初始化 Gitosis 的公钥的拥有者身份 SSH 登录服务器,应该会看到类似下面这样:

# su - git

$ ssh git@127.0.0.1

------------------------------------------------

PTY allocation request failed on channel 0

ERROR:gitosis.serve.main:Need SSH_ORIGINAL_COMMAND in environment.

  Connection to gitserver closed.

------------------------------------------------

说明 Gitosis 认出了该用户的身份,但由于没有运行任何 Git 命令,所以它切断了连接。那么,现在运行一个实际的 Git 命令 — 克隆 Gitosis 的控制仓库:

在你本地计算机上克隆git仓库

# cd /tmp

# git clone git@gitserver:gitosis-admin.git

这会得到一个名为 gitosis-admin 的工作目录,主要由两部分组成:

红色为git仓库配置,蓝色为实际仓库保存的文件

# cd gitosis-admin

# ll -a

----------------------------------------------------------

total 20

drwxr-xr-x 4 git git 4096 Aug 12 13:21 .

drwxr-xr-x 4 git git 4096 Aug 12 13:23 ..

drwxr-xr-x 8 git git 4096 Aug 12 13:22 .git

-rwxr-xr-x 1 git git  157 Aug 12 13:21 gitosis.conf

drwxr-xr-x 2 git git 4096 Aug 12 13:20 keydir

-----------------------------------------------------------

以上操作相当于,系统git用户初始化并成为gitosis管理员,且利用其管理员权限将gitosis-admin仓库clone到本地.


5.添加本地用户john和仓库test到gitosis,并和管理员git合作管理gitosis

1. 用户john添加并发送id_rsa.pub给git

# su -

# useradd john & passwd john

# su - john

# ssh-keygen -t rsa

-----------------------------------------------------------

Generating public/private rsa key pair.

Enter file in which to save the key (/home/john/.ssh/id_rsa): 

Created directory '/home/john/.ssh'.

Enter passphrase (empty for no passphrase): 

Enter same passphrase again: 

Your identification has been saved in /home/john/.ssh/id_rsa.

Your public key has been saved in /home/john/.ssh/id_rsa.pub.

-----------------------------------------------------------

# cp /home/john/.ssh/id_rsa.pub /tmp

2. gitosis管理员git分配john权限

# su - git

# mkdir projects

# cd ~/projects

# git clone git@node2.example.com:gitosis-admin

# cd gitosis-admin

# cat gitosis.conf

------------------------------------------------

[gitosis]

[group gitosis-admin]

writable = gitosis-admin

members = git@node2.example.com

------------------------------------------------

# ls keydir/

-------------------------

git@node2.example.com.pub

-------------------------

# cp /tmp/id_rsa.pub keydir/john.pub

# vi gitosis.conf

————————————————————————————————————

[gitosis]

[group gitosis-admin]

writable = gitosis-admin

members = git@node2.example.com

[group test]

writable = test

members = git@node2.example.com john

————————————————————————————————————

# git add .

# git commit -am "add member john and project foo"

# git push

3. 用户git添加项目test

# su - git

# cd ~/projects

# mkdir test

# cd test

# git init

# echo "Hello World." > hello.txt

# git add hello.txt

# git commit -am 'first commit'

# git remote add origin git@node2.example.com:test.git

# git push origin master

4. 用户 john clone test并修改hello.txt

# su - john

# git clone git@node2.example.com:test.git

# cd test

# date >> hello.txt

# git commit -am 'add time to hello.txt' && git push

整个过程分为:

1.通过修改gitosis-admin管理gitosis用户权限,需要clone到本地,然后修改配置文件,最后add push将结果推送到远程实现权限修改.

2.添加系统用户,生成该用户公钥,并将其复制到keydir下,实现该用户有权限进行git等相关操作.

3.登陆该用户账户进行git相关操作,修改完后commit,push到中服务器即可完成仓库权限配置.

七.安装gitweb

1.首先我们需要Git的源码,其中带有GitWeb,并能生成定制的CGI脚本:

# git clone git://git.kernel.org/pub/scm/git/git.git

# cd git/

# make GITWEB_PROJECTROOT="/home/git/repositories" prefix=/usr gitweb       

# cp -rf gitweb /usr/local/apache2/htdocs/

注: 通过指定 GITWEB_PROJECTROOT 变量告诉编译命令 Git 仓库的位置

2.设置Apache以CGI方式运行该脚本,并添加一个VirtualHost配置:

(1).加载apache的vhost配置文件

# vi /usr/local/apache2/conf/httpd.conf 

搜索包含httpd-vhosts的行,并去掉该行注释.

(2).加载cgid模块,使其支持perl语言.

# vi /usr/local/apache2/conf/httpd.conf 

搜索包含mod_cgid.so的行,并去掉该行注释.

(3).配置VirtualHost

# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf 

添加如下配置:

——————————————————————————————————————————

<VirtualHost *:80>

    ServerName git.example.com

    DocumentRoot /usr/local/apache2/htdocs/gitweb

    <Directory /usr/local/apache2/htdocs/gitweb>

        Options +ExecCGI

        AllowOverride All

        order allow,deny

        Allow from all

        AddHandler cgi-script cgi pl

        DirectoryIndex gitweb.cgi

    </Directory>

</VirtualHost>

——————————————————————————————————————————


(4).安装Time/HiRes.pm perl模块

首次打开web页面报Can't locate Time/HiRes.pm in @INC ….错误

解决方法:

# yum install perl-devel perl-CPAN -y

# perl -MCPAN -e shell 

cpan[2]> install Time::HiRes 

cpan[3]> exit 

(5).重启apache服务

# /usr/local/apache2/bin/apachectl restart

(6).修改本机HOST,并打开gitweb页面

http://git.example.com

QQ20140812-1@2x QQ20140812-2@2x QQ20140812-3@2x

QQ20140812-5@2x

大功告成....

正文部分到此结束
版权声明:除非注明,本文由(showerlee)原创,转载请保留文章出处!
本文链接:http://www.showerlee.com/archives/1240

继续浏览:Git

还没有评论,快来抢沙发!

发表评论

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif