Linux 環境 搭建Git 服務器,並且修改SSH端口使用


1.環境配置說明

服務器 CentOS 7 + git(git version 1.8.3.1)
客戶端 Windows10 + SourceTree

2.安裝 Git

服務器端安裝:

sudo yum -y install git

查看安裝版本

#git --version
git version 1.8.3.1

客戶端安裝

下載 SourceTree for Window 下載地址:https://www.sourcetreeapp.com

3.git 服務器端創建 git 用戶,用來管理 Git 連接使用服務,並為 git 用戶設置密碼,並且注冊此用戶無許登錄SSH權限

[root@localhost home]#id git
id: git: no such user
[root@localhost home]#groupadd -g 108 -r git && useradd -s /sbin/nologin -u 108 -r -g git git
[root@localhost home]#passwd git

 

4.在服務器端創建 Git 倉庫

[root@localhost home]#mkdir -p /opt/data/test.git
[root@localhost home]#git init --bare /opt/data/test.git
Initialized empty Git repository in /opt/data/test.git/
[root@localhost home]#chown -R git:git /opt/data/test.git

 

5.使用SourceTree 客戶端打開終端 創建SSH公鑰與私鑰證書

1.創建證書,請在注明郵箱地址填寫自己使用使用郵箱,因后面clone 會有認證

$ssh-keygen -t rsa -C "請輸入郵箱地址"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Steven/.ssh/id_rsa):直接回車
Enter passphrase (empty for no passphrase):可輸入密鑰密碼
Enter same passphrase again:確定密鑰密碼
Your identification has been saved in /c/Users/Steven/.ssh/id_rsa. 密鑰所存放的位置
Your public key has been saved in /c/Users/Steven/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ryzmTky3Ar0l+gHX8m53VPJ0iwRuez1YgDlzomaaPtM 
完整密鑰這里會有你的郵箱地址
The key's randomart image is:
+---[RSA 2048]----+
|            o    |
|           B o   |
|          o * .  |
|     . . + o..o..|
|    o * S . o==..|
|     B X o ..+.+ |
|    . B + ...   .|
|     ooO.E. .    |
|     ++o*. .     |
+----[SHA256]-----+

 

2.在服務器上配置SSH 認證支持密鑰

進入vim  /etc/ssh 目錄,編輯 sshd_config,打開以下三個配置的注釋:

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

保存之后並重啟 sshd 服務:

[root@localhost home]#systemctl restart sshd

因 AuthorizedKeysFile sshd_config 配置公鑰的存放路徑是 .ssh/authorized_keys,但實際上是 $Home/.ssh/authorized_keys,由於管理使用Git 服務的用戶是 git,所以實際存放公鑰的路徑是 /home/git/.ssh/authorized_keys,同時把上一步所配置得到的id_rsa.pub 上傳到服務器端

[root@localhost git]# pwd
/home/git
[root@localhost git]# mkdir .ssh
[root@localhost git]# ls -a 
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla .ssh id_rsa.pub

 

3.將客戶端公鑰導入服務器端 /home/git/.ssh/authorized_keys 文件

[root@localhost git]#cat id_rsa.pub >> /home/git/.ssh/authorized_keys

重要:

修改 .ssh 目錄的權限為 700

修改 .ssh/authorized_keys 文件的權限為 777

[root@localhost git]# chmod 700 .ssh
[root@localhost git]# cd .ssh
[root@localhost .ssh]# chmod 777 authorized_keys 

 

 

6.客戶端 SourceTree clone 遠程倉庫

重點注意:如果SSH 修改默認端口,配置方式跟默認並不一樣

正常未修改SSH 端口:

git clone git@192.168.10.10:/opt/data/test.git

 

修改SSH端口配置方式

ssh://git@192.168.10.10:32569/opt/data/test.git

 

以上配置完成可正常使用

參考:在CentOS下搭建自己的Git服務器

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM