ssh:連接到主機連接埠 22:連線被外部 IP 位址拒絕

ssh:連接到主機連接埠 22:連線被外部 IP 位址拒絕
ssh: connect to host XXX.XXX.XXX.XX port 22: Connection refused

當我嘗試連接外部 IP(WAN IP 位址)時會發生這種情況。

我能夠成功地使用 lan 下的內部 IP 位址進行連線。

出了什麼問題為什麼我無法連接外部IP?

請注意,我已在路由器上將連接埠轉送至連接埠 22 上的 ssh 的內部 IP 位址。

當我跑步時ps -eaf|grep sshd

根 5942 1 0 18:42 ? 00:00:00 /usr/sbin/sshd
1000 6628 6382 0 18:55 pts/0 00:00:00 grep sshd

運行 ssh -VV 時[電子郵件受保護]

OpenSSH_6.0p1 Debian-4、OpenSSL 1.0.1e 2013 年 2 月 11 日

我的 sshd_config 檔案:

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# 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
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768

# 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 yes

# 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

您確定您在路由器上做了正確的事情(即這是否可以與其他服務上的另一個連接埠一起使用)?

我問這個問題是因為「連線被拒絕」發生在 TCP 級別,原因通常是沒有人在目標電腦上偵聽該連接埠(這相當於 sshd 未運行)或某些防火牆阻止了它。

既然你說它適用於你的本地網路的位址,那麼它似乎是路由器沒有進行轉送或轉送到其他地方。

答案2

將以下行新增至您的 ssh 使用者設定檔中,如下所示。

vi ~/.ssh/config

Host *
ServerAliveInterval 300

更改權限如下:

 chmod 600 ~/.ssh/config

重新啟動守護程式。希望這可以幫助。

相關內容