iptables防火墻具有4表5鏈,4表分別是filter表、nat表、raw表、mangle表,5鏈分別是INPUT鏈、OUTPUT鏈、FORWARD鏈、PREROUTING鏈、POSTROUTING鏈。防火墻規(guī)則要求寫在特定表的特定鏈中。


關(guān)閉firewalld服務(wù)器,啟動(dòng)iptables服務(wù)
systemctl stop firewalld.service
systemctl disable firewalld.service
iptables命令的基本使用方法
iptables [-t 表名] 選項(xiàng) [鏈名] [條件] [-j 目標(biāo)操作]
示例:
iptables -t filter -I INPUT -p icmp -j REJECT
#注意事項(xiàng)與規(guī)律:
#可以不指定表,默認(rèn)為filter表
#可以不指定鏈,默認(rèn)為對應(yīng)表的所有鏈
#按順序匹配,匹配即停止,如果沒有找到匹配條件,則執(zhí)行防火墻默認(rèn)規(guī)則
#選項(xiàng)/鏈名/目標(biāo)操作用大寫字母,其余都小寫
#目標(biāo)操作:
# ACCEPT:允許通過/放行
# DROP:直接丟棄,不給出任何回應(yīng)
# REJECT:拒絕通過,必要時(shí)會給出提示
# LOG:記錄日志,然后傳給下一條規(guī)則
iptables常用的管理選項(xiàng):

一、iptables命令的使用
創(chuàng)建規(guī)則
iptables -F #清空所有規(guī)則:
追加規(guī)則至filter表中的INPUT鏈的末尾,允許任何人使用TCP協(xié)議訪問本機(jī)
iptables -t filter -A INPUT -p tcp -j ACCEPT
插入規(guī)則至filter表中的INPUT鏈的開頭,允許任何人使用UDP協(xié)議訪問本機(jī)
iptables -I INPUT -p udp -j ACCEPT
插入規(guī)則至filter表中的INPUT鏈的第2行,允許任何人使用ICMP協(xié)議訪問本機(jī)
iptables -I INPUT 2 -p icmp -j ACCEPT
查看iptables防火墻規(guī)則
iptables -nL INPUT #僅查看INPUT鏈的規(guī)則
iptables -L INPUT --line-numbers #查看規(guī)則,顯示行號
刪除規(guī)則,清空所有規(guī)則:
iptables -D INPUT 3 #刪除filter表中INPUT鏈的第3條規(guī)則
iptables -F #清空filter表中所有鏈的防火墻規(guī)則
iptables -t nat -F #清空nat表中所有鏈的防火墻規(guī)則
iptables -t mangle -F #清空mangle表中所有鏈的防火墻規(guī)則
iptables -t raw -F #清空raw表中所有鏈的防火墻規(guī)則
設(shè)置防火墻默認(rèn)規(guī)則
設(shè)置INPUT鏈默認(rèn)規(guī)則為DROP:
iptables -t filter -P INPUT DROP
iptables -nL
Chain INPUT (policy DROP)
… …
設(shè)置INPUT鏈默認(rèn)規(guī)則為ACCEPT
iptables -t filter -P INPUT ACCEPT
二、filter過濾和轉(zhuǎn)發(fā)控制
主機(jī)型防火墻,主要保護(hù)的是服務(wù)器本機(jī)(過濾威脅本機(jī)的數(shù)據(jù)包)。
網(wǎng)絡(luò)防火墻,主要保護(hù)的是防火墻后面的其他服務(wù)器,如web服務(wù)器、FTP服務(wù)器等。
iptables防火墻可以根據(jù)很多規(guī)則進(jìn)行過濾行為

1)主機(jī)型防火墻
iptables -I INPUT -p tcp --dport 80 -j REJECT
iptables -I INPUT -s 192.168.2.100 -j REJECT
iptables -I INPUT -d 192.168.2.5 -p tcp --dport 80 -j REJECT
iptables -I INPUT -i eth0 -p tcp --dport 80 -j REJECT
丟棄192.168.2.0/24網(wǎng)絡(luò)中所有主機(jī)發(fā)送給本機(jī)的所有數(shù)據(jù)包:
iptables -A INPUT -s 192.168.2.0/24 -j DROP
拒絕114.212.33.12使用tcp協(xié)議遠(yuǎn)程連接本機(jī)ssh(22端口):
iptables -A INPUT -s 114.212.33.12 -p tcp --dport 22 -j REJECT
2)網(wǎng)絡(luò)型防火墻
要把proxy主機(jī)的路由轉(zhuǎn)發(fā)功能打開

1)client主機(jī)配置IP、添加網(wǎng)關(guān)(網(wǎng)卡名稱僅供參考
注意:如果client主機(jī)有2網(wǎng)段IP的網(wǎng)卡,必須要關(guān)閉該網(wǎng)卡
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.4.10/24 autoconnect yes
nmcli connection modify eth0 ipv4.gateway 192.168.4.5
nmcli connection up eth0
iptables -F
2)proxy主機(jī)配置IP、添加網(wǎng)關(guān)、開啟路由轉(zhuǎn)發(fā)(網(wǎng)卡名稱僅供參考)
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.4.5/24 autoconnect yes
nmcli connection up eth0
nmcli connection modify eth1 ipv4.method manual ipv4.addresses 192.168.2.5/24 autoconnect yes
nmcli connection up eth1
iptables -F
Linux內(nèi)核默認(rèn)支持軟路由功能,通過修改內(nèi)核參數(shù)即可開啟或關(guān)閉路由轉(zhuǎn)發(fā)功能。
echo 0 > /proc/sys/net/ipv4/ip_forward #關(guān)閉路由轉(zhuǎn)發(fā)
echo 1 > /proc/sys/net/ipv4/ip_forward #開啟路由轉(zhuǎn)發(fā)
#注意以上操作僅當(dāng)前有效,計(jì)算機(jī)重啟后無效
#修改/etc/sysctl.conf配置文件,可以實(shí)現(xiàn)永久有效規(guī)則
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
3)web1主機(jī)配置IP、添加網(wǎng)關(guān)(網(wǎng)卡名稱僅供參考)
注意:如果web1主機(jī)有4網(wǎng)段IP的網(wǎng)卡,必須要關(guān)閉該網(wǎng)卡
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.2.100/24 autoconnect yes
nmcli connection modify eth0 ipv4.gateway 192.168.2.5
nmcli connection up eth0
4)確認(rèn)不同網(wǎng)絡(luò)的聯(lián)通性
ping 192.168.2.100
ping 192.168.4.10
5)在web1主機(jī)上啟動(dòng)http服務(wù)
yum -y install httpd
echo "test page" > /var/www/html/index.html
systemctl restart httpd
沒有防火墻的情況下client訪問web服務(wù)(成功)
curl http://192.168.2.100
設(shè)置proxy主機(jī)的防火墻規(guī)則,保護(hù)防火墻后面的Web服務(wù)器
iptables -I FORWARD -s 192.168.4.10 -p tcp --dport 80 -j DROP
設(shè)置完防火墻規(guī)則后,再次使用client客戶端訪問測試效果(失敗)
curl http://192.168.2.100
三、禁ping的相關(guān)策略
ping的流程(A主機(jī)pingB主機(jī)):

禁止其他主機(jī)ping本機(jī),允許本機(jī)ping其他主機(jī),僅禁止入站的ping請求,不拒絕入站的ping回應(yīng)包。
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
注意:關(guān)于ICMP的類型,可以參考help幫助,參考命令如下:
iptables -p icmp --help
所有iptables規(guī)則都是臨時(shí)規(guī)則,如果需要永久保留規(guī)則需要執(zhí)行如下命令:
安裝iptables-services并啟動(dòng)服務(wù),保存防火墻規(guī)則。
yum -y install iptables-services
systemctl start iptables.service
systemctl enable iptables.service
service iptables save