搭建Git服務器需要准備一台運行Linux的機器,本文以Centos6.8純凈版系統為例搭建自己的Git服務。
准備工作:以root用戶登陸自己的Linux服務器。
第一步安裝依賴庫
[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[root@localhost ~]# yum install gcc perl-ExtUtils-MakeMaker
第二步卸載舊版git
加入原先有用yum安裝過git,則需要先卸載一下
[root@localhost ~]# yum remove git
第三步下載源碼
下載git-2.10.0.tar.gz 到 /usr/local/src
(查找git版本可以到https://www.kernel.org/pub/software/scm/git/下查看git的版本號自行選擇下載)
查看版本方法:
[root@iZbp1ap7v4yegqdgzrh7cuZ ~]# wget -v https://www.kernel.org/pub/software/scm/git/
[root@iZbp1ap7v4yegqdgzrh7cuZ ~]# vi index.html
復制想下載的版本 --> Esc --> :q! --> 回車!
這里我選擇下載git-2.10.0.tar.gz
[root@localhost ~]# cd /usr/local/src
[root@localhost ~]# wget https://www.kernel.org/pub/software/scm/git/git-2.10.0.tar.gz
第四步解壓、編譯和安裝
[root@localhost ~]# tar -zvxf git-2.10.0.tar.gz
[root@localhost ~]# cd git-2.10.0
[root@localhost ~]# make prefix=/usr/local/git all
[root@localhost ~]# make prefix=/usr/local/git install
第五步將git目錄加入PATH
[root@localhost ~]# echo 'export PATH=$PATH:/usr/local/git/bin' >> /etc/bashrc
[root@localhost ~]# source /etc/bashrc
安裝成功后就可以查看到git版本了。
[root@localhost ~]# git --version
git version 2.10.0
第六步創建git賬號並設置密碼
[root@localhost ~]# useradd -m git
[root@localhost ~]# passwd git
Changing password for user git.
New password:
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
第七步創建git倉庫並初始化
[root@localhost ~]# mkdir -p /home/git/repositories/test.git
[root@localhost ~]# cd /home/git/repositories/test.git
[root@localhost test.git]# git --bare init
Initialized empty Git repository in /home/git/repositories/test.git/
第八步給git倉庫目錄設置用戶和用戶組並設置權限
[root@localhost test.git]# chown -R git:git /home/git/repositories
[root@localhost test.git]# chmod 755 /home/git/repositories
第九步限制git賬號的ssh連接
查找git-shell所在目錄
[root@localhost ~]# whereis git-shell
git-shell: /usr/src/git-2.10.0/git-shell
編輯passwd文件
[root@localhost ~]# vi /etc/passwd
找到這一行
git:x:500:500::/home/git:/bin/bash
將最后的/bin/bash改為:git-shell的目錄 /usr/src/git-2.10.0/git-shell 如下:
git:x:500:500::/home/git:/usr/src/git-2.10.0/git-shell
Esc --> :wq! --> 回車!
完成搭建,去克隆提交試試吧!
clone地址:
ssh://git@服務器ip地址:端口/home/git/repositories/test.git
附加:以后每次新建倉庫時,只需執行上面第七、八步即可!