iptables是基於內核的防火牆,內置filter,nat,mangle三張表
- filter表:過濾數據包(input,output,forward)
- nat表:網絡地址轉換(prerouting,postrouting)
- mangle表:修改數據包內容
5個鏈:
- input:匹配目標ip是本機的包
- output:一般不在此鏈做配置
- forward:匹配經過本機數據包
- prerouting:修改目的地址DNAT
- postrouting:修改源地址SNAT
防火牆轉發:prerouting--------------forward----------------postrouting
發給防火牆:prerouting--------------input-------------output------------postrouting
- 查看防火牆是否安裝:rpm -qa iptables
- 配置文件位置:/etc/sysconfig/iptables
- 啓動:/etc/init.d/iptables start
- 查看是否開機啓動:chkconfig --list iptables
iptable使用規則:
iptables -t 表名 管理選項 鏈名 匹配條件 -j 動作
- iptables -L 查看規則
- iptables -D INPUT 1 刪除INPUT表裏第一條規則
- iptables -D INPUT -s 192.168.1.1 -j DROP 刪除INPUT鏈表裏源地址爲192.168.1.1的數據包
- iptables -P INPUT DROP 修改INPUT鏈表的默認規則
- iptables -F INPUT 清空INPUT鏈表裏全部規則
- iptables -F 清空所有鏈表規則
- iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT – to 10.1.12.4 將源地址爲192.168.0.0/24網段轉爲10.1.12.4
- iptables -t nat -A POSTROUTING -i eth0 -p tcp – dport 80 -j DNAT --to 192.168.51.1:80 將從eth0口收到的要訪問80端口的tcp協議數據包轉到192.168.51.1的80端口
包匹配狀態:
- NEW:剛建立連接
- ESTABLISH:已建立連接
- RELATED:建立連接後關聯協議,如FTP
- INVALID:不能識別屬於哪個連接
包的使用:
- iptables -A INPUT -m state --state RELATED,ESTABLISH -j ACCEPT 接受所有
- iptables -A INPUT -m mac --mac-source xx:xx:xx:xx:xx -j DROP 阻斷來自指定mac的數據包
- iptables -A FORWARD -d 192.168.0.1 -m limit --limit 50/s -j ACCEPT 接受目的地址爲192.168.0.1的數據包,每秒鐘50個
多端口匹配:
- iptables -A INPUT -p tcp -m multiport --dports 21,22,80 -j ACCEPT 接受目的端口爲21,22,80的包
🎈注意事項:
- 配置後立即生效不需要重啓
- REJECT:拒絕,有迴應
- DROP:丟棄,沒有迴應