利用阿里雲搭建私有Git服務器


服務器系統:Centos 6 (查看centos版本命令:lsb_release -a

客戶端系統:Windows 7

一、服務器端安裝Git

==通常centos上使用yum源安裝的git版本過低==

 

1. 檢查系統上是否已經安裝git,若已有則卸載

// 查看當前git版本
# git --version
git version 1.7.1

// 卸載舊版本
# yum remove -y git

 

2. 安裝依賴包,下載最新版本git源碼

# yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
# wget https://github.com/git/git/archive/v2.13.2.tar.gz
# tar zxf v2.13.2.tar.gz

 

3. 安裝git,配置環境變量

# cd git-2.13.2
# make prefix=/usr/local/git all
# make prefix=/usr/local/git install
# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
# source /etc/bashrc    // 實時生效 

 

4. 查看git版本號,正確顯示則安裝成功

# git --version
git version 2.13.2

 

5. 若編譯時報錯如下

libgit.a(utf8.o): In function `reencode_string_iconv':
/usr/local/src/git-2.13.2/utf8.c:463: undefined reference to `libiconv'
libgit.a(utf8.o): In function `reencode_string_len':
/usr/local/src/git-2.13.2/utf8.c:524: undefined reference to `libiconv_open'
/usr/local/src/git-2.13.2/utf8.c:535: undefined reference to `libiconv_close'
/usr/local/src/git-2.13.2/utf8.c:529: undefined reference to `libiconv_open'
collect2: ld returned 1 exit status
make: *** [git-credential-store] Error 1 

可以按照如下方式解決

// 對之前git的make 操作進行 make clean
# make clean
# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
# tar zxf libiconv-1.14.tar.gz
# cd libiconv-1.14
# ./configure --prefix=/usr/local/libiconv
# make && make install
// 創建一個軟鏈接到/usr/lib
# ln -s /usr/local/lib/libiconv.so /usr/lib
# ln -s /usr/local/lib/libiconv.so.2 /usr/lib

然后

# make configure
# ./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv/
# make && make install
# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
# source /etc/bashrc

 

6. 配置git 用戶名及郵箱(客戶機安裝后也要配置,在這里略過windows的git安裝過程)

# git config --global user.name 'your name'
# git config --global user.email 'your email address'

 

二、服務器設置

1、添加一個git用戶

# useradd git

 

2、初始化一個項目目錄為一個倉庫

su git
//git的~的實際地址為/home/git
cd ~
//yourName為自定義的倉庫名稱
mkdir yourName.git
cd yourName.git
git init --bare

 至此,我們的倉庫完整地址為/home/git/yourName.git

【問題】:建立遠程倉庫使用 git init 命令,也可以增加 --bare 參數。寫不寫 --bare 參數有什么區別呢?

【答案】:

  我們知道,一般從遠程 clone 下來的倉庫會生成一個獨立的目錄,在這個目錄下有當前分支最新版本的文件,同時還有一個 .git 文件夾,與 .git 同級的文件夾稱為我們的“工作目錄”,我們的修改都在這個目錄中進行。而 .git 就是我們 Git 本地倉庫的工作目錄,我們 add 和 commit 的東西都會被提交到這個目錄中。對 git init 命令添加 --bare 參數就表示初始化 Git 倉庫的時候不要創建本地工作目錄,所以相當於 .git 文件夾下的所有內容直接創建到當前目錄下,而不是被放到 .git 目錄下。在 Git 服務器上建立好倉庫以后,用戶就可以克隆這個倉庫了。等等。。還沒配置用戶 SSH 公鑰呢,這么就讓用戶去下載,肯定還是要輸入密碼才行的。

 

3、在 Git 服務器上為用戶配置 SSH 公鑰

 還是先在 Git 服務器上使用 authorized_keys 文件來管理所有用戶的 SSH 公鑰。(密鑰登錄的方式比密碼登錄更安全、更便捷,注意保管好自己的私鑰,下面會講到如何生成秘鑰對)

git@Linux:~$ mkdir .ssh
git@Linux:~$ touch .ssh/authorized_keys
git@Linux:~$ chmod 600 .ssh/authorized_keys 
git@Linux:~$

注意:這里的authorized_keys跟配置好的centos的證書方式ssh登錄不同(如已配置),我們git的證書文件路徑為/home/git/.ssh/authorized_keys(ssh終端登錄所用證書文件路徑為/etc/ssh/authorized_keys,一般使用xshell或者putty等工具用的證書登錄ssh所用的pub密鑰信息都在里面)

 

4、打開服務器的RSA認證

# vim /etc/ssh/sshd_config
// 找到下面3行並去掉注釋
 1. RSAAuthentication yes     
 2. PubkeyAuthentication yes     
 3. AuthorizedKeysFile  .ssh/authorized_keys

 重啟sshd

service sshd restart

 

5、為安全起見禁用git用戶shell登錄

// 為安全起見,禁用 git 用戶的 shell 登錄
# vim /etc/passwd
// 修改 git 用戶的 shell 為 git-shell,路徑使用 which git-shell 查看
// 找到如下一行
git:x:1001:1001::/home/git:/bin/bash
// 修改成如下
git:x:1001:1001::/home/git:/usr/local/git/bin/git-shell

 重啟sshd服務

service sshd restart

 

三、客戶端開始使用

1、打開git bash

2、生成秘鑰對

  2.1 客戶機執行以下命令將在windows的“用戶目錄/.ssh”下得到秘鑰對

cd ~/.ssh
ssh-keygen -t rsa -C “youremail@example.com” 

 

  2.2上傳公共秘鑰到git服務器有以下2種方式:

  ①復制到git服務器的/home/git/.ssh/authorized_keys文件末尾中;

  ②通過ftp等方式上傳后,執行以下命令:

cat 源秘鑰文件路徑 >> /home/git/.ssh/authorized_keys


2、任意新建一個工作區文件夾

3、執行clone命令(輸入自己的IP地址,端口默認為22,如有不同就加上去)

git clone git@ip:/home/git/yourname.git

 

4、隨便新建個文件

5、提交

cd 項目文件下下
git add .
git commit -m "本次提交的備注"
git push

 

6、服務器端驗證是否上傳成功

cd /home/git/yourName.git/branches
git log

成功信息:

commit 087966c9f3f73f4aee153213213212132132ac191a7 (HEAD -> master) Author: upLoadUserName <yourEmailAddress> Date: Tue Oct 9 08:59:21 2018 +0800

 

【遇到的坑】

1、回到家git clone一下發覺這玩意兒 Permission denied我了!

解決辦法:再次百度鼓搗一趟,原來是的ssh-keygen的時候自定義了密鑰的文件名,保持默認回車下去生成的id_rsa能夠正常使用,ssh -v 服務器IP 一下發現可能客戶端的git默認使用了id_rsa的私鑰,待深入學習git考究(原諒我懶人不看git官網原著,專門看百度出來的博客被坑)

 

2、git push報錯1

$ git push
Enter passphrase for key '/c/Users/PC-name/.ssh/id_rsa':
Enumerating objects: 145, done.
Counting objects: 100% (145/145), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (134/134), done.
fatal: sha1 file '<stdout>' write error: Broken pipe
error: remote unpack failed: unable to create temporary object directory error: failed to push some refs to 'git@youripaddr:survey.git'

緩存區不夠大,搞大它

$ git config http.postBuffer 52428800

 

3、git push報錯2

$ git push
Enter passphrase for key '/c/Users/hp206/.ssh/id_rsa':
Enumerating objects: 145, done.
Counting objects: 100% (145/145), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (134/134), done.
Writing objects: 100% (145/145), 103.16 KiB | 685.00 KiB/s, done.
Total 145 (delta 64), reused 0 (delta 0)
error: remote unpack failed: unable to create temporary object directory
To youripaddr:yourprojectname.git
 ! [remote rejected] master -> master (unpacker error) error: failed to push some refs to 'git@youripaddr:survey.git'

權限的問題,因為不是服務器git用戶創建的目錄
到服務器git目錄下

chown -vR git *

完美!

 

參考文章:

1.搭建私有git服務器進行版本控制
2.向git服務器添加shh公鑰
3.搭建Git服務器遇到的那些坑

 


免責聲明!

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



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