SSH 服务调试

ssh service debuging

foreversmart write on 2020-09-01
经常在使用 SSH 服务的时候我们会遇到各式各样的连接问题
SSH 服务是什么
它是有一个 SSHD 服务和一些 SSH client 组成的形式
SSHD 服务的端口默认会是22,我们可以通过修改 /etc/ssh/sshd_config 配置文件来指定其它端口。
我们遇到最最最常见的问题就是错误
ssh: connect to host 192.168.1.104 port 22: Operation timed out
Shell
我们需要检查 IP 和端口是否是通的,我们可以用 telnet 工具来检查
telnet 192.168.1.104 22 Trying 192.168.1.104... telnet: connect to address 192.168.1.104: Host is down telnet: Unable to connect to remote host
Shell
如果 IP 不通可能需要去检查是否在同一个子网,是否有正确的路由,sshd 服务的主机是否开机,sshd 服务的主机是否异常。
如果 IP 通而端口不同,大概率是我们使用了错误的 ssh 端口,或者防火墙,安全组原因导致的。
如果 IP 和 端口是通的
telnet 192.168.1.104 22 // TODO 正确的
Shell
但是我们执行
ssh root@192.168.1.104
Shell
还是连接不上,可能会报很多奇奇怪怪的错误,比如下面的这种错误
Permission denied (publickey)
Shell
我们可能在检查完客户端和服务端 ssh 配置后完全看不错有任何问题,这通常是由于很多问题导致的,比如权限问题,sshd 配置问题等等,甚至和 ssl 的版本也有关系; 这个时候我们就需要更多的细节供我们分析具体 SSH 连接不上的原因。
客户端加入 -vvv 参数我们可以看到更多客户端连接 SSHD 服务的日志:
ssh root@192.168.1.104 -vvv OpenSSH_8.1p1, LibreSSL 2.7.3 debug1: Reading configuration data /Users/hong/.ssh/config debug1: /Users/hong/.ssh/config line 30: Applying options for nas debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 47: Applying options for * debug2: resolve_canonicalize: hostname 192.168.3.43 is address debug2: ssh_connect_direct debug1: Connecting to 192.168.3.43 [192.168.3.43] port 2233. debug1: Connection established. debug1: identity file /Users/hong/.ssh/id_rsa type 0 debug1: identity file /Users/hong/.ssh/id_rsa-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_8.1 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4 debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002 debug2: fd 3 setting O_NONBLOCK debug1: Authenticating to 192.168.3.43:2233 as 'hong' debug3: put_host_port: [192.168.3.43]:2233 debug3: hostkeys_foreach: reading file "/Users/hong/.ssh/known_hosts" debug3: record_hostkey: found key type ECDSA in file /Users/hong/.ssh/known_hosts:147 debug3: load_hostkeys: loaded 1 keys from [192.168.3.43]:2233 debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521 debug3: send packet: type 20 debug1: SSH2_MSG_KEXINIT sent debug3: receive packet: type 20 debug1: SSH2_MSG_KEXINIT received debug2: local client KEXINIT proposal debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1 debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1 debug2: compression ctos: none,zlib@openssh.com,zlib debug2: compression stoc: none,zlib@openssh.com,zlib debug2: languages ctos: debug2: languages stoc: debug2: first_kex_follows 0 debug2: reserved 0 debug2: peer server KEXINIT proposal debug2: KEX algorithms: curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256 debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519 debug2: ciphers ctos: aes128-ctr,aes128-gcm@openssh.com,aes192-ctr,aes256-ctr,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com debug2: ciphers stoc: aes128-ctr,aes128-gcm@openssh.com,aes192-ctr,aes256-ctr,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com debug2: MACs ctos: hmac-sha2-256,hmac-sha2-256-etm@openssh.com,hmac-sha2-512,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com,umac-128@openssh.com debug2: MACs stoc: hmac-sha2-256,hmac-sha2-256-etm@openssh.com,hmac-sha2-512,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com,umac-128@openssh.com debug2: compression ctos: none,zlib@openssh.com debug2: compression stoc: none,zlib@openssh.com debug2: languages ctos: debug2: languages stoc: debug2: first_kex_follows 0 debug2: reserved 0 debug1: kex: algorithm: curve25519-sha256@libssh.org debug1: kex: host key algorithm: ecdsa-sha2-nistp256 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug3: send packet: type 30 debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug3: receive packet: type 31 debug1: Server host key: ecdsa-sha2-nistp256 SHA256:1IYf7jb5jlMr5Swj/OwTY46veOzqu3yxA3whSCh/P5k debug3: put_host_port: [192.168.3.43]:2233 debug3: put_host_port: [192.168.3.43]:2233 debug3: hostkeys_foreach: reading file "/Users/hong/.ssh/known_hosts" debug3: record_hostkey: found key type ECDSA in file /Users/hong/.ssh/known_hosts:147 debug3: load_hostkeys: loaded 1 keys from [192.168.3.43]:2233 debug3: hostkeys_foreach: reading file "/Users/hong/.ssh/known_hosts" debug3: record_hostkey: found key type ECDSA in file /Users/hong/.ssh/known_hosts:147 debug3: load_hostkeys: loaded 1 keys from [192.168.3.43]:2233 debug1: Host '[192.168.3.43]:2233' is known and matches the ECDSA host key. debug1: Found key in /Users/hong/.ssh/known_hosts:147 debug3: send packet: type 21 debug2: set_newkeys: mode 1 debug1: rekey out after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug3: receive packet: type 21 debug1: SSH2_MSG_NEWKEYS received debug2: set_newkeys: mode 0 debug1: rekey in after 134217728 blocks debug1: Will attempt key: /Users/hong/.ssh/id_rsa RSA SHA256:TzTrQGFpuwNbLzb/RemHHbK0KiX/F+1PfUfWbuHqyfU explicit debug2: pubkey_prepare: done debug3: send packet: type 5 debug3: receive packet: type 7 debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521> debug3: receive packet: type 6 debug2: service_accept: ssh-userauth debug1: SSH2_MSG_SERVICE_ACCEPT received debug3: send packet: type 50 debug3: receive packet: type 51 debug1: Authentications that can continue: publickey debug3: start over, passed a different list publickey debug3: preferred publickey,keyboard-interactive,password debug3: authmethod_lookup publickey debug3: remaining preferred: keyboard-interactive,password debug3: authmethod_is_enabled publickey debug1: Next authentication method: publickey debug1: Offering public key: /Users/hong/.ssh/id_rsa RSA SHA256:TzTrQGFpuwNbLzb/RemHHbK0KiX/F+1PfUfWbuHqyfU explicit debug3: send packet: type 50 debug2: we sent a publickey packet, wait for reply debug3: receive packet: type 51 debug1: Authentications that can continue: publickey debug2: we did not send a packet, disable method debug1: No more authentication methods to try. hong@192.168.3.43: Permission denied (publickey).
Shell
2. 开启 SSHD 服务日志
通常 sshd 的日志文件会在
/var/log/auth 文件中
我们需要将 sshd 的日志打开,并且将日志级别调高
# Logging SyslogFacility AUTH LogLevel DEBUG3
Shell
下面收集了几个失败的 ssh 连接失败服务端的日志:
rexec line 41: Deprecated option RSAAuthentication //
Shell
Authentication refused: bad ownership or modes for file .ssh/authorized_keys
Shell
Could not open authorized keys '.ssh/authorized_keys': Permission denied
Shell

「真诚赞赏,手留余香」

Foreversmart

真诚赞赏,手留余香

使用微信扫描二维码完成支付