- 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
命令,免密登陆成功!
最后将远程仓库代码克隆到本地且无需输入密码,效果图以下:
参考连接: