centos7.6搭建git服務器:ssh方式

參照:https://blog.csdn.net/qq_42176520/article/details/83541015
實驗環境:

  1. centos7.6x64
  2. git-2.9.5.tar.gz
  3. TortoiseGit 2.7.0.0、git-window

一、下載git源碼

瀏覽器訪問:https://mirrors.edge.kernel.org/pub/software/scm/git/
在這裏插入圖片描述
將下載後的壓縮包上傳到centos的/soft目錄

二、安裝依賴

[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel gcc -y
此時輸入git version命令會發現已經存在git了:
在這裏插入圖片描述
那麼把這個git移除掉:
[root@localhost ~]# yum remove git -y

三、編譯安裝git-2.9.5

3.1 解壓壓縮包

tar -zxf git-2.9.5.tar.gz
在這裏插入圖片描述

3.2 編譯並安裝

下面兩條命令將git編譯並安裝到/usr/local/git目錄
[root@localhost git-2.9.5]# make prefix=/usr/local/git all
[root@localhost git-2.9.5]# make prefix=/usr/local/git install

3.3 修改環境變量

編輯文件/etc/profile,在最後一行添加:
export PATH="/usr/local/git/bin:$PATH"
如下圖:
在這裏插入圖片描述

3.3 使修改的環境變量生效

[root@localhost git-2.9.5]# source /etc/profile

3.4 測試git git version

在這裏插入圖片描述
到這裏,git服務器的安裝就完成了,下面是git使用的用戶權限和倉庫的配置測試。

四、準備訪問git的用戶和用戶組

4.1 創建用戶組:gitg-test、gitg-test2

[root@localhost git-2.9.5]# groupadd gitg-test
[root@localhost git-2.9.5]# groupadd gitg-test2

4.2 創建用戶xiaoming、xiaohong、xiaogang

[root@localhost git-2.9.5]# useradd xiaoming
[root@localhost git-2.9.5]# passwd xiaoming
更改用戶 xiaoming 的密碼 。
新的 密碼:
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
[root@localhost git-2.9.5]# useradd xiaohong
[root@localhost git-2.9.5]# passwd xiaohong
更改用戶 xiaohong 的密碼 。
新的 密碼:
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
[root@localhost git-2.9.5]# useradd xiaogang
[root@localhost git-2.9.5]# passwd xiaogang
更改用戶 xiaogang 的密碼 。
新的 密碼:
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。

注意:使用的密碼均爲123qweQWE

4.3 禁止xiaoming、xiaohong、xiaogang的shell登錄

修改/etc/passwd文件內容如下:
在這裏插入圖片描述
此時你再用xiaoming、xiaohong、xiaogang遠程shell登錄是不行的。

4.4 用戶和組的分屬情況:

在這裏插入圖片描述

[root@localhost git-2.9.5]# gpasswd gitg-test -a xiaoming
正在將用戶“xiaoming”加入到“gitg-test”組中
[root@localhost git-2.9.5]# gpasswd gitg-test2 -a xiaohong
正在將用戶“xiaohong”加入到“gitg-test2”組中
[root@localhost git-2.9.5]# gpasswd gitg-test -a xiaogang
正在將用戶“xiaogang”加入到“gitg-test”組中
[root@localhost git-2.9.5]# gpasswd gitg-test2 -a xiaogang
正在將用戶“xiaogang”加入到“gitg-test2”組中

五、創建git倉庫目錄test.git和test2.git,並分別隸屬到組gitg-test、gitg-test2

5.1 使用root創建/repo目錄,這個目錄是服務器所有倉庫的存放位置

[root@localhost git-2.9.5]# mkdir /repo

5.2 初始化test.git和test2.git倉庫

[root@localhost git-2.9.5]# cd /repo
[root@localhost repo]# git init --bare test.git
初始化空的 Git 倉庫於 /repo/test.git/
[root@localhost repo]# git init --bare test2.git
初始化空的 Git 倉庫於 /repo/test2.git/

5.3 分別對倉庫賦予隸屬關係和權限

在這裏插入圖片描述

[root@localhost repo]# chown -R xiaoming:gitg-test test.git
[root@localhost repo]# chown -R xiaohong:gitg-test2 test2.git
[root@localhost repo]# chmod -R 770 test.git
[root@localhost repo]# chmod -R 770 test2.git

chmod命令是指定擁有者和所屬組都具有讀、寫、執行的權限,其他人沒有任何權限

六、客戶端克隆倉庫

注意:
客戶端的git環境(windows):
1. 只使用Git-2.23.0-64-bit.exe 然後在命令行中操作
2. 使用Git-2.23.0-64-bit.exeTortoiseGit-2.8.0.0-64bit.msi然後可以在tortoise客戶端中操作

測試用戶對倉庫的使用權限:
使用xiaoming用戶克隆倉庫test.git和test2.git,按提示輸入密碼:

git clone ssh://[email protected]/repo/test.git

git clone ssh://[email protected]/repo/test2.git

效果如下圖:
在這裏插入圖片描述
可以看到用戶xiaoming可以對倉庫test.git進行操作,卻無法訪問test2.git
所以說:
xiaoming只能訪問倉庫test.git
xiaohong只能訪問倉庫test2.git
xiaogang可以同時訪問倉庫test.git和test2.git
總結:
git倉庫中的權限控制依賴的是linux系統自帶的權限機制,如果想新建倉庫和控制倉庫訪問權限的話就使用linux自帶的用戶和組以及對目錄的賦權就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章