千家信息网

linux做防火墙时如何运用iptables进行NAT地址转换

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本篇内容主要讲解"linux做防火墙时如何运用iptables进行NAT地址转换",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"linux做防火墙时如何运用
千家信息网最后更新 2026年11月02日linux做防火墙时如何运用iptables进行NAT地址转换

本篇内容主要讲解"linux做防火墙时如何运用iptables进行NAT地址转换",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"linux做防火墙时如何运用iptables进行NAT地址转换"吧!

前言:简述地址映射

DNAT地址转换,客户机访问服务端
SNAT地址转化,服务端访问客户机

图示


注意:配置网卡要全都配置为仅主机模式

一:防火墙配置:两个网卡,都是仅主机模式,配置静态ip

[root@localhost ~]# cd /etc/sysconfig/network-scripts/[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36[root@localhost network-scripts]# vim ifcfg-ens33[root@localhost network-scripts]# vim ifcfg-ens36[root@localhost network-scripts]# systemctl restart network[root@localhost network-scripts]# ifconfigens33: flags=4163  mtu 1500        inet 192.168.10.1  netmask 255.255.255.0  broadcast 192.168.10.255ens36: flags=4163  mtu 1500        inet 12.0.0.1  netmask 255.255.255.0  broadcast 12.0.0.255

二:web服务端修改网卡,搭建web服务,测试是否与linux防火墙连通(临时关闭firewalld)

web服务端:
仅主机模式,修改网卡

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 [root@localhost ~]# systemctl restart network[root@localhost ~]# ifconfigens33: flags=4163  mtu 1500        inet 192.168.10.10  netmask 255.255.255.0  broadcast 192.168.10.255

ping 防火墙验证是否互联互通

[root@localhost ~]# ping 192.168.10.1PING 192.168.10.1 (192.168.10.1) 56(84) bytes of data.64 bytes from 192.168.10.1: icmp_seq=1 ttl=64 time=2.09 ms64 bytes from 192.168.10.1: icmp_seq=2 ttl=64 time=0.903 ms64 bytes from 192.168.10.1: icmp_seq=3 ttl=64 time=0.984 ms^C--- 192.168.10.1 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2003msrtt min/avg/max/mdev = 0.903/1.325/2.090/0.543 ms[root@localhost ~]#

搭建web服务

[root@localhost ~]# yum install httpd -y[root@localhost ~]# vim /etc/httpd/conf/httpd.conf  41 Listen 192.168.10.10:80 42 #Listen 8095 ServerName www.kgc.com:80[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0[root@localhost ~]# systemctl start httpd[root@localhost ~]# netstat -natp | grep httpdtcp        0      0 192.168.10.10:80        0.0.0.0:*               LISTEN      9437/httpd

三 :客户端配置地址,测试与linux防火墙端是否连通,测试与web服务是否连通

客户端配置ip地址:
此时可以临时把防火墙主机的防火墙给关掉,测试是否互联互通,然后再开启

C:\Users\GSY>ipconfigWindows IP 配置以太网适配器 Ethernet0:   连接特定的 DNS 后缀 . . . . . . . :   本地链接 IPv6 地址. . . . . . . . : fe80::3407:c668:fbd0:9316%3   IPv4 地址 . . . . . . . . . . . . : 12.0.0.12   子网掩码  . . . . . . . . . . . . : 255.255.255.0   默认网关. . . . . . . . . . . . . : 12.0.0.1   C:\Users\GSY>ping 12.0.0.1正在 Ping 12.0.0.1 具有 32 字节的数据:来自 12.0.0.1 的回复: 字节=32 时间=7ms TTL=64来自 12.0.0.1 的回复: 字节=32 时间<1ms TTL=64来自 12.0.0.1 的回复: 字节=32 时间<1ms TTL=64来自 12.0.0.1 的回复: 字节=32 时间<1ms TTL=6412.0.0.1 的 Ping 统计信息:    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),往返行程的估计时间(以毫秒为单位):    最短 = 0ms,最长 = 7ms,平均 = 1ms    C:\Users\GSY>ping 192.168.10.1正在 Ping 192.168.10.1 具有 32 字节的数据:来自 192.168.10.1 的回复: 字节=32 时间<1ms TTL=64来自 192.168.10.1 的回复: 字节=32 时间<1ms TTL=64来自 192.168.10.1 的回复: 字节=32 时间<1ms TTL=64来自 192.168.10.1 的回复: 字节=32 时间<1ms TTL=64192.168.10.1 的 Ping 统计信息:    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),往返行程的估计时间(以毫秒为单位):    最短 = 0ms,最长 = 0ms,平均 = 0msC:\Users\GSY>ping 192.168.10.10正在 Ping 192.168.10.10 具有 32 字节的数据:请求超时。请求超时。192.168.10.10 的 Ping 统计信息:    数据包: 已发送 = 2,已接收 = 0,丢失 = 2 (100% 丢失),Control-C^C

可以发现此时客户机无法访问服务端

四:再次配置防火墙(重点):

4.1 清理规则

[root@localhost network-scripts]# systemctl stop firewalld.service [root@localhost network-scripts]# setenforce 0[root@localhost network-scripts]# setenforce 1[root@localhost network-scripts]# systemctl start firewalld.service [root@localhost network-scripts]# iptables -F[root@localhost network-scripts]# iptables -t nat  -F[root@localhost network-scripts]# vim /etc/sysctl.conf [root@localhost network-scripts]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf [root@localhost network-scripts]# tail -2 /etc/sysctl.conf # For more information, see sysctl.conf(5) and sysctl.d(5).net.ipv4.ip_forward=1[root@localhost log]# sysctl -pnet.ipv4.ip_forward = 1

备注:httpd端也可以执行以下两条命令

[root@localhost network-scripts]# iptables -F[root@localhost network-scripts]# iptables -t nat  -F

firewall也有日志文件

[root@localhost network-scripts]# cd /var/log[root@localhost log]# lsanaconda   gdm                 qemu-ga            vmware-vgauthsvc.log.0audit      glusterfs           rhsm               vmware-vmsvc.logboot.log   grubby_prune_debug  sa                 vmware-vmusr.logbtmp       lastlog             samba              wpa_supplicant.logchrony     libvirt             secure             wtmpcron       maillog             speech-dispatcher  Xorg.0.logcups       messages            spooler            Xorg.0.log.olddmesg      ntpstats            sssd               Xorg.9.logdmesg.old  pluto               tallylog           yum.logfirewalld  ppp                 tuned

4.2 接下来开始做映射,思路如下

从外网访问内网,先转换,再转发

从内网访问外网时,先转发,再转换

dnat是在路由前转换地址 prerouting

snat是在路由后转换地址 postrouting

-d 要转换的目标地址名

-p 指定协议

--dport 80端口

-i ens36端口进

-j DNAT操作

--to-destination 转换后的ip地址

[root@localhost log]# iptables -t nat -I PREROUTING -d 12.0.0.1 -p tcp --dport 80 -i ens36 -j DNAT --to-destination 192.168.10.10[root@localhost log]# iptables -t nat -I POSTROUTING -s 192.168.10.10 -o ens36 -j SNAT --to-source 12.0.0.1

五 :此时,客户端刷新网页,即可进入

到此,相信大家对"linux做防火墙时如何运用iptables进行NAT地址转换"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

地址 字节 防火墙 防火 时间 服务 配置 客户 主机 数据 网卡 测试 客户机 客户端 模式 正在 内容 是在 端口 路由 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 伟宏互联网科技 互联网佳科技有限公司 科技互联网企业 互联网科技财经 广州互联网络科技有限公司 重庆科技学院互联网 顺网科技2021年互联网排名 佩信高科技互联网 玖二柒互联网科技公司待遇 北京大学互联网科技怎么样 小米互联网科技园设置 彤创互联网科技有限公司 苏州五指互联网络科技有限 呼和浩特新华互联网科技学院 爱医互联网络科技 互联网金融科技元年 互联网金融科技公司的优势 上海创道互联网络科技有限公司 互联网科技品牌 高科技和互联网的现代国家 广东万易互联网科技有限公司惠州分公司 互联网科技金融公司 唐信互联网科技 大连 山东吉时兔互联网科技有限公司 互联网科技公司招聘 哈尔滨屹滨互联网科技有限公司 互联网数据科技专业 梅州GM互联网络科技有限公司 万物互联网智能科技 杭州科技互联网公司有哪些
0