ssh port

端口转发

本地转发

用于发送

本地端口转发(Local Port Forwarding

发送到本地端口的请求,转发到目标端口

1
-L [bind_address:]port:host:hostport
  • listen to port on the local side 监听本地连接

  • optionally bound to the specified bind_address

  • 与此端口建立连接时,该连接都会通过安全通道转发,将流量转发到远程hosthostport

  • The bind_address of “localhost” indicates that the listening port 本地使用, while an empty address or ‘*’ indicates that the port should be available from all interfaces.

应用:远程主机端口服务没有对外开放,使用可以直接

1
2
3
ssh -i private_key -p 1822 -fN -L :33061:127.0.0.1:3306 root@remote_host

ssh -L 8080:facebook.com:80 username@host

然后本地直接向对应端口发送数据就可以了

remote_host 可以任一主机

跳板机

目标: host1 连接 host2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# host1 上  监听它自己的2222端口,然后将所有数据经由host3,转发到host2的22端口
ssh -L 2222:host2:22 user3@host3
# host1 上 host3 相当于跳板机
ssh -p 2222 user2@localhost

# ssh-host 可以解析remote-address
ssh -L local-port:remote-address:remote-port user@ssh-host



# 另一做法

# 在host3上面
ssh -L 2222:host2:22 user3@host3

远程转发

用于接收

远程端口转发(Remote Port Forwarding

将发送到远程端口的请求,转发到目标端口

1
-R [bind_address:]port:host:hostport

过程和上面差不多

应用:host不能连接外网,需要在中转机器运行命令

应用

背景: 内网访问不了外网,me只能访问host1。

target: host1 和 hosts2 可以访问remote

image-20200722153608451

host1:host1_port –> me –> remote:remote_port

1
2
# me执行
ssh -i private -p host1_ssh_port -fN -R host1_port:remote:remote_port root@host1

host2_port–>host1:host1_port –> me –> remote:remote_port

1
2
# host1 执行
ssh -fN -R host2_port:localhost:host1_port root@host2

host1和host2 都通过remote域名访问 修改/etc/hosts127.0.0.1 remote