0
点赞
收藏
分享

微信扫一扫

CentOS7使用iptables搭建nat网关服务器【拓展延伸】

花明 2022-09-04 阅读 379


一、前言

一般情况下,SNAT和DNAT使用参考前篇文章(​​https://blog.51cto.com/hatech/5301589​​)即可满足需求,本文是在该基础上进行拓展延伸。

二、目标

假设有一个vpc网段(172.16.0.0/24,网关172.16.0.1),网段内有一个DHCP服务器(172.16.0.1),用于地址分配,有一个iptables网关服务器(内网172.16.0.200)用于代理,实现该网段内系统的SNAT和DNAT需求。

三、拓扑图


四、DHCP Server配置

[root@dhcp ~]# cat /etc/dhcp/dhcpd.conf

#

# DHCP Server Configuration file.

#   see /usr/share/doc/dhcp*/dhcpd.conf.example

#   see dhcpd.conf(5) man page

#

subnet 172.16.0.0

netmask 255.255.255.0

{

range 172.16.0.200 172.16.0.250;

option routers 172.16.0.1;

option domain-name-servers 114.114.114.114;

}


subnet 192.168.80.0

netmask 255.255.255.0

{

}


五、配置iptables网关服务器

1、首先关闭centos7自带的firewalld防火墙

# systemctl disable firewalld

# systemctl stop firewalld

2、配置路由转发

# vi /etc/sysctl.conf  

net.ipv4.ip_forward = 1

# sysctl -p

3、安装iptables管理工具

# yum install iptables-services

# systemctl enable iptables

# systemctl restart iptables

4、查看路由

[root@natgw ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

0.0.0.0         192.168.80.2    0.0.0.0         UG    0      0        0 ens33

0.0.0.0         172.16.0.1      0.0.0.0         UG    0      0        0 ens37

172.16.0.0      0.0.0.0         255.255.255.0   U     0      0        0 ens37

192.168.80.0    0.0.0.0         255.255.255.0   U     0      0        0 ens33


六、添加规则

1、添加DNAT规则

# iptables -t nat -A POSTROUTING -j MASQUERADE

# iptables -t nat -A PREROUTING -i ens33 -d 192.168.80.131 -p tcp --dport 2222 -j DNAT --to-destination 172.16.0.200:22


2、添加SNAT规则

# iptables -t nat -I POSTROUTING -s 172.16.0.200 -j MASQUERADE

注意,添加完对应主机的SNAT规则之后,需要在该主机内将nat网关服务器的内网IP添加为该主机的默认网关,否则无法连接外网。

[root@vm1 ~]# route add default gw 172.16.0.203

[root@vm1 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

0.0.0.0         172.16.0.203    0.0.0.0         UG    0      0        0 ens33

0.0.0.0         172.16.0.1      0.0.0.0         UG    0      0        0 ens33

172.16.0.0      0.0.0.0         255.255.255.0   U     0      0        0 ens33


3、查看规则

# iptables -nvL -t nat

Chain PREROUTING (policy ACCEPT 8 packets, 706 bytes)

pkts bytes target     prot opt in     out     source               destination

   4   208 DNAT       tcp  --  ens33  *       0.0.0.0/0            192.168.80.131       tcp dpt:2222 to:172.16.0.200:22


Chain INPUT (policy ACCEPT 1 packets, 229 bytes)

pkts bytes target     prot opt in     out     source               destination


Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination


Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination

   7   477 MASQUERADE  all  --  *      *       172.16.0.200         0.0.0.0/0

   8   750 MASQUERADE  all  --  *      *       0.0.0.0/0            0.0.0.0/0


附:

1 将内网控制平面(dhcp和nat网关)解藕,效率提升,可用性提高,操作简便,架构明了

2 nat网关不是必需的,也就是如果没有SNAT和DNAT需求,就不用创建,如果有,可以单独创建

3 如果有snat需求,需要在主机中指定网关服务器的内网IP作为该主机系统的网关

4 可控且灵活,有些云内的nat网关就是这样实现的,供参考

5 nat网关服务器可以2台作为高可用

6 不影响网段内默认网关IP








举报

相关推荐

0 条评论