我的服务器通过路由器(NAT)连接到互联网,我已经设置了端口转发。
在局域网IP连接时SSH工作正常,但通过广域网连接时不工作。
我曾经尝试过
/etc/ssh ) /etc/hosts.allow和/etc/hosts.deny sshd: ALL到hosts.allow iptables -L (没有任何内容) ProxyCommand nc %h %p 从这里回答 debug1: sshd version OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014 debug1: key_parse_private2: missing begin marker debug1: read PEM private key done: type RSA debug1: private host key: #0 type 1 RSA debug1: key_parse_private2: missing begin marker debug1: read PEM private key done: type DSA debug1: private host key: #1 type 2 DSA debug1: key_parse_private2: missing begin marker debug1: read PEM private key done: type ECDSA debug1: private host key: #2 type 3 ECDSA debug1: private host key: #3 type 4 ED25519 debug1: rexec_argv[0]='/usr/sbin/sshd' debug1: rexec_argv[1]='-d' Set /proc/self/oom_score_adj from 0 to -1000 debug1: Bind to port 222 on 0.0.0.0. Server listening on 0.0.0.0 port 222. debug1: Bind to port 222 on ::. Server listening on :: port 222. debug1: Server will not fork when running in debugging mode. debug1: rexec start in 5 out 5 newsock 5 pipe -1 sock 8 debug1: inetd sockets after dupping: 3, 3 Connection from xxx.xxx.xxx.xxx port 58644 on 192.168.0.101 port 222
ssh -vvv -C -A -X -p 2222 [email protected] OpenSSH_7.2p2 Ubuntu-4ubuntu2.1, OpenSSL 1.0.2g 1 Mar 2016 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug2: resolving "xxx.xxx.xxx.xxx" port 2222 debug2: ssh_connect_direct: needpriv 0 debug1: Connecting to xxx.xxx.xxx.xxx [xxx.xxx.xxx.xxx] port 2222. debug1: Connection established. debug1: key_load_public: No such file or directory debug1: identity file /home/yan/.ssh/id_rsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/yan/.ssh/id_rsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/yan/.ssh/id_dsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/yan/.ssh/id_dsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/yan/.ssh/id_ecdsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/yan/.ssh/id_ecdsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/yan/.ssh/id_ed25519 type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/yan/.ssh/id_ed25519-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.1 ssh_exchange_identification: read: Connection reset by peer
任何意见将不胜感激。 谢谢!
感谢您提供日志。 所以我们可以看到,在服务器上的sshd看到连接进来,但它是。
我的第一个猜测是关于你的NAT路由器。 也许这不是很好。 你的描述在一个方面是不清楚的:如果你从服务器连接到你的NAT路由器的互联网端,这是行不通的,你可以尝试从互联网连接。
您可以尝试其他服务,例如Web服务器或其他任何您了解的服务,然后调整您的NAT路由器以使其可访问。 它工作与否?
非常确切的信息,你可以使用嗅探工具,如tcpdump或wireshark收集。 理想情况下,您可以在连接的两侧使用它; 那么你会看到数据包是如何NAT的。
假设你的ssh客户端是IP 1.1.1.1,你的服务器的互联网连接是3.3.3.3。 所以inputssh 3.3.3.3 ENTER应该从1.1.1.1和随机端口发送一个tcp syn数据包,比如说55555到3.3.3.3。 你应该使用你的sniffe4r在networking接口上看到这个包。 这个数据包在3.3.3.3上通过互联网到你的NAT路由器,在那里它被NAT到一个局域网IP,比如192.168.1.11。 您的服务器上的嗅探器应该看到从1.1.1.1到192.168.1.11端口22的TCP SYN数据包。
当sshd接受连接时,你应该在你的本地嗅探器上看到应答数据包:从192.168.1.11端口22到1.1.1.1端口55555的tcp ack / syn数据包。它通过你的NAT路由器,并且应该被修改为“from 3.3 .3.3端口22到1.1.1.1端口55555“。 你通常看不到传出的数据包,因为在这一点上你不能嗅探。 但是,如果一切正常到这里,它将会到达你的客户端,那里的嗅探器可以抓住它。 此时,客户端将看到build立的连接,并使用tcp ack数据包从1.1.1.1端口55555到3.3.3.3端口22的数据包应答,以确认连接build立。 此数据包将travle并获得NAT,并且…您的服务器也将看到一个双向握手完成。
但是,当我们看到“通过同行重置连接”时,很可能您的问题会在此之前出现。
只是提示tcpdump ing:如果你ssh到你的客户端机器(可能是互联网上的根服务器)并且在那里启动tcpdum,你可能会看到一个数据包的结尾stream,它将tcpdump转发你的tcpdump输出的数据包, tcpdumped再次等等。 为了方便pipe理,你可以把你的ssh服务器的端口(或者至less你的路由器上的NATconfiguration)改成另一个端口,比如2222。
那么你的ssh命令将是: ssh 3.3.3.3 -p 2222 ,你可以像你一样过滤你的tcpdump
tcpdump port 2222
或者,如果你想看到除了你正在工作的SSH客户端连接,
tcpdump not port 22
玩得开心,观看互联网stream量,学习很多!
TomTomTom