SSH 问题经验
Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
问题描述
ssh 客户端 macbook 上通过ssh-keygen -t rsa
生成公钥id_rsa.pub
和私钥id_rsa
用键盘复制粘贴到 ssh 服务端的~/.ssh/authorized_keys
然后在客户端上发起连接:
1
| ssh -i \~/.ssh/id\_rsa heng@服务端IP
|
报错:
lost connection

解法
提示是公钥报错,直接复制粘贴有问题,复制粘贴的效果如下:

需要把客户端的公钥文件 scp 到远程的 ssh 服务端上,这里的 - i 是老私钥:
也可以在服务端打开密码登录,就不用指定私钥了:
之后在服务端上再做累加:
1
| cat id\_rsa.pub >> authorized\_keys
|
cat 累加到 authorized_keys的效果如下:

权限保证
SSH 配置项
主配置文件:/etc/ssh/sshd_config
帮助信息:man sshd_config
示例一、网管部配置文件(新用户需要创建密码,无密码无法登陆)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Port 32200
Protocol 2
PermitRootLogin no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized\_keys
PasswordAuthentication no
PermitEmptyPasswords yes
ChallengeResponseAuthentication no
UsePAM no
UseDNS no
Subsystem sftp /usr/lib/openssh/sftp-server
|
示例二、常规配置
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
| Port 22
Port 8210
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh\_host\_rsa\_key
HostKey /etc/ssh/ssh\_host\_dsa\_key
HostKey /etc/ssh/ssh\_host\_ecdsa\_key
HostKey /etc/ssh/ssh\_host\_ed25519\_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024
# Logging
SyslogFacility AUTH
LogLevel INFO
# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile h/.ssh/authorized\_keys
# Don't read the user's \~/.rhosts and \~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh\_known\_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust \~/.ssh/known\_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC\_\*
Subsystem sftp /usr/lib/openssh/sftp-server
# 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 yes %
|
配置项意义
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 168 169 170 171
| # 服务端默认的监听端口,可\*\*逐列写多个\*\*
# 默认22
Port 22
Port 8210
# 指定
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# 服务端本机的key所在位置
HostKey /etc/ssh/ssh\_host\_rsa\_key
HostKey /etc/ssh/ssh\_host\_dsa\_key
HostKey /etc/ssh/ssh\_host\_ecdsa\_key
HostKey /etc/ssh/ssh\_host\_ed25519\_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024
# Logging
SyslogFacility AUTH
LogLevel INFO
# Authentication:
LoginGraceTime 120
# 是否允许用户名为Root的进行远程登录
# 可选项为:yes、no、without-password
# 建议no
PermitRootLogin yes
StrictModes yes
# 启用RSA加密认证
RSAAuthentication yes
# 启用公钥认证
PubkeyAuthentication yes
# 指定存放本地用户公钥的文件
# 默认 \*\*%h/.ssh/authorized\_keys\*\*
AuthorizedKeysFile .ssh/authorized\_keys
# 忽略用户文件: \~/.rhosts and \~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh\_known\_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust \~/.ssh/known\_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
# 在\*\*PasswordAuthentication no\*\*的前提下,允许空密码
# 默认no,建议no
PermitEmptyPasswords no
# 在\*\*PasswordAuthentication no\*\*的前提下,提示用户输入密码
# 默认no,建议no
ChallengeResponseAuthentication no
# 通过密码做SSH认证
# 默认no
PasswordAuthentication no
# Kerberos认证相关选项
# 默认注释
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI认证相关选项
# 默认注释
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
# 允许ssh的client端变更语言环境变量
# 默认 AcceptEnv LANG LC\_\*
AcceptEnv LANG LC\_\*
# 指定sftp命令所在的文件位置,可通过 \*\*find / -name sftp-server\*\*做二次确认
# \*\*Debian\*\*默认 Subsystem sftp /usr/lib/openssh/sftp-server
# \*\*Centos\*\*默认 Subsystem sftp /usr/libexec/openssh/sftp-server
Subsystem sftp /usr/lib/openssh/sftp-server
# 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'.
# 启用PAM认证选项,相关配置文件位于\*\*/etc/pam.conf\*\*、\*\*/etc/pam.d/\*\*\*
# 启用后,密码提示(\*\*ChallengeResponseAuthentication\*\*)、使用密码认证(\*\*PasswordAuthentication\*\*)将被允许使用,具体依赖于PAM的配置
UsePAM yes %h/
|
SSH 使用经验
SSH 工具
SSH 限制指定用户从指定地址远程登录 / SSH 根据用户名限制登录
在/etc/ssh/sshd_config
中使用关键字:AllowUsers、AllowGroups、DenyUsers、DenyGroups
默认允许所有用户、组从任意地址登录,参考资料:man sshd_config
其中:
Deny是仅拒绝指定的条件,放行其他
Allow是放行指定的条件,拒绝其他
Allow 比 Deny 更严格规范。
当使用限制以后,若登录失败,报错提示:

例 1:只允许 admin 可以在任何地方登录,不允许其他用户登录
例 2:只允许 admin 从 192.168.130.21 / 23 登录,禁止其他用户登录
例 3:只允许 root 从 192.168.130.23 登录,允许 admin 从任意地点登录,禁止其他用户、地址登录
例 4:仅不允许 root 从 192.168.130.23 登录,但是允许 root 从其他地址登录,也允许其他用户从任意地址登录
sshd_config 属性
1 2 3 4 5 6 7
| # 本地用户名\*\*必须要有密码\*\*才行,useradd加的用户名不包含密码,必须手动创建
PermitEmptyPasswords no
# 允许Root登录
PermitRootLogin yes
|
仅允许密钥登录
重启服务:
配置 openssh(鸟哥服务器篇_P.313~337)
连接命令:
-p:表示服务端开放的端口;
-X:表示提供图形化接口;
原则
私钥永不离身,只做签名
基本原理
两台 PC,一台客户机 desktop,一台服务器 server,采用非对称加密方式:
客户机通过ssh-keygen生成公钥、私钥对,服务器通过下载 openssh、启动 ssh 服务生成公钥私钥对;
客户机要把生成的公钥(~/.ssh/id_rsa.pub
,以 rsa 加密方式为例),投放到服务器上对应用户目录下的~/.ssh/authorized_keys
;
重启服务systemctl restart ssh
即可。
注:
~/.ssh/authorized_keys
需要自己建立,~/.ssh
权限 700,~/.ssh/authorized_keys
权限 600,拥有者是本人
只要对 **/etc/ssh/sshd_config
、~/.ssh/authorized_keys
进行修改,需要重启 ssh 服务 ** 才能响应
Xshell 上生成的公钥复制时,可能会少一个 s,注意核对
Debian 和 Centos 的 sshd_config 默认有出入 ——Debian 默认 Root 禁登录
openssh配置文件(所有都以此文件为准):/etc/sshd_config
加密算法类型:rsa、ecdsa、des
通过ssh-keygen
生成客户端的公钥、私钥密钥对;
通过ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
把客户机的公钥发送到服务端用户所在的目录
类似的还可以使用:
1 2 3
| \[desktop] scp \~/.ssh/id\_rsa.pub [email protected]:\~ # 投放在服务端用户目录下
\[server] cat id\_rsa.pub >> /.ssh/authorized\_keys # 把客户端公钥投放在服务端用户认证目录下
|
服务器公钥保存在客户端的位置:~/.ssh/known_hosts
(客户机)
客户机公钥存储在服务器上的位置:~/.ssh/authorized_keys
(服务器)
PS:服务端重新生成公钥、私钥方法
1 2 3
| rm /etc/ssh/ssh\_host\* # 先删除原有的私钥、公钥
/etc/init.d/sshd restart # 再通过重新启动sshd服务重新生成
|
服务端第一次启动 sshd 生成的公钥、私钥:/etc/ssh/ssh_host_key*
(服务器)
客户端用户生成的公钥(复制到服务端):~/.ssh/id_rsa.pub
客户端用户生成的私钥(本地保存):~/.ssh/id_rsa
原理流程:
服务器建立公钥:每一次启动 sshd 服务时,该服务会主动去找/etc/ssh/ssh_host*
的文件,若系统刚刚安装完成时,由于没有这些公钥,因此 sshd 会主动去计算出这些需要的公钥,同时也会计算出服务器自己需要的私钥
客户端主动联机请求:若客户端想要联机到 ssh 服务器,则需要使用适当的客户端程序来联机,包括 ssh, putty 等客户端程序连接
服务器传送公钥给客户端:接收到客户端的要求后,服务器便将第一个步骤取得的公钥传送给客户端使用 (此时应是明码传送,反正公钥本来就是给大家使用的)
客户端记录并比对服务器的公钥数据及随机计算自己的公私钥:若客户端第一次连接到此服务器,则会将服务器的公钥记录到客户端的用户家目录内的~/.ssh/known_hosts
。若是已经记录过该服务器的公钥,则客户端会去比对此次接收到的与之前的记录是否有差异。若接受此公钥,则开始计算客户端自己的公私钥
回传客户端的公钥到服务器端:用户将自己的公钥传送给服务器。此时服务器:具有服务器的私钥与客户端的公钥,而客户端则是:具有服务器的公钥以及客户端自己的私钥,你会看到,在此次联机的服务器与客户端的密钥系统 (公钥 + 私钥) 并不一样,所以才称为非对称加密系统
开始双向加解密:
参考资料:http://www.cnblogs.com/nexiyi/archive/2013/01/06/2847865.html ##RSA和DSA的原理