git服务器搭建(1)

搭建git服务器: 

首先要选择搭建git时的协议,在文档里有三种协议
      1.本地协议:就是多人使用一台电脑,就是使用不同的账号登录同一台电脑,使用共享文件来同时进行操作,很危险。因为你的所有代码版本库如果长存于同一台电脑,更可能发生灾难性的损失。使用命令为:      

1 $ git clone /opt/git/project.git

    注意:因此本地协议常见于团队每一个成员都对一个共享的文件系统(例如一个挂载的 NFS)拥有访问权,通过共享文件夹避免多人使用一台电脑的缺点(单点问题)。

    使用本地协议执行如下的命令:      

1 $ git clone /opt/git/project.git

    或可以执行这个命令:      

1 $ git clone file:///opt/git/project.git

    如果在 URL 开头明确的指定 file://,那么 Git 的行为会略有不同。 如果仅是指定路径,Git 会尝试使用硬链接(hard link)或直接复制所需要的文件。 如果指定 file://,Git 会触发平时用于网路传输资料的进程,那通常是传输效率较低的方法。 指定 file:// 的主要目的是取得一个没有外部参考(extraneous references)或对象(object)的干净版本库副本– 通常是在从其他版本控制系统导入后或一些类似情况需要这么做。 在此我们将使用普通路径(即/opt/git/project.git),因为这样通常更快。


      2.HTTP 协议: http协议又分为两种 一种为智能(Smart) HTTP 协议 和 哑(Dumb) HTTP 协议 。命令类似于

1 $ git clone https://example.com/gitproject.git

     3.ssh协议: 架设 Git 服务器时常用 SSH 协议作为传输协议,优点大多数环境下已经支持 SSH 访问,并且非常普遍性,架设和使用都很容易。命令类似于:

1 $ git clone ssh://user@server/project.git

  4.git协议:

扫描二维码关注公众号,回复: 7385579 查看本文章

  Git 协议包含在 Git 里的一个特殊的守护进程(即要安装git-daemon软件包);它监听在一个特定的端口(9418),类似于 SSH 服务,但是访问无需任何授权。 要让版本库支持 Git 协议,需要先创建一个 git-daemon-export-ok 文件 —— 它是 Git 协议守护进程为这个版本库提供服务的必要条件 —— 但是除此之外没有任何安全措施。 要么谁都可以克隆这个版本库,要么谁也不能。 这意味着,通常不能通过 Git 协议推送。 由于没有授权机制,一旦你开放推送操作,意味着网络上知道这个项目 URL 的人都可以向项目推送数据。 不用说,极少会有人这么做。

  注意:git-daemon-export-ok 文件是告诉Git 协议哪个仓库提供无授权访问。

    所以你需要告诉 Git 哪些仓库允许基于服务器的无授权访问。 你可以在相关仓库下创建一个名为 git-daemon-export-ok 的文件来实现。如下:

1 $ cd /path/to/project.git        //进入Git服务器上的project.git版本库存储路径
2 $ touch git-daemon-export-ok

    但有些人会发现自己的版本库没有创建git-daemon-export-ok,而在git clone过程能无授权访问。那是为什么?是因为在git-daemon守护进程启动过程中使用了--export-all 参数选项。

    --export-all  :使用该选项后,在git仓库中就不必创建git-daemon-export-ok文件。如果不使用该选项,则还需要创建git-daemon-export-ok文件。

Git 协议优点:目前,Git 协议是 Git 使用的网络传输协议里最快的。

Git 协议缺点:是缺乏授权机制。 把 Git 协议作为访问项目版本库的唯一手段是不可取的。 一般的做法里,会同时提供 SSH 或者 HTTPS 协议的访问服务,只让少数几个开发者有推送(写)权限,其他人通过 git:// 访问只有读权限。 Git 协议也许也是最难架设的。 它要求有自己的守护进程,这就要配置 xinetd 或者其他的程序,这些工作并不简单。 它还要求防火墙开放 9418 端口,但是企业防火墙一般不会开放这个非标准端口。 而大型的企业防火墙通常会封锁这个端口。

本例搭建本地协议的git服务器:

在搭建本地协议的git服务器以多人使用一台电脑的场景开始,系统以Centos 7为环境:

1)服务器创建一个空版本库:

[root@db-52 ~]# yum -y install git    //安装git
[root@db-52 ~]# mkdir /var/git         //版本库目录
[root@db-52 ~]# cd /var/git
[root@db-52 git]# git init --bare /var/git/wordpress.git   //创建名为wordpress.git空版本库

2)查看版本库的权限

1 [root@db-52 ~]# ll -d /var/git/wordpress.git
2 drwxr-xr-x. 7 root root 4096 9月  29 01:34 /var/git/wordpress.git

3)创建用户,以模拟多人使用一台电脑

1 [root@db-52 ~]# useradd tom
2 [root@db-52 ~]# useradd jack
3 [root@db-52 ~]# echo 1 | passwd --stdin tom
4 [root@db-52 ~]# echo 1 | passwd --stdin jack

4)添加权限:修改对wordpress版本添加tom,jack用户权限(git push需要对wordpress的w权限)

  规划:对WordPress库 tom用户有rw的读和提交权限;jack用户只有r的读的权限。

 1 [root@db-52 ~]# ll -d /var/git/wordpress.git
 2 drwxr-xr-x. 7 root root 4096 9月  29 01:34 /var/git/wordpress.git
 3 [root@db-52 ~]# getfacl /var/git/wordpress.git/
 4 getfacl: Removing leading '/' from absolute path names
 5 # file: var/git/wordpress.git/
 6 # owner: root
 7 # group: root
 8 user::rwx
 9 group::r-x
10 other::r-x
11 [root@db-52 ~]# setfacl -Rm u:tom:rwx /var/git/wordpress.git/
12 [root@db-52 ~]# setfacl -Rm u:jack:rx /var/git/wordpress.git/
13 [root@db-52 ~]# getfacl /var/git/wordpress.git/
14 getfacl: Removing leading '/' from absolute path names
15 # file: var/git/wordpress.git/
16 # owner: root
17 # group: root
18 user::rwx
19 user:tom:rwx
20 user:jack:r-x
21 group::r-x
22 mask::rwx
23 other::r-x

5)tom对WordPress版本库进行下载并提交新增内容 

 1 [root@db-52 ~]# su - tom
 2 [tom@db-52 ~]$ mkdir git
 3 [tom@db-52 ~]$ cd git/
 4 [tom@db-52 git]$ git clone file:///var/git/wordpress.git          //下载一个版本库
 5 正克隆到 'wordpress'...
 6 warning: 您似乎克隆了一个空版本库。
 7 [tom@db-52 git]$ ls
 8 wordpress
 9 [tom@db-52 git]$ cd wordpress/
10 [tom@db-52 wordpress]$ echo wx > index.html
11 [tom@db-52 wordpress]$ echo tom > tom.txt
12 [tom@db-52 ~]$ git config --global push.default simple
13 [tom@db-52 wordpress]$ git config --global user.email "[email protected]"    //全局配置邮箱
14 [tom@db-52 wordpress]$  git config --global user.name "tom"      //全局配置用户名
15 [tom@db-52 wordpress]$ git add .              //
16 [tom@db-52 wordpress]$ git commit -m "tom code"     //
17 [master(根提交) b693669] tom code
18  2 files changed, 2 insertions(+)
19  create mode 100644 index.html
20  create mode 100644 tom.txt
21 [tom@db-52 wordpress]$ git push              //上传新增内容
22 Counting objects: 4, done.
23 Compressing objects: 100% (2/2), done.
24 Writing objects: 100% (4/4), 250 bytes | 0 bytes/s, done.
25 Total 4 (delta 0), reused 0 (delta 0)
26 To file:///var/git/wordpress.git
27  * [new branch]      master -> master
28 [tom@db-52 wordpress]$ exit

6)jack用户对WordPress版本库只有下载无提交功能。

 1 [root@db-52 ~]# su - jack
 2 [jack@db-52 ~]$ mkdir git
 3 [jack@db-52 ~]$ cd git
 4 [jack@db-52 git]$ git clone file:///var/git/wordpress.git/
 5 正克隆到 'wordpress'...
 6 remote: Counting objects: 4, done.
 7 remote: Compressing objects: 100% (2/2), done.
 8 remote: Total 4 (delta 0), reused 0 (delta 0)
 9 接收对象中: 100% (4/4), done.
10 [jack@db-52 git]$ ls
11 wordpress
12 [jack@db-52 git]$ cd wordpress/
13 [jack@db-52 wordpress]$ ls
14 index.html  tom.txt
15 
16 [jack@db-52 wordpress]$ echo jack > jack.txt
17 [jack@db-52 wordpress]$ git config --global user.email "[email protected]"
18 [jack@db-52 wordpress]$ git config --global user.name "jack"
19 [jack@db-52 wordpress]$ git config --global push.default simple
20 
21 
22 [jack@db-52 wordpress]$ git add .
23 [jack@db-52 wordpress]$ git commit -m 'jack code'
24 [master 66b1be3] jack code
25  1 file changed, 1 insertion(+)
26  create mode 100644 jack.txt
27 [jack@db-52 wordpress]$ git push
28 Counting objects: 4, done.
29 Compressing objects: 100% (2/2), done.
30 Writing objects: 100% (4/4), 250 bytes | 0 bytes/s, done.
31 Total 4 (delta 0), reused 0 (delta 0)
32 remote: error: insufficient permission for adding an object to repository database ./objects
33 remote: fatal: failed to write object
34 error: unpack failed: unpack-objects abnormal exit
35 To file:///var/git/wordpress.git
36  ! [remote rejected] master -> master (unpacker error)
37 error: 无法推送一些引用到 'file:///var/git/wordpress.git'
38 [jack@db-52 wordpress]$ 

补充:

  1)git配置文件的优先级

      .git/conifg > ~/.gitconfig > etc/gitconfig

    说明:
      第一个(.git/conifg)指的是特定版本库的配置文件;
      第二个(~/.gitconfig)是当前系统用户环境下的配置文件;
      第三个(etc/gitconfig)是系统级别的配置文件。

搭建中引出问题:

1)/etc/services

2)/etc/inetd

参考:

https://git-scm.com/book/zh/v2/服务器上的-Git-协议 

https://blog.csdn.net/ljl6390221/article/details/85931157

https://blog.csdn.net/ryo1060732496/article/details/88614458

https://www.cnblogs.com/shijiaoyun/p/9663396.html

https://www.liaoxuefeng.com/wiki/896043488029600/899998870925664

猜你喜欢

转载自www.cnblogs.com/gageshen/p/11605171.html
今日推荐