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

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

142次阅读
没有评论

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

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

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

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

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

 

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

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

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

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

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

 

6. 更改 gitadmin 的权限

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

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

  更改仓库的访问权限:

sudo chmod 700 /home/git/repo

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

7. 安装配置 gitosis

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

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

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

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

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

      @后是服务器 IP。

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

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

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

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

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

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

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

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

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

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

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

Perfect,Next,python 安装 gitosis

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

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

到此为止,gitosis 安装完成!

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

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

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

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

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

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

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

查看软链接状态:

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

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

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

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

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

例如:scp .ssh/id_rsa.pub light-server@10.255.13.253:/tmp

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

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

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

果然有 id_rsa.pub.

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

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

13. 让 gitosis 运行起来:

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

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

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$

Ubuntu12.04 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$

 

 

Ubuntu12.04 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 root@10.255.13.253:/home/git/repositories/gitosis-admin.git
Cloning into ‘gitosis-admin’…
root@10.255.13.253’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$

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

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

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

[group gitosis-admin]
members = wuzhang@ubuntu
writable = gitosis-admin
members = wuzhang@10.255.13.253 light-server@10.255.13.253

[group hello]
writable = teamwork
members = lijiangkun@server b git@10.258.13.100

[group hello_ro]
readonly = teamwork
members = lz

 

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

 wq!  保存退出.

这个配置文件表达了如下含义:gitosis-admin 组成员有 wuzhang@ubuntu, wuzhang@10.255.13.100,light-server@10.255.13.253
该组对 gitosis-admin 仓库有读写权限;
team 组有 lijinagkun@server,b 两个成员. 该组对 teamwork 仓库有读写权限;
team_ro 组有 lz 一个成员,对 teamwork 仓库有只读权限
wuzhang@ubuntu上创建一 hello.txt 的测试文档:
wuzhang@ubuntu:~/work/teamwork_projectechoTestgitserver.>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$

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

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

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 <root@ubuntu.ubuntu-domain>
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 you@example.com

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 gitadmin@10.255.13.253:/home/gitadmin/teamwork.git
root@ubuntu:/home/wuzhang/work/teamwork_project# git remote -v
origin gitadmin@10.255.13.253:/home/gitadmin/teamwork.git (fetch)
origin gitadmin@10.255.13.253:/home/gitadmin/teamwork.git (push)
root@ubuntu:/home/wuzhang/work/teamwork_project#
提交文件到服务器:
lroot@ubuntu:/home/wuzhang/work/gitosis-admin# git remote -v
origin gitadmin@10.255.13.253:teamwork.git (fetch)
origin gitadmin@10.255.13.253:teamwork.git (push)
root@ubuntu:/home/wuzhang/work/gitosis-admin# git push -f origin master
gitadmin@10.255.13.253’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 gitadmin@10.255.13.253: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 gitadmin@10.255.13.253:teamwork.git (fetch)
origin gitadmin@10.255.13.253:teamwork.git (push)

 

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

git@ubuntu:/home/wuzhang/work/teamwork_projectcd/gitClone/git@ubuntu:/gitClone  git clone gitadmin@10.255.13.253:teamwork.git
fatal: could not create work tree dir ‘teamwork’.: ????
git@ubuntu:/gitClone$ git clone gitadmin@10.255.13.253:teamwork.git

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

PS:因为 git@ubuntu 是普通用户,克隆的文件在本地需要创建文件夹,所以权限不够,需要 sudo
git@ubuntu:/gitClone$ sudo git clone gitadmin@10.255.13.253:/home/wuzhang/work/teamwork
Cloning into ‘teamwork’…
gitadmin@10.255.13.253’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 下测试:

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

 

打开 winidows 本地文件,ok!

 

 

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

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

GitHub 教程系列文章

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm 

Git 标签管理详解 http://www.linuxidc.com/Linux/2014-09/106231.htm 

Git 分支管理详解 http://www.linuxidc.com/Linux/2014-09/106232.htm 

Git 远程仓库详解 http://www.linuxidc.com/Linux/2014-09/106233.htm 

Git 本地仓库(Repository)详解 http://www.linuxidc.com/Linux/2014-09/106234.htm 

Git 服务器搭建与客户端安装  http://www.linuxidc.com/Linux/2014-05/101830.htm 

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm 

分享实用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm 

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

更多 Ubuntu 相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-05/117502.htm

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