问题:
有时候,在使用SSH登录到服务器时,明明密码正确,但是登录时候却报被拒绝,这是什么原因呢?
解决思路:
1、此时我们可以查看Linux服务器的安全日志文件/var/log/secure,若无异常可以看到如下类似报错异常
Oct 6 21:44:47 hostname sshd[265919]: input_userauth_request: invalid user admin [preauth]
Oct 6 21:44:47 hostname sshd[265919]: Failed password for invalid user admin from 10.168.100.250 port 63780 ssh2
Oct 6 21:44:56 hostname sshd[265919]: Failed password for invalid user admin from 10.168.100.250 port 63780 ssh2
Oct 6 21:44:58 hostname sshd[265919]: error: Received disconnect from 10.168.100.250 port 63780:0: [preauth]
我们可以注意到SSH登录使用的admin被提示为invalid user【无效用户】
2、进一步验证登录使用的admin用户是否存在
#id admin
经过验证admin用户确实是存在的
3、检查/etc/ssh/sshd_config配置文件
经过检查发现sshd_config启用了仅允许某些用户使用ssh,但是admin用户却没有被囊括进去,其允许用户的字段信息如下
:~ # grep -i allowusers /etc/ssh/sshd_config
AllowUsers Root root test2
:~ #
4、将仅允许用户使用SSH后面追加admin用户
# grep -i allowusers /etc/ssh/sshd_config
AllowUsers Root root test2 admin
尝试再次SSH登录,可以看到此时SSH可以成功连接使用
至此问题得到解决。
SSH其他拓展知识:
1、启用密码认证,取消如下行的注释符
#PasswordAuthentication yes
2、启用root登录功能,取消如下行的注释符
#PermitRootLogin yes
3、启用SSH密钥对登录,取消如下行的注释符
#PubkeyAuthentication yes
#AuthorizeKeysFile .ssh/authorized_keys
4、如何在Linux中允许用户使用SSH
#echo "AllowUsers user3" >> /etc/ssh/sshd_config
若有多个用户,用户之间使用空格分隔
5、如何在Linux中阻止用户使用SSH
#echo "DenyUsers user1" >> /etc/ssh/sshd_config
若有多个用户,用户之间使用空格分隔
6、如何在Linux中允许组使用SSH
#echo "AllowGroups 组名称" >> /etc/ssh/sshd_config
若有多个组,组之间使用空格分隔
7、禁止组使用SSH
#echo "DenyGroups 组名称" >>/etc/ssh/sshd_config
若有多个组,组之间使用空格分隔