我正在努力弄清楚为什么 sshd 没有响应传入的请求。我有一个通过无线连接到 Linksys 路由器的 ubuntu 18.04 系统。
Router: 10.132.2.1
Computer: 10.132.2.20 This is a static IP address on wlp1s0
sshd 正在侦听端口 20(选择奇怪端口的原因并不重要)。
我在计算机上运行 /usr/sbin/sshd -dddD 并查看:
debug1: Bind to port 20 on 10.132.2.20
Server listening on 10.132.2.20 port 20
我尝试使用 ssh -vv -p 20 从另一台计算机上的 WAN 连接到 ssh,然后查看:
debug2: ssh_connect_direct
debug1: Connecting to [ ] port 20.
在计算机 10.132.2.20 上的一个单独窗口中,我运行 tcpdump -i wlp1s0 port 20 -n -Q inout 并查看:
10:05:29.787051 IP 107.77.240.93.40190 > 10.132.2.20.20: Flags [s], seq 1030904803, win 64240, options [mss 1390,sackOK,TS val 1967897638 ecr 0,nop,wscale 7], length 0
sshd 没有输出。
编辑:
作为调试的一部分,我尝试将计算机连接到为 WIFI 提供服务的路由器。这有效。我能够从 WAN 连接到与无线连接关联的 IP 地址。再次将有线连接切换回 10.132.1 网络导致 10.132.2 ssh 连接失败。所以问题与生活在两个不同网络上的 ssh 有关。我尝试制作 sshd 服务的副本,这样我就有一个服务于 10.132.1.* 网络,另一个服务于 10.132.2.* 网络。这再次失败,但同样,仅当计算机位于两个网络上时。如果有线网络和无线网络在同一个路由器上,那么它可以工作。
编辑2:
这是 ip route 的输出(我不知道如何解码这里的所有内容——在我的基本理解中,看起来有两个网络,10.132.1.1 优先级(指标 100)超过 10.132.2.1)
default via 10.132.1.1 dev eno1 proto dhcp metric 100
default via 10.132.2.1 dev wlp1s0 proto dhcp metric 600
10.132.1.0/24 dev eno1 proto kernel scope link src 10.132.1.10 metric 100
10.132.2.0/24 dev wlp1s0 proto kernel scope link src 10.132.2.20 metric 600
169.254.0.0/16 dev wlp1s0 scope link metric 1000
最后,这里是原始的sshd_config文件(注:10.132.1.10是电脑所在的有线网络——ssh在有线网络上没有问题):
# $OpenBSD: sshd_config,v 1.101 2017/03/14 07:19:07 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/bin:/bin:/usr/sbin:/sbin
# 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 override the
# default value.
Port 20
Port 22
#Port 80
#Port 22
#AddressFamily any
ListenAddress 10.132.2.20:20
ListenAddress 10.132.1.10:22
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# 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
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
#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 yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts yes
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
# override default of no subsystems
Subsystem sftp /usr/lib/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
tl;dr:删除到 10.132.1.1 的默认路由,10.132.2.x 上的传入连接将在 10.132.2.x 网络上成功返回。请注意,这取决于我不将 10.132.1.1 用于传入连接这一事实。
长版:
在各种论坛上发布这个问题后,有人建议我的配置是所谓的“双主”,并且我没有正确设置以支持两个网络。对此的一些研究使我从“ip route”中找到了这些条目:
这就是说机器当前设置了两条默认路由。在 COMPUTER B 的 10.132.1.10 端放置一个 tcpdump,然后尝试 ssh 到 10.132.2.20 表明 COMPUTER B 正试图在 10.132.1.x 网络上发送 IP 地址为 10.132.2.20 的响应——这失败的。
奇怪的是,在此之前,我从未发现此设置有任何问题。我的系统的简单解决方案就是删除 10.132.1.1 上的默认路由。由于路由表还包括以下内容:
与本地设备的任何连接都将继续工作。