CentOS 7 SSH 免密登录的方法

来源:互联网 发布:农村淘宝一单赚多少钱 编辑:程序博客网 时间:2024/06/06 02:34

先决条件

3 台 CentOS 7

HOSTNAMEIPROLEserver110.8.26.197Masterserver210.8.26.196Slave1server310.8.26.195Slave2

步骤

1. 用 root 用户登录。每台服务器都生成公钥,再合并到 authorized_keys。

2. CentOS 默认没有启动 ssh 无密登录,去掉 /etc/ssh/sshd_config 其中 2 行的注释,每台服务器都要设置。

RSAAuthentication yesPubkeyAuthentication yes

3. 每台服务器下都输入命令 ssh-keygen -t rsa,生成 key,一律不输入密码,直接回车,/root 就会生成 .ssh文件夹。

4. 在 Master 服务器下,合并公钥到 authorized_keys 文件,进入 /root/.ssh 目录,通过 SSH 命令合并.

# cat id_rsa.pub>> authorized_keys# ssh root@10.8.26.196 cat ~/.ssh/id_rsa.pub>> authorized_keys# ssh root@10.8.26.195 cat ~/.ssh/id_rsa.pub>> authorized_keys

5. 把 Master 服务器的 authorized_keysknown_hosts 复制到 Slave 服务器的`/root/.ssh 目录

# scp authorized_keys root@server2:/root/.ssh/# scp authorized_keys root@server3:/root/.ssh/# scp known_hosts root@server2:/root/.ssh/# scp known_hosts root@server3:/root/.ssh/

6. 完成,ssh root@10.8.26.196ssh root@10.8.26.195 就不需要输入密码了。

7、如仍提示要输入密码,可能 是.ssh 目录及其下文件权限问题:

出现下面命令

username@localhost ~ $ ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is 6e:6b:0f:2a:b8:91:3f:c8:f0:39:e4:df:b4:d8:16:6b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost (::1)' (RSA) to the list of known hosts.
username@localhost's password: 


仍然让我输入密码,这时应该修改一下.ssh的用户和权限
  1. chown username: /home/username/.ssh  
  2. chown username: /home/username/.ssh/*  
  3. chmod 700 /home/username/.ssh  
  4. chmod 600 /home/username/.ssh/* 


原创粉丝点击