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

简介:

一、知识点补充

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

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

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


二、问题描述

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


三、问题呈现

1、生成免密钥对

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[hadoop2@master ~]$  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 hadoop2@master
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|         o       |
|        S .      |
|     o . o       |
|    + *+. o      |
|   o .o=== .     |
|  ..o..=*+. E.   |
+-----------------+

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

1
2
3
4
5
[hadoop2@master root]$  ssh -copy- id  -i ~/. ssh /id_rsa .pub 10.1.9.214
hadoop2@10.1.9.214'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、验证,还是提示密码输入

1
2
3
4
5
6
[hadoop2@master 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.
hadoop2@master's password:


四、排查

1、调出debug模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[hadoop2@master ~]$  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
hadoop2@master's password: 
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Requesting no- more -sessions@openssh.com
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配置文件,没发现异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#       $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、查看权限,也正常

1
2
3
4
5
6
7
8
[hadoop2@master ~]$  ls  ~/. ssh / -ld
drwx------ 2 hadoop2 hadoop2 4096 11月 18 16:13  /home/hadoop2/ . ssh /
[hadoop2@master ~]$  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选项是调试模式

1
2
3
4
   -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端口,可以指定其他端口

1
2
3
4
5
      -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启动

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

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

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

  重新启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[hadoop2@master 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、然后打开另一个窗口,连接

1
2
[hadoop2@master ~]$  ssh  -p 1234 master
hadoop2@master's password:


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[hadoop2@master 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、现在去这个目录下验证

1
2
[hadoop2@master root]$ ll  /home/hadoop2  -ld
drwxrwxr-x 4 hadoop2 hadoop 4096 11月 18 16:51  /home/hadoop2

 

1
2
[hadoop@master root]$  umask
0002

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


5、修改回来755

1
2
3
4
5
[hadoop@master root]$  umask
0002
 
 
[root@master ~] # chmod 755 /home/hadoop2



六、最后验证,正常

1
2
3
[hadoop2@master root]$  ssh  master
Last login: Fri Nov 18 17:23:20 2016 from 10.1.9.214
[hadoop2@master ~]$









本文转自 zouqingyun 51CTO博客,原文链接:http://blog.51cto.com/zouqingyun/1874410,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
网络安全 开发工具 git
配置本地环境以管理Git多账户SSH连接的方法
通过以上步駟设置后, 您可以轻松管理多個 Git 账户并且根据不同项目需求切换 SSH 密匙进行版本控制操作。
359 20
|
7月前
|
应用服务中间件 网络安全 数据安全/隐私保护
网关服务器配置指南:实现自动DHCP地址分配、HTTP服务和SSH无密码登录。
哇哈哈,道具都准备好了,咱们的魔术秀就要开始了。现在,你的网关服务器已经魔法满满,自动分配IP,提供网页服务,SSH登录如入无人之境。而整个世界,只会知道效果,不会知道是你在幕后操控一切。这就是真正的数字世界魔法师,随手拈来,手到擒来。
388 14
|
6月前
|
网络安全 开发工具 git
GitHub 多账户 SSH 配置指南
本文介绍了如何在同一台电脑上配置多个 GitHub 账户的 SSH 密钥。内容包括:检查现有密钥、生成新的 SSH 密钥、配置 SSH config 文件、将公钥添加到 GitHub、验证 SSH 连接、设置 Git 用户信息、创建工作区目录、使用不同账户克隆仓库,以及为每个仓库配置独立的用户信息等步骤。通过这些操作,可以实现在不同项目中使用不同的 GitHub 账户进行提交和管理。
469 0
|
9月前
|
安全 Linux 网络安全
在Linux(CentOS和AWS)上安装更新的git2的方法并配置github-ssh
经过以上这些步骤,你现在就能在GitHub上顺利往返,如同海洋中的航海者自由驰骋。欢迎你加入码农的世界,享受这编程的乐趣吧!
402 10
|
10月前
|
安全 网络安全 数据安全/隐私保护
Debian 12系统中允许Root远程SSH登录解决方法!
在 Debian 12 系统中开启 SSH 远程 Root 登录需修改 SSH 配置文件 (`sshd_config`),将 `PermitRootLogin` 设置为 `yes` 并确保密码认证启用。完成后重启 SSH 服务并验证连接。若防火墙启用,需放行端口 22。注意,直接开放 Root 登录可能带来安全风险,建议使用普通用户登录后切换至 Root。
1279 1
|
监控 Ubuntu 安全
debian或Ubuntu中开启ssh允许root远程ssh登录的方法
在Debian或Ubuntu系统中启用root用户的SSH远程登录需要编辑SSH配置文件、设置root密码并重启SSH服务。虽然这可以在某些情况下提供便利,但必须注意安全性,通过使用强密码、限制IP访问、使用SSH密钥认证等方法来保护服务器的安全。
5533 5
|
安全 Shell Linux
ssh密码忘记了怎么办
通过上述措施,不仅能够有效应对SSH密码遗忘的挑战,还能全方位加固SSH连接的安全,确保数据传输的无忧。
1005 2
|
安全 Linux Shell
SSH服务器拒绝密码登录的解决方法
SSH服务器拒绝密码登录的解决方法
4424 1
|
安全 网络协议 Linux
|
安全 Linux 网络安全
ssh中的密码登录和密钥登录
ssh中的密码登录和密钥登录