詳解在Linux下搭建Git伺服器

2022-09-26 15:36:16 字數 3953 閱讀 8961

眾所周知,版本系統在開發環境中是必不可少的,但是我們可以把**免費的託管到github上,如果我們不原意公開專案的源**,公司又不想付費使用,那麼我們可以自己搭建一xwhff臺git伺服器,可以用gitosis來管理公鑰,還是比較方便的。

搭建環境:

伺服器 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,位址:

安裝完之後,可以使用git bash作為命令列客戶端。

$ git --version

git version 2.11.1.windows.1 //安裝完之後,檢視git版本

安裝gitosis

[root@linuxprobe ~]# cd software/

[root@linuxprobe software]# git clone

[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 [email protected]:~ //複製到git伺服器上

4. 配置gitosis

使用git使用者並初始化gitosis

[root@linuxprobe ~]# cd .ssh

[root@linuxprobe ~]# gitosis-init < ./id_rsa.pub

initialized empty git repowww.cppcns.comsitory 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:

$ 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 protected]"

$ cd ~/repo

$ mkdir test

$ git init

$ tocuh readme.txt

提交到遠端伺服器

$ git add .

$ git commit -a -m 'init test'

$ git remote add repo [email protected]: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 [email protected] //修改公鑰為主機名.pub

$ vim gitosis.conf

[group test]

w程式設計客棧ritable = 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 cl程式設計客棧one [email protected]:/home/git/repositories/test.git

報錯問題:error:gitosis serve main repository read access denied

根據這個報錯,可以看出key是沒問題的,通過排查,發現不應該把這個/home/git/repositories/test.git寫全,git clone [email protected]:test.git

這樣就可以了。

本文標題: 詳解在linux下搭建git伺服器

本文位址:

在Linux通過Apache搭建git伺服器

初始化git倉庫 git init bare 1.1 建立新使用者 然後輸入該使用者要使用的密碼。1.2 修改git team.htpasswd檔案的所有者與所屬群組 1.3 設定git team.htpasswd檔案的訪問許可權 2.1 用vim開啟httpd.conf vi etc httpd ...

在Linux上用Apache搭建Git伺服器

最近在學linux,終於在linux上用apache搭建起了git伺服器,在此記錄一下。伺服器 阿里雲伺服器 linux版本 centos 6.5 apache版本 apache 2.2.15 git版本 git 1.7.1 git訪問方式 基於http的基本驗證 非ssl apache的安裝 1....

在Linux上用Apache搭建Git伺服器

最近在學linux,終於在linux上用apache搭建起了git伺服器,在此記錄一下。伺服器 阿里雲伺服器 linux版本 centos 6.5 apache版本 apache 2.2.15 git版本 git 1.7.1 git訪問方式 基於http的基本驗證 非ssl apache的安裝 1....