最新消息:

Linux 端口转发用哪个工具好?

札记 chuangfuzhe 354浏览 0评论

linux 进行端口转发的工具很多,自带的有 iptables,其他的工具有 gost,rinetd,socat nc等。本文主要记录iptables的使用方式

1.iptables : 支持tcp 和udp,是优先选用的转发方式。

CentOS 7.0 以下使用的是iptables,使用iptables 之前需要开启:

$ echo 1 >/proc/sys/net/ipv4/ip_forward

iptable 可以进行单端口和端口段转发:

 

单端口转发
iptables -t nat -A PREROUTING -p tcp --dport [要转发的端口号] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -A PREROUTING -p udp --dport [要转发的端口号] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -A POSTROUTING -p tcp -d [要转发的服务器IP] --dport [要转发的端口号] -j SNAT --to-source [本机IP]
iptables -t nat -A POSTROUTING -p udp -d [要转发的服务器IP] --dport [要转发的端口号] -j SNAT --to-source [本机IP]
端口段转发
栗子:转发10000到20000这个端口段,则填10000:20000

iptables -t nat -A PREROUTING -p tcp --dport [要转发的端口段] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -A PREROUTING -p udp --dport [要转发的端口段] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -A POSTROUTING -p tcp -d [要转发的服务器IP] --dport [要转发的端口段] -j SNAT --to-source [本机IP]
iptables -t nat -A POSTROUTING -p udp -d [要转发的服务器IP] --dport [要转发的端口段] -j SNAT --to-source [本机IP]
最后保存规则和重启iptables服务
/etc/init.d/iptables save && /etc/init.d/iptables restart

CentOS 7.0以上使用的是firewall:

开启伪装IP

firewall-cmd --permanent --add-masquerade

配置端口转发,将到达本机的1234端口的访问转发到另一台服务器的22端口

firewall-cmd --permanent --add-forward-port=port=1234:proto=tcp:toaddr=192.168.172.131:toport=22

重载生效

firewall-cmd --reload

 

 

转载请注明:创富者 » Linux 端口转发用哪个工具好?

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址