iptables详解以及企业常用案例
iptables詳解以及企業(yè)常用案例
??? iptables采用netfilter網(wǎng)絡(luò)架構(gòu)實(shí)現(xiàn)包過(guò)濾防火墻,可以實(shí)現(xiàn)硬件防火墻的常用功能,亦可以在應(yīng)用方案中作為硬件防火墻的替代品.在本文檔里我們將詳細(xì)介紹iptables的功能,以及剖析企業(yè)常用案例
一、iptables的工作原理
1、iptables的3個(gè)表
??? iptables共三個(gè)表,分別是filter,nat,mangle。我們通過(guò)編輯這三個(gè)表可以實(shí)現(xiàn)不同的功能。其中fileter主要是為了控制外部PC對(duì)本服務(wù)器的訪問(wèn),比如在服務(wù)器上作了web服務(wù),想只允許某網(wǎng)段才能訪問(wèn)就是利用了fileter進(jìn)行訪問(wèn)控制。Nat表主要用在網(wǎng)關(guān)服務(wù)器上,可以實(shí)現(xiàn)nat,路由等功能。Mangle不常用,此教程不做介紹。
2、每個(gè)表的規(guī)則鏈
iptables默認(rèn)的表:
filter(包含INPUT,FORWARD,OUTPUT)
Nat(包含PREROUTING,POSTROUTING,OUTPUT)
Mangle (包含INPUT,FORWARD,OUTPUT,PREROUTING ,POSTROUTING)
其中更詳細(xì)如下圖所示:
??? 注:filter的OUTPUT規(guī)則鏈和nat的OUTPUT沒(méi)有聯(lián)系,即不同表的規(guī)則鏈即使名字相同也毫無(wú)關(guān)系。
我們通過(guò)添加刪除修改規(guī)則來(lái)改變規(guī)則鏈,具體如何修改我們將在下文進(jìn)行詳細(xì)介紹。
二、iptables配置
1、iptables的常用命令
安裝服務(wù)所需要的rpm包iptables-*-i386.rpm
安裝服務(wù)rpm –ivh iptables*
啟動(dòng)iptables服務(wù)service iptables start
關(guān)閉iptables服務(wù)service iptables stop
查看iptables服務(wù)狀態(tài)service iptables status
或者通過(guò)腳本來(lái)進(jìn)行啟動(dòng)|關(guān)閉|查看:/etc/init.d/iptables {start|stop|status}
保存當(dāng)前設(shè)置到/etc/sysconfig/iptables:service iptables save
查看當(dāng)前表中規(guī)則iptables [-t 表名]–L????? //如果不指定表名將以filter查看,下同
清空表中規(guī)則iptables [-t 表名] –F
刪除自定義規(guī)則鏈iptables [-t 表名] –X
計(jì)數(shù)器歸零iptables –Z
2、iptables默認(rèn)規(guī)則鏈的默認(rèn)設(shè)置
iptables [-t 表名] –P 規(guī)則鏈 處理方式???????????
(注:處理方式常用有3種,丟棄,拒絕,接受允許,分別是DROP,REJECT,ACCEPT)
例:我們需要將filter的INPUT規(guī)則鏈的默認(rèn)設(shè)置為丟棄,則
iptables –t filter –P INPUT DROP
3、添加iptables規(guī)則
iptables [-t 表名] –A 規(guī)則鏈 –s 源地址 –d 目的地址 –dport 目的端口 –j 處理方式
例:我們接受外界PING
iptables –t filter –A INPUT –p icmp –j ACCEPT
附:iptables添加規(guī)則時(shí)所涉及的命令選項(xiàng)以及處理方式
添加規(guī)則的命令選項(xiàng);-i? 指定入口網(wǎng)卡,-o? 指定輸出網(wǎng)卡,-p? 指定數(shù)據(jù)包協(xié)議,如TCP,UDP,-s? 指定源地址,-sport? 指定源端口,-d? 指定目的地址,-dport? 指定目的端口,-j? 處理方式
其中處理方式有一下幾種,ACCEPT? 接受數(shù)據(jù)包,DROP? 丟棄數(shù)據(jù)包,REJECT? 拒絕,SNAT源地址轉(zhuǎn)換,DNAT? 目的地址轉(zhuǎn)換,MASQUERADE? IP偽裝,LOG? 日志功能。
三、企業(yè)常用案例
??? 前言:企業(yè)上一般使用腳本來(lái)配置iptables,這樣方便以后修改。
1、? 一臺(tái)web服務(wù)器對(duì)外提供http,htp服務(wù),禁止外來(lái)的其他訪問(wèn)。
腳本如下
#!/bin/bash
iptables –F
iptables –X
iptables –Z
iptables –t nat –F
iptables –t nat –X
iptables –t nat –Z
iptables –P INPUT DROP
iptables –P OUTPUT ACCEPT
iptables –P FORWARD ACCEPT
iptables –t nat –P PREROUTING ACCEPT
iptables –t nat –P POSTROUTING ACCEPT
iptables –t nat –P OUTPUT ACCEPT
iptables –A INPUT –p tcp –-dport 80 –j ACCEPT
iptables –A INPUT –p tcp –-dport 21 –j ACCEPT
service iptables save
執(zhí)行腳本,完成
2、? 企業(yè)有一臺(tái)linux網(wǎng)管,需要做NAT服務(wù),將192.168.1.0網(wǎng)段的IP修改成網(wǎng)卡ech0的IP
腳本如下
#!/bin/bash
echo "1" > /proc/sys/net/ipv4/ip_forward
modprobe ip_tables
modprobe ip_nat_ftp
modprobe ip_nat_irc
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_conntrack_irc
iptables -F
iptables -X
iptables -Z
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
service iptables save
執(zhí)行腳本,完成
轉(zhuǎn)載于:https://blog.51cto.com/koy1619/284578
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的iptables详解以及企业常用案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微信“小号”功能详解
- 下一篇: 话里话外:从“种房子”谈流程与制度的差别