服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - 详解在Linux下搭建Git服务器

详解在Linux下搭建Git服务器

2022-01-07 19:19岳永 Linux

本篇文章主要介绍了详解在Linux下搭建Git服务器,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

搭建环境:

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

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

1. 安装Git相关软件

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

安装服务端:

?
1
2
3
[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作为命令行客户端。

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

安装Gitosis

?
1
2
3
4
5
[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

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

?
1
2
Using /usr/lib/python2.6/site-packages
Finished processing dependencies for gitosis==0.2

2. 服务器端创建git用户来管理Git服务

?
1
2
3
4
5
[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命令行

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

4. 配置gitosis

使用git用户并初始化gitosis

?
1
2
3
4
5
[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到管理者主机

?
1
2
3
4
$ 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文件

?
1
2
3
4
5
6
7
8
9
10
$ 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服务端

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

提交到远程服务器

?
1
2
3
4
$ 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仓库

?
1
2
3
4
[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文件,然后传到服务器上,配置好后,用户即获得项目权限,可以从远程仓库拉取和推送项目,达到共同开发项目。

?
1
2
3
4
5
6
$ 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  //添加成员
?
1
2
3
$ git add .
$ git commit -m "add zhangsan@SHA2-002 pub and update gitosis.conf"
$ git push repo master

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

?
1
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

这样就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.linuxprobe.com/linux-deploy-git.html

延伸 · 阅读

精彩推荐
  • Linux学会这8个技巧让你成为一个超强的Linux终端用户

    学会这8个技巧让你成为一个超强的Linux终端用户

    这篇文章是写给缺乏经验的新手的,高手相信都知道,使用Linux终端不仅仅是只输入命令。学习这些基本的技巧,你就会逐渐掌握Bash shell,这个在大多数...

    Linux教程网3272019-10-29
  • Linux致命的7个Linux命令

    致命的7个Linux命令

    如果你是一个 Linux 新手,在好奇心的驱使下,可能会去尝试从各个渠道获得的命令。以下是 7 个致命的 Linux 命令,轻则使你的数据造成丢失,重则使你的...

    Linux教程网9412021-10-08
  • LinuxLinux系统中strace命令的使用教程

    Linux系统中strace命令的使用教程

    这篇文章主要介绍了Linux系统中strace命令的使用教程,strace命令用于追踪和统计系统调用信息,需要的朋友可以参考下...

    开源中文社区2432019-07-15
  • Linux安装 Linux,只需三步

    安装 Linux,只需三步

    Linux操作系统的安装看似神秘,但其实很简单。以下是成功安装 Linux 的步骤。...

    Linux中国8392021-03-02
  • LinuxLinux系统中下载文件的wget命令操作实例

    Linux系统中下载文件的wget命令操作实例

    wget是Linux系统中用来下载文件的工具,其功能还是比较多的,下面小编将针对wget命令的用法给大家做个实例介绍,11个实例中覆盖的功能是比较全面的,感...

    Linux之家6012019-10-17
  • LinuxLinux操作系统主机名(hostname)简介

    Linux操作系统主机名(hostname)简介

    本文是关于Linux操作系统主机名(hostname)的文档,对主要配置文件/etc/hosts进行简要的说明 ;另外对基配具工具hostname也进行了举例说明; 欢迎高手斧正,谢...

    Linux教程网3392020-04-19
  • Linuxlinux .htaccess 设置 404 等错误页面

    linux .htaccess 设置 404 等错误页面

    linux服务器下通过设置htaccess来实现404转向的代码 ...

    Linux教程网4412020-06-01
  • Linuxlinux三个特殊权限setuid、setgid和stick bit使用详解

    linux三个特殊权限setuid、setgid和stick bit使用详解

    今天一直在弄Samba服务的配置,深深的感受到权限控制的困难,文件权限的机制是Linux系统中的一大特色,除了我们现在所熟知的读(r)、写(w)、执行(...

    Linux教程网3352019-12-03