新聞中心
本篇內(nèi)容主要講解“l(fā)inux做防火墻時如何運用iptables進行NAT地址轉(zhuǎn)換”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“l(fā)inux做防火墻時如何運用iptables進行NAT地址轉(zhuǎn)換”吧!
前言:簡述地址映射
DNAT地址轉(zhuǎn)換,客戶機訪問服務(wù)端
SNAT地址轉(zhuǎn)化,服務(wù)端訪問客戶機
圖示
注意:配置網(wǎng)卡要全都配置為僅主機模式
一:防火墻配置:兩個網(wǎng)卡,都是僅主機模式,配置靜態(tài)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]# ifconfig ens33: flags=4163mtu 1500 inet 192.168.10.1 netmask 255.255.255.0 broadcast 192.168.10.255 ens36: flags=4163 mtu 1500 inet 12.0.0.1 netmask 255.255.255.0 broadcast 12.0.0.255
二:web服務(wù)端修改網(wǎng)卡,搭建web服務(wù),測試是否與linux防火墻連通(臨時關(guān)閉firewalld)
web服務(wù)端:
僅主機模式,修改網(wǎng)卡
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 [root@localhost ~]# systemctl restart network [root@localhost ~]# ifconfig ens33: flags=4163mtu 1500 inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
ping 防火墻驗證是否互聯(lián)互通
[root@localhost ~]# ping 192.168.10.1 PING 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 ms 64 bytes from 192.168.10.1: icmp_seq=2 ttl=64 time=0.903 ms 64 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 2003ms rtt min/avg/max/mdev = 0.903/1.325/2.090/0.543 ms [root@localhost ~]#
搭建web服務(wù)
[root@localhost ~]# yum install httpd -y [root@localhost ~]# vim /etc/httpd/conf/httpd.conf 41 Listen 192.168.10.10:80 42 #Listen 80 95 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 httpd tcp 0 0 192.168.10.10:80 0.0.0.0:* LISTEN 9437/httpd
三 :客戶端配置地址,測試與linux防火墻端是否連通,測試與web服務(wù)是否連通
客戶端配置ip地址:
此時可以臨時把防火墻主機的防火墻給關(guān)掉,測試是否互聯(lián)互通,然后再開啟
C:\Users\GSY>ipconfig Windows IP 配置 以太網(wǎng)適配器 Ethernet0: 連接特定的 DNS 后綴 . . . . . . . : 本地鏈接 IPv6 地址. . . . . . . . : fe80::3407:c668:fbd0:9316%3 IPv4 地址 . . . . . . . . . . . . : 12.0.0.12 子網(wǎng)掩碼 . . . . . . . . . . . . : 255.255.255.0 默認網(wǎng)關(guān). . . . . . . . . . . . . : 12.0.0.1 C:\Users\GSY>ping 12.0.0.1 正在 Ping 12.0.0.1 具有 32 字節(jié)的數(shù)據(jù): 來自 12.0.0.1 的回復(fù): 字節(jié)=32 時間=7ms TTL=64 來自 12.0.0.1 的回復(fù): 字節(jié)=32 時間<1ms TTL=64 來自 12.0.0.1 的回復(fù): 字節(jié)=32 時間<1ms TTL=64 來自 12.0.0.1 的回復(fù): 字節(jié)=32 時間<1ms TTL=64 12.0.0.1 的 Ping 統(tǒng)計信息: 數(shù)據(jù)包: 已發(fā)送 = 4,已接收 = 4,丟失 = 0 (0% 丟失), 往返行程的估計時間(以毫秒為單位): 最短 = 0ms,最長 = 7ms,平均 = 1ms C:\Users\GSY>ping 192.168.10.1 正在 Ping 192.168.10.1 具有 32 字節(jié)的數(shù)據(jù): 來自 192.168.10.1 的回復(fù): 字節(jié)=32 時間<1ms TTL=64 來自 192.168.10.1 的回復(fù): 字節(jié)=32 時間<1ms TTL=64 來自 192.168.10.1 的回復(fù): 字節(jié)=32 時間<1ms TTL=64 來自 192.168.10.1 的回復(fù): 字節(jié)=32 時間<1ms TTL=64 192.168.10.1 的 Ping 統(tǒng)計信息: 數(shù)據(jù)包: 已發(fā)送 = 4,已接收 = 4,丟失 = 0 (0% 丟失), 往返行程的估計時間(以毫秒為單位): 最短 = 0ms,最長 = 0ms,平均 = 0ms C:\Users\GSY>ping 192.168.10.10 正在 Ping 192.168.10.10 具有 32 字節(jié)的數(shù)據(jù): 請求超時。 請求超時。 192.168.10.10 的 Ping 統(tǒng)計信息: 數(shù)據(jù)包: 已發(fā)送 = 2,已接收 = 0,丟失 = 2 (100% 丟失), Control-C ^C
可以發(fā)現(xiàn)此時客戶機無法訪問服務(wù)端
四:再次配置防火墻(重點):
4.1 清理規(guī)則
[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 -p net.ipv4.ip_forward = 1
備注:httpd端也可以執(zhí)行以下兩條命令
[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]# ls anaconda gdm qemu-ga vmware-vgauthsvc.log.0 audit glusterfs rhsm vmware-vmsvc.log boot.log grubby_prune_debug sa vmware-vmusr.log btmp lastlog samba wpa_supplicant.log chrony libvirt secure wtmp cron maillog speech-dispatcher Xorg.0.log cups messages spooler Xorg.0.log.old dmesg ntpstats sssd Xorg.9.log dmesg.old pluto tallylog yum.log firewalld ppp tuned
4.2 接下來開始做映射,思路如下
從外網(wǎng)訪問內(nèi)網(wǎng),先轉(zhuǎn)換,再轉(zhuǎn)發(fā)
從內(nèi)網(wǎng)訪問外網(wǎng)時,先轉(zhuǎn)發(fā),再轉(zhuǎn)換
dnat是在路由前轉(zhuǎn)換地址 prerouting
snat是在路由后轉(zhuǎn)換地址 postrouting
-d 要轉(zhuǎn)換的目標地址名
-p 指定協(xié)議
--dport 80端口
-i ens36端口進
-j DNAT操作
--to-destination 轉(zhuǎn)換后的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
五 :此時,客戶端刷新網(wǎng)頁,即可進入
到此,相信大家對“l(fā)inux做防火墻時如何運用iptables進行NAT地址轉(zhuǎn)換”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文名稱:linux做防火墻時如何運用iptables進行NAT地址轉(zhuǎn)換-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://www.ef60e0e.cn/article/dpcppd.html