配置公钥登录提示还是输入密码

一、知识点补充:

在客户端来看,SSH提供两种级别的安全验证

第一种级别(基于密码的安全验证),知道帐号和密码,就可以登录到远程主机,并且所有传输的数据都会被加密。

第二种级别(基于密钥的安全验证),需要依靠密钥,也就是你必须为自己创建一对密钥,并把公有密钥放在需要访问的服务器上。

二、问题描述

集群一台主节点master,用到第二种级别,运行的用户是hadoop2,现在配置了公钥登录后,ssh连接还是显示输入密码。

三、问题呈现

1、生成免密钥对

[[email protected] ~]$ ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
Generating public/private rsa key pair.
Created directory ‘/home/hadoop2/.ssh‘.
Your identification has been saved in /home/hadoop2/.ssh/id_rsa.
Your public key has been saved in /home/hadoop2/.ssh/id_rsa.pub.
The key fingerprint is:
79:f8:aa:87:bd:de:22:bd:30:89:69:fa:5b:73:5b:bf [email protected]
The key‘s randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|         o       |
|        S .      |
|     o . o       |
|    + *+. o      |
|   o .o=== .     |
|  ..o..=*+. E.   |
+-----------------+

2、复制公钥,10.1.9.214也就是本机master

[[email protected] root]$ ssh-copy-id -i ~/.ssh/id_rsa.pub 10.1.9.214
[email protected]‘s password: 
Now try logging into the machine, with "ssh ‘10.1.9.214‘", and check in:
  .ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.

3、验证,还是提示密码输入

[[email protected] root]$ ssh master
The authenticity of host ‘master (10.1.9.214)‘ can‘t be established.
RSA key fingerprint is f3:80:84:4f:7d:7c:97:74:57:09:35:8e:aa:b9:f2:06.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘master‘ (RSA) to the list of known hosts.
[email protected]‘s password:

四、排查

1、调出debug模式

[[email protected] ~]$ ssh -v master
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to master [10.1.9.214] port 22.
debug1: Connection established.
debug1: identity file /home/hadoop2/.ssh/identity type -1
debug1: identity file /home/hadoop2/.ssh/identity-cert type -1
debug1: identity file /home/hadoop2/.ssh/id_rsa type 1
debug1: identity file /home/hadoop2/.ssh/id_rsa-cert type -1
debug1: identity file /home/hadoop2/.ssh/id_dsa type -1
debug1: identity file /home/hadoop2/.ssh/id_dsa-cert type -1
debug1: identity file /home/hadoop2/.ssh/id_ecdsa type -1
debug1: identity file /home/hadoop2/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host ‘master‘ is known and matches the RSA host key.
debug1: Found key in /home/hadoop2/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
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,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/hadoop2/.ssh/identity
debug1: Offering public key: /home/hadoop2/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/hadoop2/.ssh/id_dsa
debug1: Trying private key: /home/hadoop2/.ssh/id_ecdsa
debug1: Next authentication method: password
[email protected]‘s password: 
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = zh_CN.UTF-8
Last login: Fri Nov 18 16:14:57 2016 from 10.1.9.214

2、切换root,查看sshd_config配置文件,没发现异常

#       $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Logging
# obsoletes QuietMode and FascistLogging
# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
AuthorizedKeysCommand none
AuthorizedKeysCommandRunAs nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don‘t trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don‘t read the user‘s ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to ‘yes‘ to enable PAM authentication, account processing, 
# and session processing. If this is enabled, PAM authentication will 
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to ‘no‘.
#UsePAM no
UsePAM yes

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none

# no default banner path
#Banner none

# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       ForceCommand cvs server

5、查看权限,也正常

[[email protected] ~]$ ls ~/.ssh/ -ld
drwx------ 2 hadoop2 hadoop2 4096 11月 18 16:13 /home/hadoop2/.ssh/
[[email protected] ~]$ ls ~/.ssh/ -l
总用量 16
-rw------- 1 hadoop2 hadoop2  396 11月 18 16:13 authorized_keys
-rw------- 1 hadoop2 hadoop2 1675 11月 18 16:12 id_rsa
-rw-r--r-- 1 hadoop2 hadoop2  396 11月 18 16:12 id_rsa.pub
-rw-r--r-- 1 hadoop2 hadoop2  780 11月 18 16:14 known_hosts

6、man一下sshd 帮助信息

6.1 可以看到-d选项是调试模式

  -d      Debug mode.  The server sends verbose debug output to the system log, and does
             not put itself in the background.  The server also will not fork and will only
             process one connection.  This option is only intended for debugging for the
             server.  Multiple -d options increase the debugging level.  Maximum is 3.

6.2  默认指定22端口,可以指定其他端口

     -p port
             Specifies the port on which the server listens for connections (default 22).
             Multiple port options are permitted.  Ports specified in the configuration file
             with the Port option are ignored when a command-line port is specified.  Ports
             specified using the ListenAddress option override command-line ports.

五、解决

1、sshd指定一个端口1234启动

[[email protected] root]$ sudo /usr/sbin/sshd  -d -p 1234
[sudo] password for hadoop2: 
hadoop2 不在 sudoers 文件中。此事将被报告。

visudo添加/usr/sbin/sshd权限,即可解决

hadoop2 ALL=(ALL) NOPASSWD:/usr/sbin/sshd

重新启动

[[email protected] root]$ sudo /usr/sbin/sshd  -d -p 1234
debug1: sshd version OpenSSH_5.3p1
debug1: read PEM private key done: type RSA
debug1: private host key: #0 type 1 RSA
debug1: read PEM private key done: type DSA
debug1: private host key: #1 type 2 DSA
debug1: rexec_argv[0]=‘/usr/sbin/sshd‘
debug1: rexec_argv[1]=‘-d‘
debug1: rexec_argv[2]=‘-p‘
debug1: rexec_argv[3]=‘1234‘
Set /proc/self/oom_score_adj from 0 to -1000
debug1: Bind to port 1234 on 0.0.0.0.
Server listening on 0.0.0.0 port 1234.
debug1: Bind to port 1234 on ::.
Server listening on :: port 1234.

2、然后打开另一个窗口,连接

[[email protected] ~]$ ssh -p 1234 master
[email protected]‘s password:

3、返回启动服务1234窗口,这时出现debug信息

[[email protected] root]$ sudo /usr/sbin/sshd  -d -p 1234
debug1: sshd version OpenSSH_5.3p1
debug1: read PEM private key done: type RSA
debug1: private host key: #0 type 1 RSA
debug1: read PEM private key done: type DSA
debug1: private host key: #1 type 2 DSA
debug1: rexec_argv[0]=‘/usr/sbin/sshd‘
debug1: rexec_argv[1]=‘-d‘
debug1: rexec_argv[2]=‘-p‘
debug1: rexec_argv[3]=‘1234‘
Set /proc/self/oom_score_adj from 0 to -1000
debug1: Bind to port 1234 on 0.0.0.0.
Server listening on 0.0.0.0 port 1234.
debug1: Bind to port 1234 on ::.
Server listening on :: port 1234.
debug1: Server will not fork when running in debugging mode.
debug1: rexec start in 5 out 5 newsock 5 pipe -1 sock 8
debug1: inetd sockets after dupping: 3, 3
Connection from 10.1.9.214 port 54822
debug1: Client protocol version 2.0; client software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: permanently_set_uid: 74/74
debug1: list_hostkey_types: ssh-rsa,ssh-dss
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST received
debug1: SSH2_MSG_KEX_DH_GEX_GROUP sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_INIT
debug1: SSH2_MSG_KEX_DH_GEX_REPLY sent
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: KEX done
debug1: userauth-request for user hadoop2 service ssh-connection method none
debug1: attempt 0 failures 0
debug1: PAM: initializing for "hadoop2"
debug1: userauth-request for user hadoop2 service ssh-connection method publickey
debug1: attempt 1 failures 0
debug1: test whether pkalg/pkblob are acceptable
debug1: PAM: setting PAM_RHOST to "10.1.9.214"
debug1: PAM: setting PAM_TTY to "ssh"
debug1: temporarily_use_uid: 2532/2533 (e=0/0)
debug1: trying public key file /home/hadoop2/.ssh/authorized_keys
debug1: fd 4 clearing O_NONBLOCK
Authentication refused: bad ownership or modes for directory /home/hadoop2
debug1: restore_uid: 0/0
debug1: temporarily_use_uid: 2532/2533 (e=0/0)
debug1: trying public key file /home/hadoop2/.ssh/authorized_keys
debug1: fd 4 clearing O_NONBLOCK
Authentication refused: bad ownership or modes for directory /home/hadoop2
debug1: restore_uid: 0/0
Failed publickey for hadoop2 from 10.1.9.214 port 54822 ssh2

从debug信息,可以看出Authentication refused: bad ownership or modes for directory /home/hadoop2,显然这个目录权限有问题!

4、现在去这个目录下验证

[[email protected] root]$ ll /home/hadoop2 -ld
drwxrwxr-x 4 hadoop2 hadoop 4096 11月 18 16:51 /home/hadoop2
[[email protected] root]$ umask
0002

额,775目录权限,系统默认umask是0022,即755的权限。

5、修改回来755

[[email protected] root]$ umask
0002

[[email protected] ~]# chmod 755 /home/hadoop2

六、最后验证,正常

[[email protected] root]$ ssh master
Last login: Fri Nov 18 17:23:20 2016 from 10.1.9.214
[[email protected] ~]$
时间: 2024-08-14 08:13:03

配置公钥登录提示还是输入密码的相关文章

ssh配置公钥私钥(key)登录SecureCRT

在用windows时管理linux服务器时,常会用到SecureCRT.Xshell以及开源的putty.在我工作环境大多都是采用密码认证的方式进行登录.今天对学习了些SecureCRT的密钥登录方式.对比密码认证方式,使用密钥认证登录会有以下几个优点: 1.公钥认证允许使用空密码,省去每次登录都需要输入密码的麻烦2.多用户管理服务器时,可以通过多个公钥登录同一用户下,可以避免因为密码认证被用户都需要密码,导致的密码容易泄密的危险.并且使用passwd修改密码,也不会影响到其他用户的登录.3.做

配置github SSH公钥登录

git的安装见https://www.cnblogs.com/liliyang/p/9829931.html 配置git使用ssh密钥 git支持https和git两种传输协议,github分享链接时会有两种协议可选: 若git使用https协议,每次pull, push均提示要输入密码,使用git协议,然后使用ssh密钥对认证,即可实现免密 配置git 通过ssh协议免密需要三个步骤: 1.生成密钥对 2.配置远程仓库(这里使用github)上的公钥 3.把git的 remote url 修改

版本管理&#183;玩转git(远程仓库配置和配置公钥免密登录)

git系列的最后一部分内容,我们先来看看如何查看远程仓库. 输入 git remote -v 我们还可以删除远程库,输入 git remote remove origin 删除后再次查询,信息为空. 注意了,这样并不会把远程库真的删除了,这样只是删除了远程库地址的别名. 我们重新添加一个远程库的标记,输入 git remote add origin https://gitee.com/blizzawang/lianshou.git 然后我们推送一下. git push origin master

Office365 联合身份验证服务配置-单点登录(SSO)

上篇文章我们介绍了office365国际版本及office365国内版本的功能介绍,今天主要说一下office365环境内配置实现SSO,其实说到sso相信大家已经很熟悉了(我们同样在之前的文章中有介绍windows azure部署sso)单点登录,当然也有叫目录集成的说法.那在office365上实现SSO会有什么效果呢?如果我们的机构内部已经在使用本地的 Active Directory,则可将其与我们的 Azure AD 目录相集成,借此可自动执行基于云的管理任务,并可向用户提供更加简化的

Ubuntu 13.10上用户如何获得root权限,用户如何获得永久root权限,如果配置root登录

一.用户如何获得root权限: 1. 进入terminal 2. 输入sudo  passwd root   并设置密码,提示要你输入两次密码,自己设定密码,一定要记住,然后切换到root使用 3. 输入 su root, 要求你输入密码,然后输入刚才设定的密码回车就进去了 4. 如果不想切换root但是想拥有大部分root权限,可以在使用命令时候加上sudo, sudo mount挂载 sudo 命令 ...... another 如果你想在linux上切换用户,那么输入 su  + 用户名就

使用公钥登录OpenSSH

使用公钥登录OpenSSH 2013年10月24日 16:47 生成密钥对 我所知道的,有三种生成密钥对的工具,分别为GNU/Linux下的ssh-keygen命令.puttygen.SSH Secure Shell. 使用ssh-keygen生成密钥对 [[email protected] ~]# ssh-keygen -C "Proxy for developers" Generating public/private rsa key pair. Enter file in whi

Ubuntu Server 14.04 配置ssh登录

Linux服务器限制ssh登录,查看登录日志 1.查看端口占用情况 $sudo su #lsof -i:<NUM> netstat -apn | grep <NUM> 2.修改默认端口号22 /etc/ssh/sshd_config Port 22 最好先添加一个端口号,然后启用成功之后再关闭端口22. 然后重启sshd: sudo /etc/init.d/ssh restart 没有反应,使用 # service sshd restart 提示 sshd:unrecognized

SSH公钥登录原理

在平时工作中我们经常要远程登录服务器,这就要用到SSH协议: $ ssh [email protected] 主要有两种登录方式:第一种为密码口令登录,第二种为公钥登录 密码口令登录 通过密码进行登录,主要流程为: 1.客户端连接上服务器之后,服务器把自己的公钥传给客户端 2.客户端输入服务器密码通过公钥加密之后传给服务器 3.服务器根据自己的私钥解密登录密码,如果正确那么就让客户端登录 公钥登录 公钥登录是为了解决每次登录服务器都要输入密码的问题,流行使用RSA加密方案,主要流程包含: 1.客

[linux]SSH公钥登录

由于口令密码容易泄露,SSH公钥登录相比口令登录更加安全.SSH可以轻松使用非对称加密技术给两台机子订立契约,步骤如下: 第一步 本地机生成秘钥对 指令:ssh-keygen 功能:在本地(~/.ssh/)产生公钥(id_rsa.pub)与私钥(id_rsa),其中私钥是本机使用的,不能泄露.公钥提供给远程服务器,追加到对方的公钥文件中(~/.ssh/authorized_keys). ssh-keygen -t rsa -P '' 这样本地的目录~/.ssh/中就会出现两个文件 id_rsa.