
我的本地網路中有一台伺服器,用於透過 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 作為第一步,如果失敗,他們就無法連線。
所有這些都是說,如果您遇到類似的問題,請務必查看您的伺服器和所有選項的路由器設定。