linux安装git服务器

以下是CentOS 6.5下Git服务器搭建的两篇说明文章

http://www.centoscn.com/CentosServer/ftp/2014/0414/2789.html

http://www.111cn.net/sys/CentOS/55849.htm

 

一、GIT的安装

安装过程:

$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
$ wget http://git-core.googlecode.com/files/git-1.9.0.tar.gz   或者  wget https://www.kernel.org/pub/software/scm/git/git-1.9.0.tar.gz tar
$ tar zxvf git-1.9.0.tar.gz
$ cd git-1.9.0
$ make prefix=/usr/local/git all
$ make prefix=/usr/local/git install #root用户运行

 如果编译时提示错误:
LINK git-credential-store
libgit.a(utf8.o): In function `reencode_string_iconv’:
/opt/git-master/utf8.c:530: undefined reference to `libiconv’
libgit.a(utf8.o): In function `reencode_string_len’:
/opt/git-master/utf8.c:569: undefined reference to `libiconv_open’
/opt/git-master/utf8.c:588: undefined reference to `libiconv_close’
/opt/git-master/utf8.c:582: undefined reference to `libiconv_open’
collect2: ld 返回 1
make: *** [git-credential-store] 错误 1

$ cd /usr/local/src/
$ wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
$ tar -zxvf libiconv-1.14.tar.gz
$ cd libiconv-1.14
$ ./configure --prefix=/usr/local/libiconv && make && make install

重新安装GIT

$ cd /usr/local/src/git-1.9.0
$ ./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv
$ make
$ make install

安装完成,查看GIT的版本

$ /usr/local/git/bin/git --version
git version 1.9.0

把GIT命令添加到全局变量中

$ vi /etc/profile
在最后添加 export PATH=$PATH:/usr/local/git/bin
$ source /etc/profile  #修改使其生效


查看GIT版本

$ git --version
git version 1.9.0

OK,GIT软件安装成功。 

 

 

二、安装Gitolite

[root@mysql01 ~]# useradd gitadmin
[root@mysql01 ~]# passwd gitadmin
Changing password for user gitadmin.
New UNIX password:
BAD PASSWORD: it does not contain enough DIFFERENT characters
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@mysql01 ~]#

切换用户生成密钥

[root@mysql01 ~]# su gitadmin
[gitadmin@mysql01 root]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/gitadmin/.ssh/id_rsa):
Created directory ‘/home/gitadmin/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/gitadmin/.ssh/id_rsa.
Your public key has been saved in /home/gitadmin/.ssh/id_rsa.pub.
The key fingerprint is:
e7:5a:ee:1f:31:eb:68:8a:c1:e2:3b:73:6c:3f:6a:3a gitadmin@mysql01
[gitadmin@mysql01 root]$

复制密钥:

[gitadmin@mysql01 ~]$ ls -al
总计 56
drwx—— 3 gitadmin gitadmin 4096 05-17 10:40 .
drwxr-xr-x 8 root     root     4096 05-17 10:36 ..
-rw——- 1 gitadmin gitadmin   60 05-17 10:41 .bash_history
-rw-r–r– 1 gitadmin gitadmin   33 05-17 10:36 .bash_logout
-rw-r–r– 1 gitadmin gitadmin  176 05-17 10:36 .bash_profile
-rw-r–r– 1 gitadmin gitadmin  124 05-17 10:36 .bashrc
drwx—— 2 gitadmin gitadmin 4096 05-17 10:37 .ssh
[gitadmin@mysql01 ~]$ pwd
/home/gitadmin
[gitadmin@mysql01 ~]$ cp .ssh/id_rsa.pub gitadmin.pub
[gitadmin@mysql01 ~]$

[gitadmin@mysql01 ~]$ ls -al
总计 64
drwx—— 3 gitadmin gitadmin 4096 05-17 10:43 .
drwxr-xr-x 8 root     root     4096 05-17 10:36 ..
-rw——- 1 gitadmin gitadmin   60 05-17 10:41 .bash_history
-rw-r–r– 1 gitadmin gitadmin   33 05-17 10:36 .bash_logout
-rw-r–r– 1 gitadmin gitadmin  176 05-17 10:36 .bash_profile
-rw-r–r– 1 gitadmin gitadmin  124 05-17 10:36 .bashrc
-rw-r–r– 1 gitadmin gitadmin  398 05-17 10:43 gitadmin.php
drwx—— 2 gitadmin gitadmin 4096 05-17 10:37 .ssh
[gitadmin@mysql01 ~]$

克隆一个到本地

[gitadmin@mysql01 ~]$  git clone http://github.com/sitaramc/gitolite.git
Cloning into gitolite…
remote: Counting objects: 8086, done.
remote: Compressing objects: 100% (2665/2665), done.
remote: Total 8086 (delta 5559), reused 7690 (delta 5256)
Receiving objects: 100% (8086/8086), 1.84 MiB | 359 KiB/s, done.
Resolving deltas: 100% (5559/5559), done.
[gitadmin@mysql01 ~]$

[gitadmin@mysql01 ~]$ ls
gitadmin.php  gitolite
[gitadmin@mysql01 ~]$ pwd
/home/gitadmin
[gitadmin@mysql01 ~]$

安装:gitolite

[gitadmin@mysql01 gitolite]$ ./install
use the following full path for gitolite:
    /home/gitadmin/gitolite/src/gitolite
[gitadmin@mysql01 gitolite]$ /home/gitadmin/gitolite/src/gitolite

出错:

[gitadmin@mysql01 gitolite]$ /home/gitadmin/gitolite/src/gitolite setup
FATAL: errors found but logfile could not be created
FATAL: /home/gitadmin/.gitolite/logs/gitolite-2013-05.log: 没有那个文件或目录
FATAL: die    ‘-pk’ or ‘-a’ required; see ‘gitolite setup -h’ for more
[gitadmin@mysql01 gitolite]$

解决

[gitadmin@mysql01 src]$ ./gitolite setup -pk /home/gitadmin/gitadmin.pub
Initialized empty Git repository in /home/gitadmin/repositories/gitolite-admin.git/
Initialized empty Git repository in /home/gitadmin/repositories/testing.git/
WARNING: /home/gitadmin/.ssh/authorized_keys missing; creating a new one
[gitadmin@mysql01 src]$ cd /home/gitadmin/.ssh/
[gitadmin@mysql01 .ssh]$ ll
总计 24
-rwx—— 1 gitadmin gitadmin  556 05-17 10:57 authorized_keys
-rw——- 1 gitadmin gitadmin 1675 05-17 10:37 id_rsa
-rw-r–r– 1 gitadmin gitadmin  398 05-17 10:37 id_rsa.pub
[gitadmin@mysql01 .ssh]$

增加用户与版本库

ADDING USERS AND REPOS

[root@mysql01 gitadmin]# git config –global user.name “yourname”
[root@mysql01 gitadmin]# git config –global user.email “youremail@localdomain.com”
[root@mysql01 gitadmin]#

增加版本库出错:

[gitadmin@mysql01 ~]$ git clone gitadmin@172.1.3.222:gitolite-admin
Cloning into gitolite-admin…
The authenticity of host ‘172.1.3.222 (172.1.3.222)’ can’t be established.
RSA key fingerprint is 34:b2:9c:a6:65:14:68:00:b6:af:db:fc:81:63:6c:c1.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.
fatal: The remote end hung up unexpectedly
[gitadmin@mysql01 ~]$

 

———————————————————————————————————————–

客户端生成密钥:

[gitadmin@mysql02 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/gitadmin/.ssh/id_rsa):
Created directory ‘/home/gitadmin/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/gitadmin/.ssh/id_rsa.
Your public key has been saved in /home/gitadmin/.ssh/id_rsa.pub.
The key fingerprint is:
ba:c0:95:d8:c0:83:d9:f5:2b:46:9f:15:50:cd:6c:df gitadmin@mysql02

把密钥复制到服务器

[gitadmin@mysql02 .ssh]$ scp id_rsa.pub 172.1.3.222:/home/gitadmin/
gitadmin@172.1.3.222’s password:
id_rsa.pub        100%  398     0.4KB/s   00:00   
[gitadmin@mysql02 .ssh]$ cd ..
[gitadmin@mysql02 ~]$ pwd
/home/gitadmin

服务器使用gitadmin登录:

[root@mysql01 gitadmin]# su – gitadmin
[gitadmin@mysql02 ~]$ cd gitolite-admin/
[gitadmin@mysql02 gitolite-admin]$ ls
conf  keydir
[gitadmin@mysql02 gitolite-admin]$

 

管理用户和版本库

不应该手动在服务器端加入新的用户或者版本库。
gitolite使用一个特殊的版本库 gitolite-admin 来管理员用户和版本库,只要在这个版本库中修改并 push,服务器就会自动根据配置作出修改。

首先在客户端迁出版本库:

git clone git@host:gitolite-admin

如果在迁出的过程中询问密码,那么说明配置出了问题。一般情况是密钥配置错误。可以检查客户端的 ~/.ssh 下有没有 YourName 私钥。如果需要使用不同的密钥连接多个ssh服务器,可以编辑 ~/.ssh/config 进行配置。

进入 gitolite-admin 目录,其中的 keydir 目录是用来放置用户公钥的,而 conf/gitolite.conf 则是用来配置用户和版本库。

编辑 conf/gitolite.conf如下:

repo foo
    RW+         =   alice
    RW          =   bob
    R           =   carol

上面的配置的含义是:

  • 有一个名为 foo 的版本库;
  • 用户 alice 对它有读、写、删除权限;
  • 用户 bob 对它有读写权限;
  • 用户 carol 对它仅有只读权限。

另外,需要找 alice/bob/carol 用户索要他们的公钥,保存在 keydir 目录中,命名为 alice.pub/bob.pub/carol.pub,然后提交这些改动, push 到服务器。

服务器会自动将公钥加入到 ~/.ssh/authorized_keys 中,并创建 foo 版本库。

foo 版本库的访问地址为 git@host:foo

如果希望把 foo 版本库放在 bar 目录下,可以这样编辑配置文件:

repo bar/foo
    RW+         =   alice
    RW          =   bob
    R           =   carol

此时foo 版本库的访问地址为 git@host:bar/foo

以下是一个配置示例:

@dev1           = lisi git1@DBsave.localdomain
@dev2           = fenestra pear
@admins         = scott
@interns        = ashok
@engineers      = sitaram dilbert wally alice
@staff          = @admins @engineers @interns
repo gitolite
-admin RW+ = gitadmin repo testing RW+ = @all repo zhs RW+ = @dev1 RW = @dev2 R = zhangsan 其中所有用户名的公钥文件应该放在keydir文件夹下面

 

 

 

三、安装 gitosis(该软件已经停止更新,改成使用Gitolite来管理更加的方便快捷)

gitosis为Git用户权限管理系统,通过管理服务端的/home/git/.ssh/authorized_key文件来执行对用户权限的管理,是一个python模块包

1、安装gitosis

$ yum install python python-setuptools
$ git clone git://github.com/res0nat0r/gitosis.git
$ cd gitosis/
$ python setup.py install

显示Finished processing dependencies for gitosis==0.2即表示成功 

 

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

$ adduser git
$ su – git
$ ssh-keygen -t rsa
$ gitosis-init < /home/git/.ssh/id_rsa.pub

#显示以下信息即表示成功
#Initialized empty Git repository in /home/git/repositories/gitosis-admin.git/
#Reinitialized existing Git repository in /home/git/repositories/gitosis-admin.git/

 

3、在服务器上导出管理

$ cd ~
$ git config --global user.name "myname"   
$ git config --global user.email "myname@server" 
$ git clone git@192.168.8.108:gitosis-admin.git

注意,可能出现下面的错误:

Traceback (most recent call last):
File "/usr/local/bin/gitosis-serve", line 8, in <module>
load_entry_point('gitosis==0.2', 'console_scripts', 'gitosis-serve')()
File "/usr/local/lib/python2.7/site-packages/gitosis-0.2-py2.7.egg/gitosis/app.py", line 24, in run
return app.main()
File "/usr/local/lib/python2.7/site-packages/gitosis-0.2-py2.7.egg/gitosis/app.py", line 38, in main
self.handle_args(parser, cfg, options, args)
File "/usr/local/lib/python2.7/site-packages/gitosis-0.2-py2.7.egg/gitosis/serve.py", line 213, in handle_args
os.execvp('git', ['git', 'shell', '-c', newcmd])
File "/usr/local/lib/python2.7/os.py", line 344, in execvp
_execvpe(file, args)
File "/usr/local/lib/python2.7/os.py", line 380, in _execvpe
func(fullname, *argrest)
OSError: [Errno 2] No such file or directory
fatal: The remote end hung up unexpectedly

解决方法:在gitosis服务器上面建个软连接

$ ln -s /usr/local/git/bin/git /usr/local/bin/git

 

4、增加及设置管理项目

在开发机上

$ ssh-keygen -t rsa   #一路回车,不需要设置密码
$ scp /root/.ssh/id_rsa.pub root@192.168.8.108:/home/git/gitosis-admin/keydir/developer01.pub   #上传公钥到服务器(默认SSH端口22)
备注:developer01.pub是在开发机用户为developer01生成的(可查看公钥的内容最后面是否一致)

在GIT服务器上

$ cd gitosis-admin
$ vim gitosis.conf

#在文件尾增加以下内容
[group test] # 组名称
writable = test # 项目名称
members = developer01#密钥用户名 #提交修改
$ git add . 
$ git commit -a -m “add test repo” 
$ git push

以上步骤在服务端上已经完成。

 

在开发机上:

5、在开发机上初始,增加及使用项目test.git

$ cd /repo
$ mkdir test-git
$ cd test-git
$ git inti
$ touch readme
$ git add .
$ git commit -a -m “init test-git”
$ git remote add origin git@192.168.8.108:test.git
$ git push origin master

6、在 test 项目新增用户
①在新增用户的本机输入ssh-keygen -t rsa生成私钥公钥;
②把生成的公钥id_rsa.pub上传或复制到 gitosis-admin/keydir文件夹下,并重名,如:jankerli.pub;
③vim gitosis.conf
代码如下 复制代码
#在文件尾增加以下内容
[group test]
writable = test
members = jankerli (此处增加新用户的名称,名称必须与上面重名的jankerli.pub一致,去掉.pub)
④#提交修改
git add .
git commit -a -m “add test repo”
git push
⑤新用户便可通过 git clone git@xxx:test.git 克隆代码到本机了。

 

posted @ 2014-11-02 21:12  gxldan  阅读(2853)  评论(0编辑  收藏  举报