阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 6.3下使用Gitosis安装搭建Git Server教程

147次阅读
没有评论

共计 6683 个字符,预计需要花费 17 分钟才能阅读完成。

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.bitsCN.com/article/54969.htm

四. 安装 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 

CentOS 6.3 下使用 Gitosis 安装搭建 Git Server 教程

CentOS 6.3 下使用 Gitosis 安装搭建 Git Server 教程

CentOS 6.3 下使用 Gitosis 安装搭建 Git Server 教程

CentOS 6.3 下使用 Gitosis 安装搭建 Git Server 教程

大功告成 ….

Fedora 通过 Http Proxy 下载 Git http://www.linuxidc.com/Linux/2009-12/23170.htm

在 Ubuntu Server 上安装 Git http://www.linuxidc.com/Linux/2009-06/20421.htm

服务器端 Git 仓库的创建(Ubuntu)http://www.linuxidc.com/Linux/2011-02/32542.htm

Linux 下 Git 简单使用教程(以 Android 为例)http://www.linuxidc.com/Linux/2010-11/29883.htm

Git 权威指南 PDF 高清中文版 http://www.linuxidc.com/Linux/2013-10/91053.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计6683字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中