我家裡有一台電腦沒有公網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
Edit1:我更改為使用 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
創建隧道的實例有任何明顯的錯誤。如果您能夠連接到它,它可能會正常工作。