https://blog.csdn.net/yageeart/article/details/78947272
git的安裝:
yum 源倉庫里的 Git 版本更新不及時,最新版本的 Git 是 1.8.3.1,但是官方最新版本已經(jīng)到了 2.9.2。想要安裝最新版本的的 Git,只能下載源碼進(jìn)行安裝。
1. 查看 yum 源倉庫的 Git 信息:
1# yum info git
可以看出,截至目前,yum 源倉庫中最新的 Git 版本才 1.8.3.1,而查看最新的 Git 發(fā)布版本,已經(jīng) 2.9.2 了。
2. 依賴庫安裝
# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
# yum install gcc perl-ExtUtils-MakeMaker
3. 卸載低版本的 Git
通過命令:git –-version查看系統(tǒng)帶的版本,Git 版本是:1.8.3.1,所以先要卸載低版本的 Git,命令:
1# yum remove git
4. 下載新版的 Git 源碼包(我放的了 ?/usr/local/git 的目錄下了,git是我自己mkdir的目錄)
進(jìn)入:/usr/local下,新建git目錄: ?# mkdir git?
# cd git?
在線下載最新的源碼包
1# wget https://github.com/git/git/archive/v2.9.2.tar.gz
也可以離線下載,然后傳到 CentOS 系統(tǒng)中指定的目錄下。
5. 解壓當(dāng)前目錄
1# tar -xzvf v2.9.2.tar.gz
6. 安裝 Git
分別執(zhí)行以下命令進(jìn)行編譯安裝,編譯過程可能比較漫長,請耐心等待完成。
# cd git-2.9.2
# make prefix=/usr/local/git all
# make prefix=/usr/local/git install
7. 添加到環(huán)境變量
vim /etc/profile ?
#如果沒有vim,則安裝vim工具 ? yum install vim
添加這一條: ? export PATH="/usr/local/git/bin:$PATH"?
source /etc/profile ? #是配置立即生效
8. 查看版本號
# git --version
git version 2.9.2
9. 將git設(shè)置為默認(rèn)路徑,不然后面克隆時會報錯
[root@localhost?code]$ ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack
[root@localhost?code]$ ln -s /usr/local/git/bin/git-receive-pack /usr/bin/git-receive-pack
至此,CentOS 就安裝上了最新版本的 Git。
第二步,創(chuàng)建一個git用戶組和用戶,用來運行g(shù)it服務(wù):
$ groupadd git$ useradd git -g git$ passwd git? #參數(shù)是用戶名
最好切換到git用戶 不然后面新建的git倉庫都要改權(quán)限 煩煩煩?。?/p>
$ su - git
第三步,創(chuàng)建證書登錄:
如何生成密鑰:http://www.cnblogs.com/xuange306/p/6403907.html
備注:下邊虛線內(nèi)容為多余內(nèi)容,只是留著存檔而已。于本教程沒有關(guān)系
添加證書之前,還要做這么一步:
Git服務(wù)器打開RSA認(rèn)證 。在Git服務(wù)器上首先需要將/etc/ssh/sshd_config中將RSA認(rèn)證打開,
即:
1.RSAAuthentication yes
2.PubkeyAuthentication yes
3.AuthorizedKeysFile .ssh/authorized_keys
這里我們可以看到公鑰存放在.ssh/authorized_keys文件中。
所以我們在/home/git下創(chuàng)建.ssh目錄,然后創(chuàng)建authorized_keys文件,并將剛生成的公鑰導(dǎo)入進(jìn)去。
然后再次clone的時候,或者是之后push的時候,就不需要再輸入密碼了:
Zhu@XXX/E/testgit/8.34 $ git clone?git@192.168.8.34:/data/git/learngit.git Cloning into ‘learngit‘... warning: You appear to have cloned an empty repository. Checking connectivity... done.
===============================
收集所有需要登錄的用戶的公鑰,就是他們自己的id_rsa.pub文件,把所有公鑰導(dǎo)入到/home/git/.ssh/authorized_keys文件里,一行一個。
$ cd /home/git/$ mkdir .ssh #新建文件夾$ chmod 700 .ssh
$ touch .ssh/authorized_keys? #新建文件$ chmod 600 .ssh/authorized_keys
第四步,初始化Git倉庫
$ cd /home/git$ git init --bare test.gitInitialized empty Git repository in /home/git/test.git/
以上命令會創(chuàng)建一個空倉庫,服務(wù)器上的Git倉庫通常都以.git結(jié)尾。
特別注意,如果后期在增加新的倉庫是,因為無法連接git用戶了,所以有兩種方式進(jìn)行添加倉庫
1:把配置文件改回來,進(jìn)入git用戶,增加倉庫
2:用root用戶創(chuàng)建倉庫,讓后進(jìn)行更改文件所屬owner
$chown -R git:git test.git?
第五步、本地克隆倉庫
$ git clone git@your-ip:test.gitCloning into ‘test‘...warning: You appear to have cloned an empty repository.Checking connectivity... done.
your-ip 為您 Git 所在服務(wù)器 ip?
用git clone 獲取服務(wù)器上的代碼
[root@localhost?code]$ git clone?root@192.168.57.61:/root/code.git
報錯如下:
bash: git-upload-pack: command not found
fatal: The remote end hung up unexpectedly
什么原因呢?原來代碼服務(wù)器【192.168.57.61】上的git安裝路徑是/usr/local/git,不是默認(rèn)路徑,根據(jù)提示,在git服務(wù)器192.168.57.61上, 建立鏈接文件:
[root@localhost?code]# ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack
再次,執(zhí)行g(shù)it clone ,果真可以了。
當(dāng)然,如果無法修改git代碼服務(wù)器上配置,可以在clone時,添加--upload-pack選項來指定git服務(wù)器上的git-upload-pack 路徑,達(dá)到上面相同的目的,如下所示:
[root@localhost?code]$ git clone --upload-pack "/usr/local/git/bin/git-upload-pack"?root@192.168.57.61:/root/code.git
當(dāng)然,也許你會遇到git-receive-pack 之類的錯誤,很有可能和這個原理是一樣的,請采用類似的操作即可
5.禁止Shell登錄
出于安全考慮,git用戶不允許登錄shell,這可以通過編輯/etc/passwd文件完成。
找到類似下面的一行:
git:x:502:502::/home/git:/bin/bash
改為
git:x:502:502::/home/git:/usr/local/git/bin/git-shell
這樣,git用戶可以正常通過ssh使用git,但無法登錄shell,因為我們?yōu)間it用戶指定的git-shell每次一登錄就自動退出
參考文檔:
git安裝部署? http://www.cnblogs.com/imayanlong/p/5617626.html
git/ssh捋不清的幾個問題? http://www.cnblogs.com/hustskyking/p/problems-in-git-when-ssh.html
centos7下git服務(wù)器端搭建和驗證? https://my.oschina.net/u/2343829/blog/644663?
Git 服務(wù)器搭建? http://www.runoob.com/git/git-server.html?
Windows下SSH連接git服務(wù)器 https://www.zybuluo.com/zhutoulwz/note/30495? ? ? ? ? ? ? ? ? ? ? ?
轉(zhuǎn)自:https://www.cnblogs.com/xuange306/p/6402796.html