您的位置:首页 > 其它

怎样构建一个GIT服务器(How to Construct A GIT Server)

2014-08-25 21:25 417 查看
Topic: 怎样构建一个GIT服务器,以及如何GIT成员对GIT仓库的访问

Reference to:
1,progit.zh.pdf
2,http://bdxnote.blog.163.com/blog/static/844423520124137333373/
3,http://blog.csdn.net/wirror800/article/details/5189564

Subtopic: 1,Install Ubuntu 10.04 server
2,Install Git-core
3,Install Git Management tool Gitosis
4,Config Git access authority as Gitosis(Public key and Private key)
5,Config Git access authority as SSH account
6,Test.
最近,刚好有一个项目需要与另外一个公司R协同开发,在立项会上,决定由我司来构建GIT服务器。
刚开始,准备直接在我们公司内部的GIT服务器上Nat外网给R公司下载代码,但出于安全考虑,这个想法还是被否定。最后IT给我一台主机,要我用这台主机做GIT服务器。
我就从装Ubuntu系统开始,构建一个GIT服务器
第一篇:安装Ubuntu-server 10.04
网上有很多安装指导的帖子,而且,安装都非常简单,我就不赘述,按以下流程。
1,光盘启动计算机
2,选择语言
//服务器建议选择成English
3,进入光盘启动界面
//这里必然选择第一项:安装Ubuntu服务器版
4,设置键盘布局
是否需要自动检查键盘布局,我们选择“否”,接下来自己设置
//选择“USA”
//继续选择“USA”
5,硬件扫描
//安装程序检测系统硬件
6,配置网络
//我选择自动配置,后面再给IT同事手动去设
7,磁盘分区
//我选择第一个,好像是什么整个分区使用…
8,安装系统
9、输入用户全名,用户账号,密码,密码确认
10、加密主目录
//我随手选择“是”,根据个人要求看说明
11、配置http代理
没有通过代理服务器上网,这里就留空,并继续
12、软件选择
//选择需要安装的软件,即使没有选择,系统也会默认帮你安装一些基本的必要的软件。我选择LAMP.SSH.SAMBA
13、安装结束,光盘退出
—————————灰常突出的分隔线————————————————
第2篇:安装并配置服务器
1,为系统添加git用户,用于管理git服务器
useradd -s /bin/bash -d /home/git -G root -m -c “Manager for git server” -p git git
其中,-p:指定账户git的密码;
-m:强制创建git账户的home目录;
-d:指定git账号的home目录;
-s:指定git账号的登录shell;
-G:指定git账户所属的用户组;
-c:指定git账号的描述;
这样添加后,git的密码不能用,不知道为什么,后面不得不使用
koffu@swsc:~$ sudo useradd -m git
koffu@swsc:~$ sudo passwd git //为git添加密码
2,Upgrade System package
koffu@swsc:~$ sudo apt-get update
3,Install git-core
koffu@swsc:~$ sudo apt-get install git-core
3,Install gitosis
STEP1:首先切换到git用户登陆 su git
STEP2:设置环境
git config –global user.name “xugangfeng”
git config –global user.email “xugangfeng@skyworth.com”
STEP3:安装python的setup tool,为安装gitosis做准备;
sudo apt-get install python-setuptools
STEP4:获取gitosis安装包;
git@zxk:~$ mkdir ~/.gitosis_setop
git@zxk:~$ cd ~/.gitosis_setop/
git@zxk:~/.gitosis_setop$ git clone git://eagain.net/gitosis.git
如果这个不行:
Cloning into ‘gitosis’…
fatal: unable to connect to eagain.net:
eagain.net[0: 208.78.102.120]: errno=Connection refused
则可从如下位置clone gitosis包:
git clone https://github.com/res0nat0r/gitosis.git Cloning into ‘gitosis’…
remote: Counting objects: 727, done.
remote: Compressing objects: 100% (216/216), done.
remote: Total 727 (delta 510), reused 701 (delta 495)
Receiving objects: 100% (727/727), 111.73 KiB | 44 KiB/s, done.
Resolving deltas: 100% (510/510), done.
STEP5:安装gitosis包;
git@zxk:~/.gitosis_setop$ cd gitosis/
git@zxk:~/.gitosis_setop/gitosis$ sudo python setup.py install
STEP6:生成ssh公钥,并上传到git服务器上,或直接使用git账户在git服务器上生成;现在采用使用git账户直接在git服务器上生成;
git@zxk:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa):
Created directory ‘/home/git/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/git/.ssh/id_rsa.
Your public key has been saved in /home/git/.ssh/id_rsa.pub.
The key fingerprint is:
34:34:76:a3:72:3f:05:46:d3:e1:72:62:68:e6:fa:63 git@zxk
The key’s randomart image is:
+–[ RSA 2048]—-+
| +.B… |
| o * =. |
| . O + + |
| B + = |
| S o |
| . . |
| . |
| .E |
| … |
+—————–+
STEP7:初始化gitosis;
git@zxk:~$ sudo -H -u git gitosis-init < ~/.ssh/id_rsa (我感兴趣的是这句到底是干嘛?)
Initialized empty Git repository in /home/git/repositories/gitosis-admin.git/
Reinitialized existing Git repository in /home/git/repositories/gitosis-admin.git/
gitosid默认会会把git仓库放在git账号的home目录下;
gitosis的有趣之处在于,它通过一个git仓库来管理配置文件,仓库就放在了/home/git/repositories/gitosis-admin.git;我们需要为一个文件加上可执行权限:
git@zxk:~$ chmod 755 repositories/gitosis-admin.git/hooks/post-update
—————————淡淡的分隔线——————————————————————
至此,git的仓库也准备好了,即STEP7生成的仓库(就是ls /home/git/repositories/gitosis-admin.git/),这个仓库是用于管理/home/git/repositories/下面仓库的授权的,那么,我就要问,这个仓库授权是谁给的呢?
无论如何,在这台主机上,git这个用户是是可以Clone下来的,先Clone下来。
STEP8:配置gitosis,以控制git客户端的操作;
git@zxk:~$ git clone git@192.168.0.251:repositories/gitosis-admin.git
git@zxk:~$ cd gitosis-admin/
git@zxk:~/gitosis-admin$ vi gitosis.conf
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = KEY—– xugangfeng koffuxu
[group co-devep]
writable = co-devep
members = xugangfeng koffuxu
新建一个仓库(co-devep)并添加xugangfeng koffuxu的授权。
git add .
git commit -m “Add xugangfeng koffuxu “
这样就开始第一步,只要增加一个人可以clone gitosis-admin,那这个人就可以添加其他人授权,以及其它仓库,这些都不难理解。
那么为什么git用户可以clone gitosis-admin。我认为就是STEP7,git用户把他的ssh的私钥在初始化admin仓库时写到这个仓库,而其它用户(koffuxu)都是公钥(koffuxu.pub)+私钥(git的私钥)的方式访问git仓库。所以只有在这台主机上的git用户才能clone下admin仓库。因为只能这个server才有git用户的私钥。也就是说在其它机器上用git账户通过ssh连接到这台机器也不能访问git目录下的账户。
不信,试试:
我在用一台服务器上,证明是不能Clone的
koffu@ubuntu:~/swsc-test$ git clone git@172.20.191.19:gitosis-admin.git
Initialized empty Git repository in /home/koffu/swsc-test/gitosis-admin/.git/
ERROR:gitosis.serve.main:Repository read access denied
fatal: The remote end hung up unexpectedly
我在19这台服务器上,证明是可以操作的,也就说(19这台服务器有git用户的私钥)
git@swsc:~/gitosis-admin$ git pull
git@172.20.191.19′s password:
Already up-to-date.
git@swsc:~/gitosis-admin$
——————————-又是一要不长不短的分隔线———————————————
接下来,我们尝试新建一个简单的仓库(co-devep),试一下手。
koffu@swsc:~$ mkdir co-devep/
koffu@swsc:~$ cd co-devep/
koffu@swsc:~/co-devep$ vime readme //随便写点啥
koffu@swsc:~/co-devep$ git init
koffu@swsc:~/co-devep$ git remote add origin git@172.20.191.19.co-devep.git //会在.git/config里面写一个orgin字段
koffu@swsc:~/co-devep$ git push origin master
至于,就使用建好了git服务器,并配置成ssh协议,gitosis-admin管理公 私钥的授权方式。
====================很坚实的分隔线================================
这应该是另启第二篇,题目就是:如何能SSH协议的用户密码访问仓库。
有这一篇文章是因为有这样一个需求:R公司说通过公钥添加授权这样的方式很麻烦,每个要访问的人都要生成一个.pub文件,发给我,还要我来授权。这样也不利于随时增加项目成员,而且有些人使用root编译代码的,不会生成.pub文件。要求别搞这么复杂,用密码控制授权就好了。R公司是牛B,说什么我就得配合。可这怎么整呀。
网上也搜不到,我就把progit-zh.pdf第4章反复读了几遍。搞了一天还不成,今天继续,结果这么简单。
思路就是在代码服务器上(172.20.191.19)新建一个用户(sky),再把这个用户添加到git的用户组里。
koffu@swsc:~$ sudo useradd sky (安全起见,不添加家目录)
koffu@swsc:~$ sudo passwd sky
koffu@swsc:~$ sudo gpasswd -a sky git
在(172.20.190.6)上测试
koffu@ubuntu:~/swsc-test$ git clone sky@172.20.191.19:/home/git/repositories/kernel411_rk30.git (要用绝对路径)
Initialized empty Git repository in /home/koffu/swsc-test/kernel411_rk30/.git/
sky@172.20.191.19′s password:
Could not chdir to home directory /home/sky: No such file or directory
remote: Counting objects: 39653, done.
remote: Compressing objects: 100% (35740/35740), done.
^Cceiving objects: 9% (3569/39653), 6.43 MiB | 2.55 MiB/s
OK,DONE!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: