
我有一個 Windows 11 機器,在連接埠 80 上的 127.0.0.1 上運行 IIS,該機器透過 CGNAT 連接到 Internet。我還有一個 Linode(VPS 供應商)的 VPS。 VPS 的 IP 位址是 139.162.19.185。我想透過我的 VPS 從互聯網上的其他任何地方連接到我的 Windows 11 機器上的伺服器/連接埠。
我在管理員模式下使用 Windows 11 命令提示字元執行以下命令:
ssh -R 80:localhost:80 [email protected]
當我嘗試使用 VPS 存取 SSH 隧道時http://139.162.19.185:80,我連接被拒絕,但是當我嘗試使用存取我的本地主機時http://127.0.0.1,我看到 IIS 啟動主頁。
我檢查了/etc/ssh/sshd_config文件,GatewayPorts和AllowTcpForwarding都啟用了。
更新:
這是 netstat -tlnp 的結果
root@localhost:~# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:80 0.0.0.0:* LISTEN 35027/sshd: root@pt
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 434/sshd: /usr/sbin
tcp6 0 0 ::1:80 :::* LISTEN 35027/sshd: root@pt
tcp6 0 0 :::22 :::* LISTEN 434/sshd: /usr/sbin
root@localhost:~#
捲曲的結果如下:
root@localhost:~# curl http://127.0.0.1:80
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
</BODY></HTML>
root@localhost:~#
是否有我需要編輯的設定檔?我嘗試了一些調試,看看Linode上是否有防火牆。我安裝了apache2,在80埠上運作正常,所以80埠沒有被阻塞。
答案1
您使用該選項的方式-R
意味著您希望遠端僅綁定到環回介面。從ssh
手冊頁:
-R [bind_address:]port:host:hostport
...
By default, TCP listening sockets on the server will be bound to the loopback interface only. This may be overridden by
specifying a bind_address. An empty bind_address, or the address ‘*’, indicates that the remote socket should listen on
all interfaces. Specifying a remote bind_address will only succeed if the server's GatewayPorts option is enabled (see
sshd_config(5)).
簡化後的意思是:
-R port:host:hostport
僅綁定到環回-R :port:host:hostport
將綁定到所有接口- 將會
-R *:port:host:hostport
和-R 0.0.0.0:port:host:hostport
因此,根據您的情況,其中任何一個都應該有效:
ssh -R :80:localhost:80 [email protected]
ssh -R *:80:localhost:80 [email protected]
ssh -R 0.0.0.0:80:localhost:80 [email protected]
*以下是我最初嘗試根據文件文字解釋此行為 - 並進行了一些說明。
您使用了 PORT:IP:PORT 語法,該語法未指定綁定位址。
上述描述中的文字是指遠端連接埠之前specifying a bind_address
有一個(PORT:IP:PORT中的第一個連接埠)。:
在範例中,沒有:
,這表示您沒有指定 bind_address ,並且根據手冊頁,遠端將僅綁定到環回介面(預設)。為了綁定到所有接口,你需要指定一個bind_address(即:
在遠端連接埠前添加一個),即使實際的bind_address是一個「空字串」!在這種情況下,「空字串」或 a 都*
可以工作,但請記住,它:
是強制性的,不能僅限於綁定到環回位址!
更新
在@barlop的評論之後,我的解釋似乎不是很清楚,所以我試圖一步一步地澄清這裡的事情:
- 該選項定義為
-R [bind_address:]port:host:hostport
。 - 整個
[bind_address:]
部分是可選的。如果不存在,則該選項具有格式-R port:host:hostport
並套用預設行為,這意味著TCP listening sockets on the server will be bound to the loopback interface only
. - 如果該
[bind_address:]
部分存在,則表示我們具有格式-R bind_address:port:host:hostport
,請注意,現在第一個:
不再是可選的。從這一刻起,bind_address
可以取多個值,其中一個可能的值確實是空字串!因此,將冒號添加到選項中,意味著我們bind_address
這次確實指定了 a,儘管是空的(根據描述,這表示the remote socket should listen to all interfaces
.
我希望這能讓它更清楚一點,基本上not specifying a bind_address
是specifying an empty string as bind_address
兩個不同的東西! 「空字串」bind_address 被認為是綁定地址,指的是使用:PORT:IP:PORT
語法,而不在最左邊的冒號之前為該地址寫入任何內容! 。
這是我測試它的輸出(在不同的連接埠上,但效果相同):
查看它正在遠端電腦上運行 SSH 並運行 netstat -an。
gepa@localhost:~$ ssh -R :5555:localhost:5555 cloud1 netstat -an | grep :5555
tcp 0 0 0.0.0.0:5555 0.0.0.0:* LISTEN
tcp6 0 0 :::5555 :::* LISTEN
gepa@localhost:~$ ssh -R 5555:localhost:5555 cloud1 netstat -an | grep :5555
tcp 0 0 127.0.0.1:5555 0.0.0.0:* LISTEN
tcp6 0 0 ::1:5555 :::* LISTEN
請注意,:
與5555
省略:
.
順便說一句,更清楚的是不使用空字串bind_address並寫入$ ssh -R *:5555:localhost:5555 comp
或0.0.0.0:80:localhost:80
PORT:IP:PORT 語法綁定到 127.0.0.1 的原因也是出於安全性原因。因此,如果您確實希望它進行非本地綁定,最好明確說明,例如使用通配符或指定 0.0.0.0 或指定 LAN 位址(如果僅從 LAN 連接)。