Using Iptables

This guide outlines how to enable port mapping using iptables, including methods to allow packet forwarding and setting up DNAT and SNAT mappings.

1. 使用 iptables 进行端口映射12

1.1 第一步 : 打开端口映射功能 :

1.1.1 方法一 : (允许数据包转发)

1
sudo echo '1' > /proc/sys/net/ipv4/ip_forward

1.1.2 方法二 :

1
vim /etc/sysctl.conf

;net.ipv4.ip_forward = 0 这一行的注视去掉 , 并将 0 改为 1

修改后的结果为 :

1
net.ipv4.ip_forward = 1

1.2 第二步 : 进行映射 :

DNAT

1
iptables -t nat -A PREROUTING -d 本机IP -p tcp --dport 本机端口 -j DNAT --to-destination 目标机IP:目标机端口

SNAT

1
iptables -t nat -A PREROUTING -d 本机IP -p tcp --dport 本机端口 -j SNAT --to-destination 目标机IP:目标机端口

2. 参考:

使用 Hugo 构建
主题 StackJimmy 设计