1慢逾、安裝git
? ? ? 服務(wù)器端:
? ? ? ①先檢查服務(wù)器有沒(méi)有自帶或者安裝git;查詢Git版本
[root@localhost ~]# git --version
git version1.8.3.1
[root@localhost~]# rpm -qa git
git-1.8.3.1-6.el7_2.1.x86_64
查詢到?jīng)]有的話就yum安裝一下;
#yum install -y git
安裝完后既鞠,查看Git版本
[root@localhost ~]# git --version
git version1.8.3.1
客戶端:
下載Git for Windows炼邀,地址:https://git-for-windows.github.io/
安裝完之后虫啥,可以使用Git Bash作為命令行客戶端烈钞。
安裝完之后蝙搔,查看Git版本
Administrator@SG MINGW64 ~
$ git--version
git version2.14.1.windows.1
2掸宛、服務(wù)器端創(chuàng)建git用戶,用來(lái)管理Git服務(wù)待牵,并為git用戶設(shè)置密碼
[root@localhost home]# id git
id: git: no such user //無(wú)此用戶
[root@localhost home]# useradd git
[root@localhost home]# passwd git
3严嗜、服務(wù)器端創(chuàng)建Git倉(cāng)庫(kù)
設(shè)置/home/data/git/git.git為Git倉(cāng)庫(kù)
然后把Git倉(cāng)庫(kù)的owner修改為git
[root@localhost home]# mkdir -p data/git/git.git
[root@localhost home]# git init --bare data/git/git.git
初始化空的Git版本庫(kù)于/home/data/git/git.git/
[root@localhost home]# cd data/git/
[root@localhost git]# chown -R git:git git.git/
4、客戶端clone遠(yuǎn)程倉(cāng)庫(kù)
Centos7
Git服務(wù)器上clone項(xiàng)目:
$ git clone git@192.168.20.101:/home/data/git.git
執(zhí)行過(guò)程如下圖所示:
當(dāng)?shù)谝淮芜B接到目標(biāo)Git服務(wù)器時(shí)會(huì)得到一個(gè)提示:
輸入yes:
The authenticity of host‘192.168.20.101 (192.168.20.101)‘can‘t be established.
ECDSA key fingerprintisSHA256:HEaAUZgd3tQkEuwzyVdpGowlI6YKeQDfTBS6vVkY6Zc.
Are you sure you want tocontinueconnecting (yes/no)?
輸入git設(shè)置的密碼:
Warning: Permanently added‘192.168.20.101‘(ECDSA) to the list of known hosts.
git@192.168.20.101‘s password:
此時(shí)C:\Users\用戶名\.ssh下會(huì)多出一個(gè)文件known_hosts洲敢,以后在這臺(tái)電腦上再次連接目標(biāo)Git服務(wù)器時(shí)不會(huì)再提示上面的語(yǔ)句漫玄。
后面提示要輸入密碼,可以采用SSH公鑰來(lái)進(jìn)行驗(yàn)證压彭。
5睦优、客戶端創(chuàng)建SSH公鑰和私鑰
$ ssh-keygen -t rsa -C"1838370@qq.com"
此時(shí)C:\Users\用戶名\.ssh下會(huì)多出兩個(gè)文件id_rsa和id_rsa.pub
id_rsa是私鑰
id_rsa.pub是公鑰
6、服務(wù)器端Git打開(kāi)RSA認(rèn)證
進(jìn)入/etc/ssh目錄壮不,編輯sshd_config汗盘,打開(kāi)以下三個(gè)配置的注釋:
[root@localhost?git]#cd /etc/ssh/
[root@localhost?ssh]#vim sshd_config
RSAAuthenticationyes
PubkeyAuthentication
yes
AuthorizedKeysFile
.ssh/authorized_keys
:wq保存
保存并重啟sshd服務(wù):
[root@localhost?ssh]#systemctl restart sshd.service
由AuthorizedKeysFile得知公鑰的存放路徑是.ssh/authorized_keys,
實(shí)際上是$Home/.ssh/authorized_keys询一,
由于管理Git服務(wù)的用戶是git隐孽,所以實(shí)際存放公鑰的路徑是/home/git/.ssh/authorized_keys
在/home/git/下創(chuàng)建目錄.ssh
[root@localhost git]# pwd
/home/git
[root@localhost git]# mkdir .ssh
[root@localhost git]# ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla .ssh
然后把.ssh文件夾的owner修改為git
[root@localhost git]# chown -R git:git .ssh
[root@localhost git]# ll-a
總用量32
drwx------.5gitgit40968月2820:04.
drwxr-xr-x.8root root40968月2819:32..
-rw-r--r--.1gitgit1810月162014.bash_logout
-rw-r--r--.1gitgit17610月162014.bash_profile
-rw-r--r--.1gitgit12410月162014.bashrc
drwxr-xr-x.2gitgit409611月122010.gnome2
drwxr-xr-x.4gitgit40965月812:22.mozilla
drwxr-xr-x.2gitgit40968月2820:08.ssh
7、將客戶端公鑰導(dǎo)入服務(wù)器端/home/git/.ssh/authorized_keys文件
回到Git Bash下健蕊,導(dǎo)入文件:
Administrator@SG-20170206ABCD?MINGW64/d/wamp/www/gittest_gitbash
$ ssh?git@192.168.20.101?‘cat >> .ssh/authorized_keys‘ <~/.ssh/id_rsa.pub
需要輸入服務(wù)器端git用戶的密碼
回到服務(wù)器端菱阵,查看.ssh下是否存在authorized_keys文件:
[root@localhost git]# cd .ssh
[root@localhost .ssh]# ll
總用量4
-rw-rw-r--. 1 git git 398 8月28 20:08 authorized_keys
可以查看一下是否是客戶端生成的公鑰。
重要:
修改.ssh目錄的權(quán)限為700
修改.ssh/authorized_keys文件的權(quán)限為600
[root@localhost git]# chmod 700 .ssh
[root@localhost git]# cd .ssh
[root@localhost .ssh]# chmod 600 authorized_keys
8缩功、客戶端再次clone遠(yuǎn)程倉(cāng)庫(kù)
$ git clone git@192.168.20.101:/home/data/git/git.git
查看客戶端項(xiàng)目目錄:
項(xiàng)目已經(jīng)clone了晴及。
也可以使用tortoiseGit客戶端來(lái)管理項(xiàng)目:
9、禁止git用戶ssh登錄服務(wù)器
之前在服務(wù)器端創(chuàng)建的git用戶不允許ssh登錄服務(wù)器
編輯/etc/passwd
找到:
git:x:502:504::/home/git:/bin/bash
修改為
git:x:502:504::/home/git:/bin/git-shell
此時(shí)git用戶可以正常通過(guò)ssh使用git嫡锌,但無(wú)法通過(guò)ssh登錄系統(tǒng)虑稼。