温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在CentOS 6.3下使用Gitosis安装搭建Git Server

发布时间:2021-07-29 18:35:47 来源:亿速云 阅读:111 作者:chen 栏目:系统运维

本篇内容介绍了“如何在CentOS 6.3下使用Gitosis安装搭建Git Server”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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

传送门:https://www.yisu.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 命令 &mdash; 克隆 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
&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = git@node2.example.com
[group test]
writable = test
members = git@node2.example.com john
&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;
# 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


添加如下配置:

代码如下:


&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;
<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>
&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;


(4).安装Time/HiRes.pm perl模块
首次打开web页面报Can't locate Time/HiRes.pm in @INC &hellip;.错误
解决方法:

代码如下:


# 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

“如何在CentOS 6.3下使用Gitosis安装搭建Git Server”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI