linux git配置服务器,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

6bed73fb54f284d631be0a8e284e0889.png

3.安装git和openssh:

git@lightserver-GREATWALL-PC:~$ sudo apt-get install git-core openssh-server openssh-client

e79969cbf9aaa48ad887868a88640107.png

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

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

027627e6492d63dcdea2b648379687ba.png

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

8d3ebb8d33fcd48c8b02750d404ff0cc.png

6.更改gitadmin的权限

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

223182e8b3645dda18843e4ba3db2d58.png

更改仓库的访问权限:

sudo chmod 700 /home/git/repo

8d5453b6d7c414492dfe98f23036b9d0.png

7.安装配置gitosis

初始化一下服务器的git用户,这一步其实是为了安装gitosis做准备。在任何一台机器上使用git,第一次必须要初始化一下:初始化用户名和邮箱。

git@lightserver-GREATWALL-PC:~/repo$ sudo git config --global user.name “tengfei”

32b6af31bf804d839152bc3176c08d7b.png

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

8b8fdafab03813dd8b6e83730910d027.png

OK,没有报错,通过设置!

初始化git邮箱

git@lightserver-GREATWALL-PC:~/repo$ sudo git config --global user.email “tengfei@10.255.13.253“

@后是服务器IP。

4e3ed012378c38a5ee157e0de4e89caf.png

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

7057e12332f150f481c316a20ebe4167.png

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

OK,Next!

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

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

ab0e55261674d47c509246de8f30f5cf.png

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

c097f741ccd2e84d4243af122789d36a.png

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

5f68832ab555039316c1952dfef00078.png

Perfect,Next,python 安装gitosis

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

531bc64203066cd1e69280bd2f616201.png

到此为止,gitosis安装完成!

10.切换到gitadmin用户下:

light-server@lightserver-GREATWALL-PC:~sugitadmin密码:

3660d0d372bf335a7e47e02756885104.png

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

5c0f284c2cd60b5be97462d278ae7202.png

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

11.默认状态下,gitosis会将git仓库放在 gitadmin用户的home下,所以我们做一个链接到/home/repo

ln -s /home/git/repo /home/gitadmin/repositories

返回正常用户:

$ exit

light-server@lightserver-GREATWALL-PC:~$

cfa587724b19a9fa1ad4bf84ec9fa513.png

查看软链接状态:

08f1fccfa5b5e4df05b2e6845b6d10fa.png

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

我在我的电脑上,用户为:wuzhang@ubuntu:$

生成ssh公钥。

wuzhang@ubuntu~:$ ssh -keygen -t rsa

4194de83fd96de03afef30ffa1bb9f6b.png

OK,生成公钥成功!

复制到远程主机上,这就考验我们Linux 命令的基础了。还好大一linux学的还不错!

Scp 远程复制命令:scp 本地文件 远端主机:存储路径

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

5a366c9efe75196637f5da9442037b64.png

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

3d9b9ca9e3c85c80a84d5e9d6b3ebb53.png

果然有id_rsa.pub.

在git服务器上,更改权限:

git@lightserver-GREATWALL-PC:/tmp/gitosis$ sudo chmod a+r /tmp/id_rsa.pub

96ba52c14946ea71a4f4b2ecebd46d73.png

13.让gitosis运行起来:

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

b1c2e068c005ca7877d3d3f288626e8b.png

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@lightserver−GREATWALL−PC: cd teamwork.git/

gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$

3d280046b61e42e5283befbf2537c180.png

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$

c4009e36a06ac35278449a088ac5b0a3.png

但是,到目前为止,这只是一个空仓库,空仓库是不能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$

1618bbd274e23fbdad4b78edd637226d.png

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

wuzhang@ubuntu:~/work/gitosis-admincdkeydir/wuzhang@ubuntu:/work/gitosis−admin/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

9989af35d5cea113d60af0ef8ec2a3fd.png

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_projectecho"Testgitserver.">hello.txtwuzhang@ubuntu:/work/teamworkproject 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$

9bb1d3b301fad3de99359c0a9886ea18.png

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

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

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 ..." 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:~sugit密码:git@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 下测试:

d450ecdaee7ada46052056c2fb4be674.png

打开winidows本地文件,ok!

86afb85768f7f4d88289cce682ce4724.png

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

GitHub 教程系列文章:

Git 的详细介绍:请点这里

Git 的下载地址:请点这里

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值