Ubuntu12.04 Git 服务器配置图文详解


Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,学过Linux的都知道,Git的优点我就不再多说了,我也是很喜欢Linux的。今天我们一起学习Git服务器在Ubuntu 12.04上的配置,废话不多说,走起! 

1.Git 服务器的搭建过程详细记录,如下:

搭建环境:Ubuntu 12.04 桌面版或服务器版均可 软件要求: git-core, gitosis, openssh-server, openssh-client, Apache2(Gitweb),python编译环境。

2,安装配置git服务器。

  准备工作,为了不改变计算机的环境,我们新增家一个用户:
  User Name:git
  Password:git
  切换用户:su git

  

3.安装git和openssh:
  git@lightserver-GREATWALL-PC:~$ sudo apt-get install git-core openssh-server openssh-client

   

 

4.添加Git用户gitadmin,该用户将作为所有代码仓库和用户权限的管理者:

  git@lightserver-GREATWALL-PC:~$ sudo useradd -m gitadmin

    

5.建立一个git仓库的存储点:

  

 

6.更改gitadmin的权限

  git@lightserver-GREATWALL-PC:~/repo$ sudo chown gitadmin:gitadmin /home/git/repo

   

   更改仓库的访问权限:

  sudo chmod 700 /home/git/repo

  

7.安装配置gitosis

  初始化一下服务器的git用户,这一步其实是为了安装gitosis做准备。在任何一台机器上使用git,第一次必须要初始化一下:初始化用户名和邮箱。
  git@lightserver-GREATWALL-PC:~/repo$ sudo git config --global user.name “tengfei”

  

  

  这是什么原因呢?不要着急,检查下啊!哦。。。。原来是 ‘--global’

  

  

  OK,没有报错,通过设置!
  初始化git邮箱
  git@lightserver-GREATWALL-PC:~/repo$ sudo git config --global user.email “[email protected]“ 

      @后是服务器IP。

  

8.安装一下python的setup tool, 这个也是为了gitosis做准备:

  

  git@lightserver-GREATWALL-PC:~/repo$ sudo apt-get install python-setuptools
  OK,Next!

9.获得gitosis包:切换到/tmp下。

  git@lightserver-GREATWALL-PC:/$ cd /tmp 

   

    git@lightserver-GREATWALL-PC:/tmp$ git clone https://github.com/res0nat0r/gitosis.git

   

    ls一下:多了个gitosis文件夹吧?

    

   Perfect,Next,python 安装gitosis

    git@lightserver-GREATWALL-PC:/tmp/gitosis$ sudo python setup.py install

  

  到此为止,gitosis安装完成!

10.切换到gitadmin用户下:
  light-server@lightserver-GREATWALL-PC:~sugitadmin   

  

  为什么是$ ?多难看。Linux支持bash,shell,sh或许就是这个原因吧?我猜的,试试不就知道了?走起。。   

  

  还真是这个原因,这样看着顺眼!

11.默认状态下,gitosis会将git仓库放在 gitadmin用户的home下,所以我们做一个链接到/home/repo
  ln -s /home/git/repo /home/gitadmin/repositories
  返回正常用户:
  $ exit
  light-server@lightserver-GREATWALL-PC:~$

   

  查看软链接状态:

  

12.如果你将作为git服务器的管理员,那么在你的电 脑上(另一台pc)生成ssh公钥:

   我在我的电脑上,用户为:wuzhang@ubuntu:$
  生成ssh公钥。
  wuzhang@ubuntu~:$ ssh -keygen -t rsa

  

  OK,生成公钥成功!
  复制到远程主机上,这就考验我们Linux 命令的基础了。还好大一linux学的还不错!
  Scp 远程复制命令: scp 本地文件 远端主机:存储路径

  例如:scp .ssh/id_rsa.pub [email protected]:/tmp

  

  OK,100%这个我喜欢, 说明复制成功了,不信我们可以去主机查看:

  

  果然有id_rsa.pub.

  在git服务器上,更改权限:
  git@lightserver-GREATWALL-PC:/tmp/gitosis$ sudo chmod a+r /tmp/id_rsa.pub

  

13.让gitosis运行起来:

  执行命令:sudo -H -u git gitosis -init < /tmp/id_rsa.pub

  

  Initialized empty Git repository in /home/repo/gitosis-admin.git/

   Reinitialized existing Git repository in /home/repo/gitosis-admin.git/
   说明实例化空的git仓库/home/git/repositories/gitosis-admin.git/已成功了。

14.gitosis的有趣之处在于,它通过一个git仓库来管理配置文件,仓库就放在了/home/git/repositories/gitosis-admin.git。

我们需要为一个文件加上可执行权限:

 获取root权限

  light-server@lightserver-GREATWALL-PC:/home$ sudo -i
  目录切换:
  root@lightserver-GREATWALL-PC:/home/git# cd repositories/
  root@lightserver-GREATWALL-PC:/home/git/repositories# ls
  gitosis-admin.git
  root@lightserver-GREATWALL-PC:/home/git/repositories# cd gitosis-admin.git
  root@lightserver-GREATWALL-PC:/home/git/repositories/gitosis-admin.git# ls
  config gitosis.conf gitosis-export HEAD hooks index objects refs

  

更改权限(关键步骤)

root@lightserver-GREATWALL-PC:/home/git/repositories/gitosis-admin.git# sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
root@lightserver-GREATWALL-PC:/home/git/repositories/gitosis-admin.git# exit
准备工作到此结束了,下面可以进行测试了!

15.在服务器上新建一个测试项目仓库

我建了一个叫“teamwork”的仓库。切换到gitadmin用户:
gitadmin@lightserver-GREATWALL-PC:~mkdirteamwork.gitgitadmin@lightserverGREATWALLPC:   cd teamwork.git/
gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$

git的初始化:
gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$ git init --bare
Initialized empty Git repository in /home/gitadmin/teamwork.git/
gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$

 

 

但是,到目前为止,这只是一个空仓库,空仓库是不能clone下来的。为了能做clone,我们必须先让某个有权限的人放一个初始化的版本到仓库中。
所以,我们必须先修改一下/home/git/repositories /gitosis-admin.

16.管理gitosis的配置文件

刚刚提到,gitosis本身的配置也是通过git来实现的。在你自己的开发机里,把gitosis-admin.git这个仓库clone下来,就可以以管理员的身份修改配置。

好的,我们回到另一台PC上进行对服务端的测试! 我自己的PC,名字叫:wuzhang@ubuntu

wuzhang@ubuntu:~/work$ sudo git clone [email protected]:/home/git/repositories/gitosis-admin.git
Cloning into 'gitosis-admin'...
[email protected]'s password:
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 5 (delta 0)
Receiving objects: 100% (5/5), done.
wuzhang@ubuntu:~/work$

PS:目前主机必须是root才可以克隆.

wuzhang@ubuntu:~/work/gitosis-admincdkeydir/wuzhang@ubuntu: /work/gitosisadmin/keydir  ls
[email protected]
wuzhang@ubuntu:~/work/gitosis-admin/keydir$
wuzhang@ubuntu上用户权限配置使用vi编辑器打开:
[gitosis]

[group gitosis-admin]
members = wuzhang@ubuntu
writable = gitosis-admin
members = [email protected] [email protected]

[group hello]
writable = teamwork
members = lijiangkun@server b [email protected]

[group hello_ro]
readonly = teamwork
members = lz

 

 

 wq!  保存退出.

这个配置文件表达了如下含义:gitosis-admin组成员有wuzhang@ubuntu, [email protected], [email protected]
该组对gitosis-admin仓库有读写权限;
team组有lijinagkun@server,b两个成员.该组对teamwork仓库有读写权限;
team_ro组有lz一个成员,对teamwork仓库有只读权限
wuzhang@ubuntu上创建一hello.txt的测试文档:
wuzhang@ubuntu:~/work/teamwork_projectecho"Testgitserver.">hello.txtwuzhang@ubuntu: /work/teamwork p roject  ls -al
总用量 16
drwxrwxr-x 3 wuzhang wuzhang 4096 5月 13 18:08 .
drwxrwxr-x 4 wuzhang wuzhang 4096 5月 13 18:07 ..
drwxrwxr-x 7 wuzhang wuzhang 4096 5月 13 18:07 .git
-rw-rw-r-- 1 wuzhang wuzhang 18 5月 13 18:09 hello.txt
wuzhang@ubuntu:~/work/teamwork_project$

接下来要提交到远端服务器了。

root@ubuntu:/home/wuzhang/work/gitosis-admin# git add .
root@ubuntu:/home/wuzhang/work/gitosis-admin# git commit -m "add teamwork and user for git server"
[master 51a4055] add teamwork and user for git server
Committer: root <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config --global user.name "Your Name"
git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

2 files changed, 9 insertions(+)
create mode 100644 keydir/b.pub

wuzhang@ubuntu:~/work/teamwork_project$ git commit -m "initial version"
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# hello.txt
nothing added to commit but untracked files present (use "git add" to track)
wuzhang@ubuntu:~/work/teamwork_project$
添加远端服务器的地址:
root@ubuntu:/home/wuzhang/work/teamwork_project# git remote add origin [email protected]:/home/gitadmin/teamwork.git
root@ubuntu:/home/wuzhang/work/teamwork_project# git remote -v
origin [email protected]:/home/gitadmin/teamwork.git (fetch)
origin [email protected]:/home/gitadmin/teamwork.git (push)
root@ubuntu:/home/wuzhang/work/teamwork_project#
提交文件到服务器:
lroot@ubuntu:/home/wuzhang/work/gitosis-admin# git remote -v
origin [email protected]:teamwork.git (fetch)
origin [email protected]:teamwork.git (push)
root@ubuntu:/home/wuzhang/work/gitosis-admin# git push -f origin master
[email protected]'s password:
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 1.12 KiB, done.
Total 9 (delta 1), reused 4 (delta 0)
To [email protected]:teamwork.git
* [new branch] master -> master
root@ubuntu:/home/wuzhang/work/gitosis-admin#

换个用户,测试下是否可以从服务器克隆刚提交的文件?
切换到另一账户进行git clone测试:
wuzhang@ubuntu:~sugitgit@ubuntu:/home/wuzhang 

查看服务器的地址:
git@ubuntu:/home/wuzhang/work/teamwork_project$ git remote -v
origin [email protected]:teamwork.git (fetch)
origin [email protected]:teamwork.git (push)

 

创建个文件夹,用于存放克隆从服务器获取的文件:

git@ubuntu:/home/wuzhang/work/teamwork_projectcd/gitClone/git@ubuntu:/gitClone  git clone [email protected]:teamwork.git
fatal: could not create work tree dir 'teamwork'.: ????
git@ubuntu:/gitClone$ git clone [email protected]:teamwork.git

出错了,不要怕,应该是权限的问题,我们再试下!

PS:因为git@ubuntu是普通用户,克隆的文件在本地需要创建文件夹,所以权限不够,需要sudo
git@ubuntu:/gitClone$ sudo git clone [email protected]:/home/wuzhang/work/teamwork
Cloning into 'teamwork'...
[email protected]'s password:
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 10 (delta 1), reused 10 (delta 1)
Receiving objects: 100% (10/10), done.
Resolving deltas: 100% (1/1), done.
git@ubuntu:/gitClonelsteamworkgit@ubuntu:/gitClone  cd teamwork/

显示一下克隆的文件:
git@ubuntu:/gitClone/teamwork$ ls
1 1.c 1.c~ hello hello.txt

执行下我编译过得1.c文件

git@ubuntu:/gitClone/teamwork$ ./1
Git Server is OK!
git@ubuntu:/gitClone/teamwork$
克隆完成,服务器环境搭建完成!

Windows 下测试:

 

打开winidows本地文件,ok!

 

 

接下来要搭建一个可以方便访问的Web环境了,下次见! 

GitHub 教程系列文章: 

GitHub 使用教程图文详解   

Git 标签管理详解  

Git 分支管理详解  

Git 远程仓库详解  

Git 本地仓库(Repository)详解  

Git 服务器搭建与客户端安装   

Git 概述  

分享实用的GitHub 使用教程  

Git 的详细介绍:请点这里
Git 的下载地址:请点这里

相关内容