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

在Linux下搭建Git服务器

30次阅读
没有评论

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

导读 众所周知,版本系统在开发环境中是必不可少的,但是我们可以把代码免费的托管到 GitHub 上,如果我们不原意公开项目的源代码,公司又不想付费使用,那么我们可以自己搭建一台 Git 服务器,可以用 Gitosis 来管理公钥,还是比较方便的。

在 Linux 下搭建 Git 服务器

搭建环境:

服务器 CentOS6.6 + git(version 1.8.3.1)

客户端 Windows10 + git(version 2.11.1.windows.1)

1. 安装 Git 相关软件

Linux 是服务器端系统,Windows 作为客户端系统,分别安装 Git

安装服务端:

[root@linuxprobe ~]# yum install -y git
[root@localhost ~]# git --version     // 安装完后,查看 Git 版本
git version 1.8.3.1

安装客户端:

下载 Git for Windows,地址:https://git-for-windows.github.io/

安装完之后,可以使用 Git Bash 作为命令行客户端。

$ git --version
git version 2.11.1.windows.1       // 安装完之后,查看 Git 版本

安装 Gitosis

[root@linuxprobe ~]# cd software/
[root@linuxprobe software]# git clone https://github.com/res0nat0r/gitosis.git
[root@linuxprobe software]# yum install python-setuptools -y
[root@linuxprobe software]# cd gitosis
[root@linuxprobe gitosis]# sudo python setup.py install

出现下面的信息表示安装成功了

Using /usr/lib/python2.6/site-packages
 Finished processing dependencies for gitosis==0.2
2. 服务器端创建 git 用户来管理 Git 服务
[root@linuxprobe ~]# id git      // 查看 git 用户是否存在
id: git: no such user
[root@linuxprobe ~]# useradd git
[root@linuxprobe ~]# echo "123" | passwd --stdin git
[root@linuxprobe ~]# su - git   // 切换到 git 用户下
3. 配置公钥

在 Windows 上配置管理者,git 服务器需要一些管理者,通过上传开发者机器的公钥到服务器,添加成为 git 服务器的管理者, 打开 git 命令行

$ ssh-keygen -t rsa     // 一直回车,不需要设置密码
~ scp ~/.ssh/id_rsa.pub git@192.168.34.184:~    // 复制到 git 服务器上
4. 配置 gitosis

使用 git 用户并初始化 gitosis

[root@linuxprobe ~]# cd .ssh
[root@linuxprobe ~]# gitosis-init < ./id_rsa.pub
Initialized empty Git repository in /home/git/repositories/gitosis-admin.git/
Reinitialized existing Git repository in /home/git/repositories/gitosis-admin.git/
[root@linuxprobe ~]# chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update     // 添加权限

在 Windows 上机器上 clone gitosis-admin 到管理者主机

$ git clone ssh://git@192.168.34.184:22/gitosis-admin.git
$ cd gitosis-admin
$ ls
$ gitosis.conf	keydir

gitosis.conf: git 服务器配置文件

keydir: 存放客户端公钥

配置 gitosis.conf 文件

$ vim gitosis.conf
[gitosis]

[group gitosis-admin]            # 组名称
members = yueyong@SHA2-001       # 组成员
writable = gitosis-admin         # 项目名称

[group test]               // 这里添加了 "test" 项目组, 上传到个 git 服务器
members = yueyong@SHA2-001
writable = test

在 Windows 管理者机器上创建本地 test 仓库,并上传到 git 服务端

$ git config --global user.name "Your Name"         // 第一次提交需要设置个人信息,设置用户名和邮箱
$ git config --global user.email "email@example.com"
$ cd ~/repo 
$ mkdir test
$ git init
$ tocuh readme.txt

提交到远程服务器

$ git add .
$ git commit -a -m 'init test'
$ git remote add repo git@192.168.186.129:test.git   //repo 远程库的名称,可以换成任意名称
$ git push repo master    // 上传本地所有分支代码到远程对应的分支上

服务端会自动创建 test 仓库

[git@repositories]# pwd
/home/git/repositories
[git@linuxprobe repositories]$ ls
gitosis-admin.git  test.git
5. 添加其他 git 用户开发者

由于公司开发团队人数不断增多,手动添加开发者私钥到 /home/git/.ssh/authorized_keys 比较麻烦,通过上面的 Windows 机器的管理者统一收集其他开发者的私钥 id_rsa.pub 文件,然后传到服务器上,配置好后,用户即获得项目权限,可以从远程仓库拉取和推送项目,达到共同开发项目。

$ cd ~/gitosis-admin/keydir
$ mv ~/id_rsa.pub zhangsan@SHA2-002.pub          // 修改公钥为主机名.pub
$ vim gitosis.conf
  [group test]
  writable = test
  members = yueyong@SHA2-001 zhangsan@SHA2-002    // 添加成员
$ git add .
$ git commit -m "add zhangsan@SHA2-002 pub and update gitosis.conf"
$ git push repo master

推送完成后,新加进来的开发者就可以进行项目的开发了,后续增加人员可以这样添加进来,开发者直接把仓库 clone 下来就可以了。

git clone git@192.168.34.184:/home/git/repositories/test.git
报错问题:ERROR:gitosis serve main repository read access denied
根据这个报错,可以看出 key 是没问题的,通过排查,发现不应该把这个 /home/git/repositories/test.git 写全,git clone git@192.168.34.184:test.git
这样就可以了。

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