简书排版
http://www.jianshu.com/p/c4c4d1975891
之前写《搭建基于SSH的Git服务器》
,有大致讲过SSH远程登陆
现在完整的小结一下
首先是SSH Key的生成
ssh-keygen -t rsa -C "byhook@163.com"
根据提示输入密码
Generating public/private rsa key pair.
Enter file in which to save the key (/home/byhook/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/byhook/.ssh/id_rsa.
Your public key has been saved in /home/byhook/.ssh/id_rsa.pub.
The key fingerprint is:
f5:9a:53:d3:97:d1:61:11:81:f6:9a:53:e1:c2:ab:62 byhook@163.com
The key's randomart image is:
+--[ RSA 2048]----+
| .*+|
| o..o|
| . o o.o|
| . . + +o|
| S + B..|
| + * . |
| + . . |
| E o |
| . . |
+-----------------+
生成的id_rsa文件是私有密钥
id_rsa.pub是公开的密钥
查看公钥的内容
cat ~/.ssh/id_rsa.pub
不建议用root用户来开启SSH登陆
建议新创建一个用户
#远程登陆
ssh root@192.168.1.104
#创建新用户
useradd andy
#配置密码
passwd andy
本机现在可以上传公钥到服务器
#本机复制公钥到服务器
scp -r ~/.ssh/id_rsa.pub andy@192.168.1.104:~/
#远程登陆
ssh andy@192.168.1.104
用户andy配置授权
cd ~
mkdir .ssh
cd .ssh
touch authorized_keys
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
rm ~/id_rsa.pub
最后加上权限
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
开启授权登陆
su
yum install vim
vim /etc/ssh/sshd_config
将如下三行注释去掉
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
如图所示
不建议使用root用户开启远程SSH登陆,如果实在想开启可以去掉
#PermitRootLogin yes
的注释
最后重启服务
service sshd restart
最后你会发现,已经不用输入密码了
ssh andy@192.168.1.104