这里有一个需求,有三个主机,分别为qwfys、xtwj88、xtwj89,其中,qwfys可以通过ssh连接到xtwj88上,在xtwj88上通过ssh也是可以连接到xtwj89上,qwfys不能直接连接到xtwj89上,现在的要求是希望能够在qwfys上访问xtwj89上的特定端口的服务。
这个需求借助ssh的端口转发是可以实现的,这里假定,我们想要访问xtwj89上的端口8001,与此同时,我们在本地监听端口号8001,当有请求的时候,可以借助xtwj88将请求由qwfys转发到xtwj89的8001端口上。
lwk@qwfys:~$ ssh root@inner88.xtwj.com -L 8001:xtwj89:8001
Last login: Fri May 22 17:48:01 2020 from xtwj87Welcome to XTWJ KVM Compute Service ![root@xtwj88 ~]# ps -ef|grep ssh
root 1125 1 0 May21 ? 00:00:00 /usr/sbin/sshd -D
root 4389 1125 1 16:27 ? 00:00:00 sshd: root@pts/0
root 4442 4392 0 16:27 pts/0 00:00:00 grep --color=auto ssh
[root@xtwj88 ~]#
lwk@qwfys:~$ ps -ef |grep ssh
lwk 3459 3388 0 May20 ? 00:00:01 /usr/bin/ssh-agent /usr/bin/sogou-session /usr/bin/im-launch cinnamon-session-cinnamon2d
lwk 4491 3385 0 May20 ? 00:00:02 /usr/bin/ssh-agent -D -a /run/user/1000/keyring/.ssh
lwk 17867 17849 0 13:58 pts/0 00:00:00 ssh root@inner89.xtwj.com
lwk 25797 24255 0 16:13 pts/1 00:00:00 man ssh
lwk 26137 25836 0 16:27 pts/2 00:00:00 ssh -L 8001:xtwj89:8001 inner88.xtwj.com -l root
lwk 27137 27117 0 16:51 pts/3 00:00:00 grep --color=auto ssh
lwk@qwfys:~$
这里是主机qwfys上的hosts文件配置
lwk@qwfys:~$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 qwfys# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters192.168.3.88 inner88.xtwj.com
192.168.3.89 inner89.xtwj.com192.168.3.90 inner90.xtwj.com
192.168.3.91 inner91.xtwj.comlwk@qwfys:~$
Reference
- SSHDraft
- 利用SSH隧道端口映射进行反向代理的应用
- 命令ssh 安全隧道/端口转发
- IBM文库 - 实战 SSH 端口转发
- Ubuntu Documention - SSH/OpenSSH/PortForwarding
- SSH Port Forwarding Example
- How to Create SSH Tunneling or Port Forwarding in Linux
- A Guide to SSH Port Forwarding/Tunnelling
- How to Set up SSH Tunneling (Port Forwarding)