linux ssh免密码登录

从机器machineA登陆到机器machineB,需要免密码。只要在A机器上生成秘钥对,将公钥追加到B机器上的授权文件authorized_keys上即可,分两步:
###1.创建秘钥对

进入machineA的文件夹/root/.ssh/,用rsa生成秘钥
[root@machineA .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
#输入秘钥生成的文件名称,可以直接叫id_rsa
Enter file in which to save the key (/root/.ssh/id_rsa): id_rsa
#输入访问id_rsa的密码,不要输入,直接回车,不然免密码登陆时还要输入(Enter passphrase for key '/root/.ssh/id_rsa')
Enter passphrase (empty for no passphrase): 
#回车
Enter same passphrase again: 
#完成,ll -rt查看已生成文件id_rsa和id_rsa.pub
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
6b:80:9b:a1:d6:0d:0b:dc:f3:0d:ad:a7:fe:da:d8:cf root@Salve4
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
| . . . .         |
|  o * o S        |
|   + X = .       |
|  o = + =        |
| .     B .       |
|     .=o+.E      |
+-----------------+
[root@machineA .ssh]# ll -rt
total 12
-rw-r--r-- 1 root root  781 Feb 15 15:54 known_hosts
-rw-r--r-- 1 root root  393 Feb 15 15:59 id_rsa.pub
-rw------- 1 root root 1743 Feb 15 15:59 id_rsa

###2.合并公钥

#在每台机器上按以上方法操作完成,将所有机器的id公钥追加到machineA机器的~/.ssh/authorized_keys文件中,将authorized_keys文件复制到每台机器的对应目录下,即可完成所有机器间的互信。
查看合并后文件
[root@machineB .ssh]# pwd
/root/.ssh
[root@machineB .ssh]# cat authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAvMbc9fsAGLvXyJQo2xS540FWi8tMJvtMHNTlsSb4Oh14Vjk6iQZxumkEUDbbbrxemlMgxIyRzDuUjrE+1T1N3fxJJTZw94LvogzAXDvcVloBFiHc3/BNszguhs6zTb56hTZJ21tJlr8PVqKSoFtSdECr+FO4tg4QLFEiqseWuGaou1d0WY8yAfMrDXu+10+pYMibI8EswpGOfef1heg04sMJe4/lep1LshHLx2HgzrNW5wpWU0CH65HCjqVs1mWu1Q9dcXhG0RPXYv0IKDGQt/cl74FssIRhIWaYyxw/lKtjA9eFz92KXDJI58l96+wn/z1kh13ZgvZA+3Lo4o85qw== root@machineA
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWnrgtVaDFTtRwm8yRBMzH+fxXdztnJDpYLSGUzkZ+QiDntenRfKWtcLjzC3KmsA8jGPR9rKLeLLHE0IdFz34+n7xNw4bVKcEe8YBp1vbd0xDPkXM1hP5yHHQkHUXF6l1415kTHF8UenxJe4LzCA/+/Os+eIkdtSl3I+tmVFES3POyHLQUcbodooFkE9Yvb9lhbyduk9/meaSh61oxIV0KmbDaa2pP5lktYRiWaBbluZawX564XHz5Lblc6xcCyqNbthx1+5SuDoR2Um6GKzRjeKyqdw61isydCTRNsrwhTcWHWEHE1a4aJ6VC7lTjmcN+Qm0xXFOvUWiYZ/+KJop/ root@machineB
[root@machineB .ssh]# 
免密登录测试
[root@machineA ~]# ssh machineB
Last login: Wed Feb 15 16:31:22 2017 from 172.18.0.150
[root@machineB ~]# 

###5.其他用户免密码登录
提醒:免密码登录只对操作的用户账号生效,其他登录账号并不起作用。
ssh不希望home目录(非root用户所在目录经常是在/home下)和~/.ssh目录对组有写权限。
所以可能会遇到非root用户做免密码登录配置时,经常遇到配置完成但是不生效,可以查看ssh登录过程中的系统安全日志,例如当前用户是HByw_root:

sudo cat /var/log/secure
...
Apr 16 14:19:02 nn1 sshd[10379]: Authentication refused: bad ownership or modes for directory /home/HByw_root
Apr 16 14:19:03 nn1 sshd[10380]: Connection closed by 172.16.0.222
Apr 16 14:19:39 nn1 sudo:     root : TTY=pts/1 ; PWD=/home/HByw_root ; USER=root ; COMMAND=/bin/cat /var/log/secure
Apr 16 13:55:50 db1 sshd[9736]: Authentication refused: bad ownership or modes for file /home/HByw_root/.ssh/authorized_keys
Apr 16 13:55:52 db1 sshd[9737]: Connection closed by ::1
Apr 16 13:55:54 db1 sudo:     root : TTY=pts/28 ; PWD=/home/HByw_root/.ssh ; USER=root ; COMMAND=/bin/cat /var/log/secure
...

日志显示的文件夹和文件所属组或权限不对,可以对照下面提示检查:

1.文件夹和文件权限
sshd为了安全,对属主的目录和文件权限有所要求。
如果权限不对,则ssh的免密码登陆不生效,所以不能一律采取chmod 777的错误做法。
用户目录权限为755或者700,不能是77x。
.ssh目录权限一般为755或者700。
id_rsa.pub 及authorized_keys权限一般为644
id_rsa权限必须为600
例如当前登录用户是root用户组但非root用户
#查看当前用户
[root@hadoop002 .ssh]# cat  /etc/passwd
HByw_root:x:0:0::/home/HByw_root:/bin/bash
[root@hadoop002 .ssh]# groups HByw_root
HByw_root : root
#重新指定权限
#用户目录
[root@hadoop002 ~]# cd /home/
[root@hadoop002 home]# chmod 700 HByw_root/
[root@hadoop002 home]# ll
total 24
drwx------ 5 root   root    4096 Apr 16 13:54 HByw_root
#.ssh目录
[root@hadoop003 ~]# cd /home/HByw_root/
[root@hadoop003 ~]# chmod 700 .ssh/
[root@hadoop003 ~]# ll -a
drwx------  2 root root 4096 Apr 16 14:52 .ssh
#进入默认生成目录/root下查看id_rsa和id_rsa.pub文件
[root@hadoop002 .ssh]# cd /root/.ssh/
[root@hadoop002 .ssh]# chmod 600 id_rsa
[root@hadoop002 .ssh]# chmod 644 id_rsa.pub 
[root@hadoop002 .ssh]# ll
-rw------- 1 root root 1675 Apr 16 14:16 id_rsa
-rw-r--r-- 1 root root  396 Apr 16 14:16 id_rsa.pub
#重启ssh服务
sudo service sshd restart
#重新从步骤1、2
#完成后,可以先对本机做免密码登录测试
即将把密钥追加到本机的 ~/.ssh/authorized_key
测试免密码登录,成功!
[root@hadoop002 home]# ssh HByw_root@hadoop002
Last login: Mon Apr 16 14:56:24 2018 from 172.16.0.166
[root@hadoop002 ~]# 

以上!

你可能感兴趣的:(linux,linux,免密钥登陆,非root用户)