Linux配置好公钥后无法登陆

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux配置好公钥后无法登陆相关的知识,希望对你有一定的参考价值。

前提:

  1. /etc/ssh/sshd_config 配置正确;

  2. 在远程服务器上添加了公钥;

  3. 远程服务器ssh端口正确并且是默认的22端口;

  4. 使用用户名与密码登陆正常;

问题:

  1. 无法使用公钥登陆,每次都要求输入用户名与密码;

  2. 普通用户可以公钥登陆,root不行,但sshd_config中是允许root登陆的且各种配置都正确;

登陆时提示如: 
[email protected]:~$ ssh egit-root 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)

原因

问题1可能原因:文件及文件夹权限问题

权限要求如下: 
~/.ssh/authorized_keys: 此文件要求权限为600 
~/.ssh: 此文件夹要求权限为700 (我忽略了此权限,导致一台服务器始终无法用公钥登陆,设置后正常) 
为了避免权限问题,建议尽量不要手动添加,采用命令添加,命令如下:

# 生成密钥(遇到提示可以一路回车,具体作用请查看帮助)ssh-keygen -t rsa# 上传公钥ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

问题2可能原因:selinux开启导致

selinux开启时,普通用户可以登陆,但root不允许登陆 
解决方案:关闭selinux

#!/bin/bash# 临时关闭selinux(0:关闭;1:开启)setenforce 0# 永久关闭selinux(disabled:关闭;enforcing:开启)vi /etc/selinux/config  
SELINUX=disabled123456

测试

在正常的登陆语句后面加上-v即可查看登陆时与服务器交换的详情,-vv更详细 
[email protected]:~$ ssh egit-root -v 
OpenSSH_6.9p1, LibreSSL 2.1.8 
debug1: Reading configuration data /Users/dengqingyong/.ssh/config 
debug1: /Users/dengqingyong/.ssh/config line 39: Applying options for egit-root 
debug1: Reading configuration data /etc/ssh/ssh_config 
debug1: /etc/ssh/ssh_config line 21: Applying options for * 
debug1: Executing proxy command: exec ssh -q -W 192.168.1.62:22 gwe 
debug1: permanently_drop_suid: 502 
debug1: identity file /Users/dengqingyong/.ssh/id_rsa type 1 
debug1: key_load_public: No such file or directory 
debug1: identity file /Users/dengqingyong/.ssh/id_rsa-cert type -1 
debug1: Enabling compatibility mode for protocol 2.0 
debug1: Local version string SSH-2.0-OpenSSH_6.9 
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3 
debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000 
debug1: Authenticating to 192.168.1.62:22 as ‘root’ 
debug1: SSH2_MSG_KEXINIT sent 
debug1: SSH2_MSG_KEXINIT received 
debug1: kex: server->client aes128-ctr [email protected] none 
debug1: kex: client->server aes128-ctr [email protected] none 
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sent 
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP 
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent 
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY 
debug1: Server host key: ssh-rsa SHA256:npT3KAq+nbSjgRVKutJre9aq8GcWPq1cnykDN8ImkCY 
debug1: Host ‘192.168.1.62’ is known and matches the RSA host key. 
debug1: Found key in /Users/dengqingyong/.ssh/known_hosts:80 
debug1: SSH2_MSG_NEWKEYS sent 
debug1: expecting SSH2_MSG_NEWKEYS 
debug1: SSH2_MSG_NEWKEYS received 
debug1: SSH2_MSG_SERVICE_REQUEST sent 
debug1: SSH2_MSG_SERVICE_ACCEPT received 
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password 
debug1: Next authentication method: publickey 
debug1: Offering RSA public key: /Users/dengqingyong/.ssh/id_rsa 
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password 
debug1: Offering RSA public key: 
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password 
debug1: No more authentication methods to try. 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). 
[email protected]


以上是关于Linux配置好公钥后无法登陆的主要内容,如果未能解决你的问题,请参考以下文章

如何在SecureCRT上使用公钥登陆Linux服务器

如何在SecureCRT上使用公钥登陆Linux服务器

Mac终端配置免密码登陆云服务器

Linux免密远程登陆

如何在SecureCRT上使用公钥登陆Linux服务器

windows 系统下利用SecureCRTxshell 生成ssh登陆linux的公钥