SSH 反向隧道已建立但无法连接

SSH 反向隧道已建立但无法连接

我家里有一台电脑,没有公网ip,所以我想建立一个反向隧道到另一台有公网ip的服务器,然后通过该服务器连接到我家里的电脑。但是建立反向隧道后我无法连接到我的电脑,希望有人能帮忙。

下面是我使用 autossh 建立到服务器的反向隧道的过程。我将公网 IP 修改为 123.456.78.90。

tony@tony-S340MC:~$ autossh -M 6018 -fCNR 0.0.0.0:7020:localhost:22 [email protected]

tony@tony-S340MC:~$ sudo netstat -antp | grep 6018
tcp        0      0 127.0.0.1:6018          0.0.0.0:*               LISTEN      7637/ssh            
tcp6       0      0 ::1:6018                :::*                    LISTEN      7637/ssh    
        
tony@tony-S340MC:~$ netstat -a | grep ssh
tcp        0      0 tony-S340MC:49642       123.456.78.90:ssh      ESTABLISHED

然后,这是服务器上的网络状态和公共端口状态。

[opc@srvagent1 ~]$ sudo netstat -antp | grep 7020
tcp        0      0 0.0.0.0:7020            0.0.0.0:*               LISTEN      24261/sshd: opc     
tcp6       0      0 :::7020                 :::*                    LISTEN      24261/sshd: opc     

[opc@srvagent1 ~]$ sudo firewall-cmd --zone=public --list-all
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 6019/tcp 7020/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:                

GatewayPorts服务器上的是yes

建立反向隧道后,我用另一台电脑连接。但是无法连接。我不知道哪个部分出了问题。

$ ssh -v -p 7020 [email protected]
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 123.456.78.90 [123.456.78.90] port 7020.
debug1: connect to address 123.456.78.90 port 7020: Connection timed out
ssh: connect to host 123.456.78.90 port 7020: Connection timed out

编辑1:我改为使用 ssh -vv 而不是 autossh,这是我的电脑上的输出。

tony@tony-S340MC:~$ ssh -vv -fCNR 0.0.0.0:7020:localhost:22 [email protected]
OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: resolve_canonicalize: hostname 123.456.78.90 is address
debug2: ssh_connect_direct
debug1: Connecting to 123.456.78.90 [123.456.78.90] port 22.
debug1: Connection established.
debug1: identity file /home/tony/.ssh/id_rsa type 0
debug1: identity file /home/tony/.ssh/id_rsa-cert type -1
debug1: identity file /home/tony/.ssh/id_dsa type -1
debug1: identity file /home/tony/.ssh/id_dsa-cert type -1
debug1: identity file /home/tony/.ssh/id_ecdsa type -1
debug1: identity file /home/tony/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/tony/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/tony/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/tony/.ssh/id_ed25519 type -1
debug1: identity file /home/tony/.ssh/id_ed25519-cert type -1
debug1: identity file /home/tony/.ssh/id_ed25519_sk type -1
debug1: identity file /home/tony/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/tony/.ssh/id_xmss type -1
debug1: identity file /home/tony/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 123.456.78.90:22 as 'opc'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,ext-info-c
debug2: host key algorithms: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,[email protected],rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: [email protected],zlib,none
debug2: compression stoc: [email protected],zlib,none
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,[email protected]
debug2: compression stoc: none,[email protected]
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: [email protected]
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: [email protected]
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:XxJkoHSa4aJEXoL9Ir5i0lSDBM0TA6E2tx6J6LQd/BQ
debug1: Host '123.456.78.90' is known and matches the ECDSA host key.
debug1: Found key in /home/tony/.ssh/known_hosts:5
debug2: set_newkeys: mode 1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/tony/.ssh/id_rsa RSA SHA256:IbUI2q2QVZuuRT6rvvdAHciGlzVBRZqPKdCS2EaW3Mc agent
debug1: Will attempt key: /home/tony/.ssh/id_dsa 
debug1: Will attempt key: /home/tony/.ssh/id_ecdsa 
debug1: Will attempt key: /home/tony/.ssh/id_ecdsa_sk 
debug1: Will attempt key: /home/tony/.ssh/id_ed25519 
debug1: Will attempt key: /home/tony/.ssh/id_ed25519_sk 
debug1: Will attempt key: /home/tony/.ssh/id_xmss 
debug2: pubkey_prepare: done
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_1000)


debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_1000)


debug2: we did not send a packet, disable method
debug1: Next authentication method: publickey
debug1: Offering public key: /home/tony/.ssh/id_rsa RSA SHA256:IbUI2q2QVZuuRT6rvvdAHciGlzVBRZqPKdCS2EaW3Mc agent
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: /home/tony/.ssh/id_rsa RSA SHA256:IbUI2q2QVZuuRT6rvvdAHciGlzVBRZqPKdCS2EaW3Mc agent
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (publickey).
Authenticated to 123.456.78.90 ([123.456.78.90]:22).
debug1: Remote connections from 0.0.0.0:7020 forwarded to local address localhost:22
debug2: fd 3 setting TCP_NODELAY
debug1: Requesting [email protected]
debug1: forking to background
debug1: Entering interactive session.
debug1: pledge: network

tony@tony-S340MC:~$ debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: remote forward success for: listen 0.0.0.0:7020, connect localhost:22
debug1: All remote forwarding requests processed

答案1

debug1: Connecting to 123.456.78.90 [123.456.78.90] port 7020.
debug1: connect to address 123.456.78.90 port 7020: Connection timed out
ssh: connect to host 123.456.78.90 port 7020: Connection timed out

您的第二个 SSH 实例无法与隧道侦听端口建立 TCP 连接。具体来说,ssh向主机 123.456.78.90 端口 7020 发送了 TCP 连接请求,但从未收到响应。

最简单的解释是防火墙阻止了这些连接请求。防火墙可能位于您正在运行的主机上ssh,也可能位于主机 123.456.78.90 上,也可能位于这两台主机之间的某个网络设备(如路由器)上。

这可能与主机 123.456.78.90 端口 7020 是 SSH 隧道的侦听端口这一事实无关。我没有发现ssh创建隧道的实例有任何明显问题。如果您能够连接到它,它可能会正常工作。

相关内容