如何进行端口转发
# 使用 SSH 命令实现端口转发
1、建立
ssh -N -f -L localhost:port-local:localhost:port-remote username@remote-ip-or-hostname
1
参数说明:
- ssh 命令
- -N 不执行远程命令
- -f 后台运行
- -L 指定本地端口转发
2、使用
访问 localhost:port-local 相当于访问 remote-ip-or-hostname@port-remote
3、检查状态
# 检查端口号
netstat -tulnp | grep 'ssh -N -f -L localhost:port-local:localhost:port-remote username@remote-ip-or-hostname'
1
2
2
4、关闭
找到相关进程,然后使用 kill pid
命令终止。
补充:
- 当你执行SSH命令时,系统会提示你输入密码。如果你设置了SSH密钥认证,可以省略密码提示。
- 通过配置
~/.ssh/config
实现快速启动,使用方式:ssh mytunnel
Host mytunnel
HostName remote-ip-or-hostname
User username
ForwardAgent no
LocalForward localhost:port-local:localhost:port-remote
1
2
3
4
5
2
3
4
5
# 使用 Mobaxterm 软件实现端口转发
1、选择 Tunneling

2、创建一个新的 tunnel

3、配置信息:

说明:
- 类型:Local port forwading
- 本地端口:填写在 Forwarded port
- 远程服务器和端口:填写 remote server 和 remote port
- SSH服务配置:填写 SSH server IP/Hostname、 SSH Login Username 和 SSH port
4、运行

