这篇文章主要介绍了 Linux 远程登录 SSH 免密码配置方法, 需要的朋友可以参考下 ( http://www.0834-3659999.com/ )
一, 情景
公司刚上几台 Linux, 现在要把主机之间都能远程 SSH 免密码登陆.
二, 原理
很简单, 使用 SSH-keygen 在主机 A 上生成 private 和 public 密钥, 将生成的 public 密钥拷贝到远程机器主机 B 上后, 就可以使用 SSH 命令无需密码登录到另外一台机器主机 B 上.
三, 步骤
主机 A:
1, 生成公钥和私钥文件 id_rsa 和 id_rsa.pub (敲 3 下回车即可)
[[email protected] ~]# SSH-keygen -t rsa
Generating public/private rsa key pair.
- Enter file in which to save the key (/root/.SSH/id_rsa):
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /root/.SSH/id_rsa.
- Your public key has been saved in /root/.SSH/id_rsa.pub.
- The key fingerprint is:
- 67:da:0d:79:e0:d6:2b:cd:7d:22:af:51:7e:9c:75:fe [email protected]
- The key's randomart image is:
- +--[ RSA 2048]----+
- | |
- | |
- | . |
- | . + |
- | S B o . o|
- | * * = o+|
- | . o B +.=|
- | . + +.|
- | ... E|
- +-----------------+
2,SSH-cop-id 命令会将指定的公钥文件复制到远程计算机
- [[email protected] ~]# SSH-copy-id -i ~/.SSH/id_rsa.pub [email protected]
- The authenticity of host '10.1.250.166 (10.1.250.166)' can't be established.
- RSA key fingerprint is c8:9d:6d:92:55:77:3d:3e:af:f5:cb:18:80:5a:47:93.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '10.1.250.166' (RSA) to the list of known hosts.
- reverse mapping checking getaddrinfo for bogon [10.1.250.166] failed - POSSIBLE BREAK-IN ATTEMPT!
[email protected]'s password: < 主机 B 的登陆密码 >
- Now try logging into the machine, with "ssh'[email protected]'", and check in:
- .SSH/authorized_keys
- to make sure we haven't added extra keys that you weren't expecting.
3, 然后 SSH 登陆主机 B 验证是否需要密码
- [[email protected] ~]# SSH 10.1.250.166
- reverse mapping checking getaddrinfo for bogon [10.1.250.166] failed - POSSIBLE BREAK-IN ATTEMPT!
- Last login: Wed Oct 21 10:05:39 2015 from 10.1.250.141
- [[email protected] ~]#
4, 登陆成功后, 我们需要在主机 B 也做下以上 3 步, 这样就可以相互免密码 SSH 登陆.(如果有多台主机, 每台主机都做下相同操作, 以方便以后管理).
5, 权限问题
建议用其他用户做 SSH 登陆的话, 调整目录文件权限
设置 authorized_keys 权限
chmod 644 authorized_keys
设置. SSH 目录权限
chmod 700 -R .SSH
6, 要保证. SSH 和 authorized_keys 都只有用户自己有写权限. 否则验证无效.
来源: http://www.bubuko.com/infodetail-3036155.html