厦门服务器租用>BGP服务器>通过iptables做端口映射

通过iptables做端口映射

发布时间:2023/6/7 11:05:54

通过iptables做端口映射

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

1.安装iptables——使用下面命令进行安装iptables服务:

yum -y install iptables-services

通过iptables做端口映射-3160

2.运行下列命令启动iptables服务:

systemctl start iptables

3.运行下列命令查看iptables服务运行状态,状态为“active”表示iptables服务已在运行:

service iptables status

通过iptables做端口映射-3161

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

systemctl enable iptables

通过iptables做端口映射-3162

5.echo net.ipv4.ip_forward=1>>/etc/sysctl.conf //开启系统路由模式功能

sysctl -p //使内核修改生效

通过iptables做端口映射-3163

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

向外映射

通过iptables做端口映射-3164

8.保存添加的规则:

iptables-save > /etc/sysconfig/iptables


在线客服
微信公众号
免费拨打400-1886560
免费拨打0592-5580190 免费拨打 400-1886560 或 0592-5580190
返回顶部
返回头部 返回顶部