通过iptables做端口映射
通过iptables做端口映射
Iptables是Linux内核集成的IP信息包过滤系统,不同版本的linux系统对于iptables的安装、配置也有所区别,本文以Centos7系统为例,通过对iptables策略进行修改配置,实现对于指定IP地址的端口映射及IP地址转换(NAT)。具体操作如下:
1.安装iptables——使用下面命令进行安装iptables服务:
yum -y install iptables-services

2.运行下列命令启动iptables服务:
systemctl start iptables
3.运行下列命令查看iptables服务运行状态,状态为“active”表示iptables服务已在运行:
service iptables status

4.设置iptables的开机自启动:
systemctl enable iptables

5.echo net.ipv4.ip_forward=1>>/etc/sysctl.conf //开启系统路由模式功能
sysctl -p //使内核修改生效

6.用DNAT做端口映射:
例如:
iptables -t nat -A PREROUTING -p tcp –m tcp --dport 12345 -j DNAT --to-destination 192.168.10.26:3389
//将访问本服务器TCP12345接口的流量映射到192.168.10.26的3389端口
7.用SNAT作源地址转换,以使回应包能正确返回:
例如:
iptables -t nat -A POSTROUTING -d 192.168.10.26/32 -p tcp –m tcp --dport 3389 -j SNAT --to 192.168.233.142 //主机192.168.10.26回应访问其3389端口的流量包,经192.168.233.142
向外映射

8.保存添加的规则:
iptables-save > /etc/sysconfig/iptables