liunx SSH密钥 免密码登录

第一步:生成密钥

ssh-keygen -t rsa
执行 ssh-keygen 时,才会在我的home目录底下的 .ssh/ 这个目录里面产生所需要的两把 Keys ,分别是私钥 (id_rsa) 与公钥 (id_rsa.pub)。

[root@localhost ~]# ssh-keygen -t rsa
//先运行这个命令
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //直接回车
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase)://输入密钥密码
Enter same passphrase again:  //重复密钥密码
Your identification has been saved in /root/.ssh/id_rsa. //提示公钥和私钥已经存放在/root/.ssh/目录
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
16:71:6b:47:c3:8c:f4:c5:8f:1f:07:e2:15:a9:7b:0f [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|        ..o+o.oo |
|         o.++o=  |
|        . o.o+ + |
|         o .o ..o|
|        S    . .o|
|       .    . E .|
|             . o |
|                .|
|                 |
+-----------------+

第二步:

[root@localhost ~]# mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
[root@localhost ~]# chmod 600 /root/.ssh/authorized_keys

第三步:修改ssh配置

修改/etc/ssh/sshd_config 文件,将RSAAuthentication 和 PubkeyAuthentication 后面的值都改成yes ,保存。

vim /etc/ssh/sshd_config
...
RSAAuthentication yes
PubkeyAuthentication yes
...

第四步:重启ssh服务

service sshd restart

第五步:测试

  1. 填写主机和端口号
    填写主机和端口号

  2. 填写用户名 与 用户密钥(id_rsa)
    填写用户名 与 用户密钥(id_rsa)

  3. 保存 测试连接

第六步: 安全配置

1、 修改/etc/ssh/sshd_config 文件

StrictModes no
#StrictModes no #修改为no,默认为yes.如果不修改用key登陆是出现server refused our key(如果StrictModes为yes必需保证存放公钥的文件夹的拥有与登陆用户名是相同的.“StrictModes”设置ssh在接收登录请求之前是否检查用户家目录和rhosts文件的权限和所有权。这通常是必要的,因为新手经常会把自己的目录和文件设成任何人都有写权限。)(来源http://matt-u.iteye.com/blog/851158)

#有了证书登录了,就禁用密码登录吧,安全要紧
PasswordAuthentication no

#建议改变默认ssh端口
Prot 22

#禁用root账户登录,非必要,但为了安全性,请配置
PermitRootLogin no

2、 重启sshd服务

service sshd restart
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章