Git服務器搭建

  • root用戶的終端提示符#在代碼塊中會影響顯示效果,暫用$代替
  • 使用虛擬機能夠在一開始生成快照,而後後面就能夠重複進行這個實驗

環境參數:html

  • 虛擬機 VMware Workstation
  • XShell 6
  • centos7
  • git bash
  • win10

接着開啓虛擬機,使用XShell以root用戶進行遠程登陸。git

修改鏡像文件

若是是在阿里雲上安裝git服務器則不用修改鏡像文件
  1. 先備份原鏡像文件,以便出錯的時候方便恢復segmentfault

    root$ cp /etc/yum.repos.d/CentOS-Base.repo{,.bak}
  2. 下載新的CentOS-Base.repo/etc/yum.repos.d/centos

    root$ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  3. 運行yum makecache 生成緩存緩存

    root$ yum makecache

注:其餘版本能夠查看http://mirrors.aliyun.com/repo/bash

安裝git

root$ yum install git-core

接下來建立一個git用戶組和用戶服務器

root$ groupadd git
root$ useradd git -g git

設置git用戶密碼,而後切換到git用戶進行操做hexo

root$ passwd git
root$ su - git

初始化Git倉庫

首先咱們選定一個目錄做爲Git倉庫,假定是/home/git/gitrepo/git-test.git,在/home/git/目錄下輸入命令:app

git$ cd ~
git$ mkdir gitrepo
git$ cd gitrepo
git$ git init --bare git-test.git
Initialized empty Git repository in /home/git/gitrepo/git-test.git/

克隆倉庫

git$ git clone git@10.13.16.47:/home/git/gitrepo/git-test.git
Cloning into 'git-test'...
git@10.13.16.47's password:
warning: You appear to have cloned an empty repository.

到此git服務器已經搭建完畢,並且能夠在客戶端克隆代碼,可是每次都要輸入密碼,接下來就是解決這個問題。ssh

建立證書登陸

在git家目錄下創建.ssh 目錄以及authorized_keys 文件來管理全部用戶的 SSH 公鑰

git$ cd ~
git$ mkdir .ssh
git$ touch .ssh/authorized_keys

win10某個文件夾下右鍵後點擊git bash here菜單,而後將公鑰文件上傳到git用戶家目錄下(生成公鑰的步驟省略了...)

scp ~/.ssh/id_rsa.pub root@10.13.16.47:/home/git

id_rsa.pub文件內容添加到authorized_keys文件末尾

git$ cd ~
git$ cat ./id_rsa.pub >> .ssh/authorized_keys

而後在git bash的終端進行免密登陸測試

ssh -v git@10.13.16.47

很不幸的是,終端提示仍是要求用戶輸入密碼!

解決辦法:

使用root用戶登陸,修改一下git用戶家目錄下.ssh文件夾和.ssh/authorized_keys文件的權限便可:

root$ cd /home/git/
root$ chmod 755 .ssh
root$ chmod 644 .ssh/authorized_keys

再次在git bash終端執行ssh -v git@10.13.16.47命令,免密登陸成功!

最後將遠程倉庫代碼克隆到本地且無需輸入密碼,效果圖以下:
免密克隆遠程倉庫代碼


參考連接:

  1. Centos修改鏡像爲國內的阿里雲源
  2. Git 服務器搭建
  3. 在我的服務器上搭建git服務,建立屬於本身的私人倉庫
  4. 安裝 Git
相關文章
相關標籤/搜索