- root用戶的終端提示符#在代碼塊中會影響顯示效果,暫用$代替
- 使用虛擬機能夠在一開始生成快照,而後後面就能夠重複進行這個實驗
環境參數:html
接着開啓虛擬機,使用XShell以root用戶進行遠程登陸。git
若是是在阿里雲上安裝git服務器則不用修改鏡像文件
先備份原鏡像文件,以便出錯的時候方便恢復segmentfault
root$ cp /etc/yum.repos.d/CentOS-Base.repo{,.bak}
下載新的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
運行yum makecache
生成緩存緩存
root$ yum makecache
注:其餘版本能夠查看http://mirrors.aliyun.com/repo/bash
root$ yum install git-core
接下來建立一個git用戶組和用戶服務器
root$ groupadd git root$ useradd git -g git
設置git用戶密碼,而後切換到git用戶進行操做hexo
root$ passwd git root$ su - 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
命令,免密登陸成功!
最後將遠程倉庫代碼克隆到本地且無需輸入密碼,效果圖以下:
參考連接: