CentOS6.9 SSH免密码登录配置

参考:https://blog.csdn.net/u010180815/article/details/78323596

http://www.jb51.net/article/121180.htm

我用的CentOS是6.9 X64版本,选的是mini-desktop,没有遇到需要配置ssh_config文件,

有两台服务器IP地址分别为:171,172:

首先修改主机名称:

sudo vim /etc/sysconfig/network
#修改171 为 master
NETWORKING=yes
HOSTNAME=master
NTPSERVERARGS=iburst

#修改172 为 backup
NETWORKING=yes
HOSTNAME=backup
NTPSERVERARGS=iburst
sudo vim /etc/hosts
#修改171 为 master
127.0.0.1   localhost master localhost4 localhost4.localdomain4
::1         localhost master localhost6 localhost6.localdomain6
#修改172 为 backup
127.0.0.1   localhost backup localhost4 localhost4.localdomain4
::1         localhost backup localhost6 localhost6.localdomain6

分别生成密钥:

ssh-keygen -t rsa
#然后一直回车,在用户目录下生成 ~/.ssh 目录
sudo chmod 700 ~/.ssh
sudo chmod 600 .ssh/authorized_keys

注意:必须修改权限,否则即使添加到要免密码登录的系统中的~/.ssh/authorized_keys 文件中,也是无法免密码登录。
可以通过查看系统的日志文件

tail /var/log/secure -n 20

发现问题的所在:Authentication refused: bad ownership or modes for file

从字面上可以看出是目录的属主和权限配置不当,查找资料得知:SSH不希望home目录和~/.ssh目录对组有写权限

分别执行复制公钥:

#171
ssh-copy-id -i .ssh/id_rsa.pub [email protected]

#172
ssh-copy-id -i .ssh/id_rsa.pub [email protected]

输出下面信息表示配置成功:

[taiji@backup ~]$ ssh-copy-id -i .ssh/id_rsa.pub [email protected]
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.


[taiji@backup ~]$ ssh [email protected]
Last login: Wed Apr 25 17:54:18 2018 from 172.16.19.172
[taiji@master ~]$ ls
Desktop  Documents  Downloads  keepalived-1.4.3  Music  Pictures  Public  Templates  Videos
[taiji@master ~]$ exit

你可能感兴趣的:(转载)