2016年12月28日 星期三

CentOS 架設 Git Server

1.安裝作業系統
2.建立 git 帳號
   Require a password to use this account 取消勾選
3.安裝SSH

   yum -y install openssh*
   service sshd start
   systemctl enable sshd.service
   systemctl status sshd.service

  測試ssh
  client:~ $ ssh root@remotehost

   產生金鑰:(Client)
 
   ssh-keygen -t rsa

   上傳 id_rsa.pub
 
   scp ~/.ssh/id_rsa.pub root@remotehost:/tmp

   接著設定伺服器上的SSH環境,連線到Server執行以下的指令:

   cd ~
   mkdir .ssh
   cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
   chmod -R go= ~/.ssh

   PS: 1.下面兩個目錄的所有權必須是user,所屬組也應該是user,權限必須為700
             /home/user
             /home/user/.ssh
          2.下面金鑰文件的所有權必須是user,所屬組也應該是user,權限必須為644
             /home/user/.ssh/authorized_keys
          3.下面私鑰文件的所有權必須是user,所屬組也應該是user,權限必須為600
             /home/user/.ssh/id_rsa
          4.關閉 SELinux解決問題:
             暫時關閉(重啟後恢復)
             setenforce 0
             永久關閉(需要重啟)
             vi /etc/selinux/config
             SELINUX=disabled
           

         

   要注意以上的指令是與帳號對應的,如果要使用root來連線,則要在Server上以root登入後執行。完成了之後就可以關閉SSH的密碼登入,只允許使用金鑰來做登入。設定的方式是修改 /etc/ssh/sshd_config檔案的下述兩個參數:

 RSAAuthentication yes
 PubkeyAuthentication yes
 PasswordAuthentication no


設定Git環境
如果在安裝CentOS時沒有選擇安裝Development Tools的套件組合,則要先執行以下的指令,以便安裝Git套件:

  yum -y install git

安裝好了Git套件,可以先把安裝CentOS就建立好的git帳號對應的shell更改為git-shell,使連線進來的Session只能使用Git相關指令,減少資安的風險。一般git-shell都是在/usr/bin下,如果不確定可以先使用 which git-shell指令來查看實際安裝的路徑。

更改shell的方式是修改 /etc/passwd檔案,git的資訊應該位在檔案的最後一行,修改後的內容如下:

 git:x:1000:1000::/home/git:/usr/bin/git-shell

接下來要設定讓Client端可以使用git帳號來存取Server上的Repository,跟之前的步驟一樣,
把上傳到tmp下的金鑰檔加到git帳號所屬的authorized_keys檔案裡:

cd/home/git
mkdir .ssh
cat /tmp/id_rsa.pub >> .ssh/authorized_keys
chmod -R go= .ssh

再來就依據實際的需求建立一到多個空的Repository,指令示範如下:
cd /srv/git
mkdir project.git
cd project.git
git --bare init
chown -hR git:git /srv/git/project.git

回到 Client端,建立本地的Repository,但如果是第一次使用還沒進行初次的設定作業,要先執行以下的指令:

  git config --global user.email "you@example.com"
  git config --global user.name "You Name"

 接下來才是執行建立 Repository的指令,其中[path]是Repository要建立的路徑:

 cd [path]
mkdir myproject
cd myproject
git init

都準備好了之後,就可以做第一次的commit並push到Server上:

git add .
git commit -m 'initial commit'
git remote add origin git@remotehost:/srv/git/project.git
git push origin master

如果一切正常,就完成了在CentOS上架設 Git Server 的工作。

參考網址: http://wznote.blogspot.tw/2014/09/instal-git-server-in-centos.html
                  http://zqscm.qiniucdn.com/data/20130903005919/index.html
               



 

沒有留言:

張貼留言