我在本地网络中有一个服务器,我用它来通过 SSH 工作。我从 LAN 连接到它没有问题。当我尝试从外部连接到它时,问题就出现了。我为端口 2209(这是我的自定义 SSH 端口)以及 Web 端口设置了端口转发。设置后我能够连接到它一次,但大约一周后,连接一直超时。
同时,我可以从服务器打开网页,因此端口 80 工作正常。它实际上只是端口 2209。我已确认,端口转发正确,该端口在 Windows 防火墙中设置为例外,并且规则已启用。但连接超时了。
如果您需要任何截图或日志,请告诉我,我可以提供。我不确定需要什么信息才能进行最佳评估。
UPD 1:评论中有人指出使用 -vvv 选项运行 ssh,以下是日志:
❯ ssh -vvv -p 2209 andrey@<ip>
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n 7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "<ip>" port 2209
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to <ip> [<ip>] port 2209.
debug1: connect to address <ip> port 2209: Resource temporarily unavailable
ssh: connect to host <ip> port 2209: Resource temporarily unavailable
同时,在 LAN 内连接完全没问题。从 WAN 连接到服务器的 HTTP 也完全没问题。
这是我的 SSH 配置:
❯ cat /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj 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.
Include /etc/ssh/sshd_config.d/*.conf
Port 2209
#AddressFamily any
#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 no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#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
并且/etc/ssh/sshd_config.d
是空的,所以这是唯一的配置。
UPD 2:我意识到我从未提到我的具体设置。这是一台 Windows 机器,但 SSH 位于 WSL 内部。所以我需要通过 Windows 防火墙才能访问 SSH。
答案1
就像所有这种情况一样,这并不是一种奇怪的神秘力量在阻止我。我查看了路由器中的端口转发和 Windows 中的防火墙。但在查看了路由器的设置后,我发现它的防火墙非常小,除了阻止来自 WAN 的 ping 之外,它几乎什么也不做。我猜我所有的 SSH 客户端都使用 ping 作为第一步,如果失败,它们就无法连接。
总之,如果你有类似的问题,一定要检查你的服务器和路由器设置的所有选项。