Linux 内核工具 iptables 配置TCP/UDP端口转发(命令参考)

1、配置TCP端口转发

把本机20000/TCP端口转发到7.7.7.7:20000

iptables -t nat -A PREROUTING -p tcp --dport 20000 -j DNAT --to-destination 7.7.7.7:20000
iptables -t nat -A POSTROUTING -j MASQUERADE

2、配置UDP端口转发

把本机20000/UDP端口转发到7.7.7.7:20000

iptables -t nat -A PREROUTING -p udp --dport 20000 -j DNAT --to-destination 7.7.7.7:20000
iptables -t nat -A POSTROUTING -j MASQUERADE

3、配置TCP端口转发(限制网卡IP)

本机网卡10.0.0.5:20000端口收到数据转发给7.7.7.7:20000

iptables -t nat -A PREROUTING -p tcp -d 10.0.0.5 --dport 20000 -j DNAT --to-destination 7.7.7.7:20000
iptables -t nat -A POSTROUTING -j MASQUERADE

4、配置UDP端口转发(限制网卡IP)

本机网卡10.0.0.5:20000端口收到数据转发给7.7.7.7:20000

iptables -t nat -A PREROUTING -p udp -d 10.0.0.5 --dport 20000 -j DNAT --to-destination 7.7.7.7:20000
iptables -t nat -A POSTROUTING -j MASQUERADE

5、定向TCP/UDP端口转发链,即不需要添加规则;

(iptables -t nat -A POSTROUTING -j MASQUERADE)

echo "${0} LocalIP LocalPort RemoteIP RemoteTcpPort RemoteUdpPort"
echo "${0} 172.16.0.29 55100 52.175.123.215 5000 10000"

#        iptables -t nat -A PREROUTING -p tcp --dport 55100 -j DNAT --to-destination 40.83.118.93:17000
#        iptables -t nat -A POSTROUTING -p tcp -d 40.83.118.93 --dport 17000 -j SNAT --to-source 172.16.0.29  
#        iptables -t nat -A PREROUTING -p udp --dport 55100 -j DNAT --to-destination 40.83.118.93:7000
#        iptables -t nat -A POSTROUTING -p udp -d 40.83.118.93 --dport 7000 -j SNAT --to-source 172.16.0.29 

        iptables -t nat -A PREROUTING -p tcp --dport $2 -j DNAT --to-destination $3:$4
        iptables -t nat -A POSTROUTING -p tcp -d $3 --dport $4 -j SNAT --to-source $1  
        iptables -t nat -A PREROUTING -p udp --dport $2 -j DNAT --to-destination $3:$5
        iptables -t nat -A POSTROUTING -p udp -d $3 --dport $5 -j SNAT --to-source $1 

        iptables -t nat --list -n

相关推荐

  1. Linux iptables地址转换

    2024-03-27 20:40:04       52 阅读
  2. nginx配置端口转发

    2024-03-27 20:40:04       67 阅读
  3. linux端口转发

    2024-03-27 20:40:04       55 阅读
  4. 各个系统配置端口转发

    2024-03-27 20:40:04       23 阅读
  5. iptables配置网络地址转换(nat)

    2024-03-27 20:40:04       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-27 20:40:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-27 20:40:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-27 20:40:04       82 阅读
  4. Python语言-面向对象

    2024-03-27 20:40:04       91 阅读

热门阅读

  1. ChatGPT编程实践指南:让AI成为你的代码良师

    2024-03-27 20:40:04       38 阅读
  2. LeetCode刷题记录——day7

    2024-03-27 20:40:04       38 阅读
  3. Qt5.14.2 全面解锁Qt5事件处理的精髓

    2024-03-27 20:40:04       43 阅读
  4. Hutool工具包中的Validator类数据校验

    2024-03-27 20:40:04       36 阅读
  5. Mysql中的执行计划怎么分析?

    2024-03-27 20:40:04       44 阅读
  6. 【零基础C语言】动态内存管理

    2024-03-27 20:40:04       39 阅读
  7. 【linux】整理一波常用linux命令

    2024-03-27 20:40:04       36 阅读
  8. 语言模型的原理、实战与评估

    2024-03-27 20:40:04       40 阅读
  9. Django query

    2024-03-27 20:40:04       34 阅读
  10. 程序猿的一些真相话,看你适不适合吃这碗饭!

    2024-03-27 20:40:04       36 阅读
  11. ubuntu系统无法播放B站视频或一直加载

    2024-03-27 20:40:04       40 阅读
  12. Dockerfile优化

    2024-03-27 20:40:04       40 阅读
  13. [蓝桥杯 2023 省 B] 冶炼金属

    2024-03-27 20:40:04       35 阅读