本地 SSH 服务器的端口转发(RouterOS)

本地 SSH 服务器的端口转发(RouterOS)

我刚刚完成了家庭 SSH 服务器的设置,它运行良好,我现在需要做的就是让它公开可用,这样我就可以在另一个网络上访问它,现在听起来很简单,对吧?好吧,我确信它是,但出于某种原因,我无法让它工作。

以下是我在 RouterOS (v6.47.9) 中配置的端口转发:

Chain: dstnat
Dst. Address: (my public IP address)
Protocol: 6 (tcp)
Dst. Port: 1024

Action: dst-nat
To Addresses: 192.168.1.125 (my SSH server IP)
To Ports: 22

我想要做的是通过我的公共 IP 地址登录到我的 SSH 服务器,本质上在我看来这应该像这样完成:

ssh (machine username)@(public IP address) -p 1024

无论如何,任何帮助都将非常感激。

(如果需要,我的 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 22
#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_ho>
#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 wi>
# 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
#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
PasswordAuthentication yes

答案1

首先要确保您的 ISP 没有执行 CGNAT,在这种情况下,如果不先要求他们在他们那一端执行此操作,您根本无法转发端口。您可以通过查看您获得的 IP 来检查这一点。如果它以 10.、100.、172. 或 192.168. 开头,则他们正在执行 CGNAT。

如果不是 CGNAT,那么除非您的 WAN IP 从未改变,否则每次您的互联网连接断开并重新连接时您都必须重新配置它(因为您的 ISP 会为您分配一个新的 WAN IP)。使用in-interface带有 WAN 接口的字段而不是,dst-address这样无论您当前分配的 IP 是什么,它都会始终有效。

您可以通过防火墙规则中的流量计数器检查端口转发是否正常工作。

相关内容