如何在 CentOS / RHEL 上设置 SSH 免密码登录

前提配置:使用root登录修改配置文件:/etc/ssh/sshd_config,将其中三行的注释去掉,如下:

然后重启ssh服务:service sshd restart。最后退出root,以下所有操作都在hadoop用户下进行

主机信息如下:

如上图,当前登录用户为hadoop,主机名为slave.hadoop,与master.hadoop主机的网络是通畅的。

当前未配置RSA校验的情况下,用ssh连接主机是需要输入密码的,如下:

如上图,执行ssh master.hadoop后会停留在下一行等待使用者输入master.hadoop主机hadoop用户的登录密码。

为避免此情况发生,进行如下操作以让主机在ssh连接时自动验证后登录。

以下步骤在 CentOS 5/6/7、RHEL 5/6/7 和 Oracle Linux 6/7 上测试通过。

节点1 : 192.168.0.9 节点2 : 192.168.l.10

步骤1 :

测试节点1到节点2的连接和访问:

 
 
  1. [root@node1 ~]# ssh root@192.168.0.10
  2. The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.
  3. RSA key fingerprint is 6d:8f:63:9b:3b:63:e1:72:b3:06:a4:e4:f4:37:21:42.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added '192.168.0.10' (RSA) to the list of known hosts.
  6. root@192.168.0.10's password:
  7. Last login: Thu Dec 10 22:04:55 2015 from 192.168.0.1
  8. [root@node2 ~]#
步骤二:

使用 ssh-key-gen 命令生成公钥和私钥,这里要注意的是可以对私钥进行加密保护以增强安全性。

 
 
  1. [root@node1 ~]# ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. b4:51:7e:1e:52:61:cd:fb:b2:98:4b:ad:a1:8b:31:6d root@node1.ehowstuff.local
  10. The key's randomart image is:
  11. +--[ RSA 2048]----+
  12. | . ++ |
  13. | o o o |
  14. | o o o . |
  15. | . o + .. |
  16. | S . . |
  17. | . .. .|
  18. | o E oo.o |
  19. | = ooo. |
  20. | . o.o. |
  21. +-----------------+
步骤三:

用 ssh-copy-id 命令将公钥复制或上传到远程主机,并将身份标识文件追加到节点2的 ~/.ssh/authorized_keys 中:

 
 
  1. [root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.10
  2. root@192.168.0.10's password:
  3. Now try logging into the machine, with "ssh '192.168.0.10'", and check in:
  4. .ssh/authorized_keys
  5. to make sure we haven't added extra keys that you weren't expecting.
步骤四:

验证免密码 SSH 登录节点2:

 
 
  1. [root@node1 ~]# ssh root@192.168.0.10
  2. Last login: Sun Dec 13 14:03:20 2015 from www.ehowstuff.local

我希望这篇文章能帮助到你,为你提供 SSH 免密码登录 CentOS / RHEL 的基本认知和快速指南。


如果失败:

[root@CentOS02 .ssh]# chmod 700 ~/.ssh
[root@CentOS02 .ssh]# chmod 600 ~/.ssh/authorized_keys
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值