默认情况下,当您使用这样的命令时,本地端(ssh 客户端)会在地址为 127.0.0.1 的环回接口上创建侦听端口
ssh me@server -L3306:localhost:3306
如果您检查主机上的 netstat,您会看到类似这样的内容
sudo netstat -ntlp | grep 3306
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 12354/ssh
因此,本地节点上的应用程序可以连接到此类映射服务,因为环回接口对主机本身可见,但外部节点无权访问此虚拟接口,因此无法与正在侦听此单个接口的任何服务(端口)建立任何连接.
要指示本地 ssh 客户端与世界共享此类映射端口,您需要指示它绑定到所有接口(包括环回)或仅绑定到特定接口
# here you explicitly tell ssh client to accept connection to your tunnel
# from any client(i.e. bind listenning port to all interfaces)
ssh me@server -L0.0.0.0:3306:localhost:3306
sudo netstat -ntlp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 12354/ssh
#here you do the same thing by using -g option
ssh me@server -g -L3306:localhost:3306
sudo netstat -ntlp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 12354/ssh
#and here is an example of how to bind to specific interfaces only
# 10.0.0.12 is an IP of one of interfaces on your node
# 10.1.0.156 is also IP address of one interfaces of your node
ssh me@server -L10.0.0.12:3306:localhost:3306 -L10.1.0.156:3306:localhost:3306
sudo netstat -ntlp | grep 3306
tcp 0 0 10.0.0.12:3306 0.0.0.0:* LISTEN 12354/ssh
tcp 0 0 10.1.0.156:3306 0.0.0.0:* LISTEN 12354/ssh