1、配置文件:
[root@xuegod63 ~]# ls/etc/sysconfig/iptables
2、啓動:
[root@xuegod63~]# /etc/init.d/iptables start
iptables:應用防火牆規則:
[root@xuegod63 ~]# chkconfig iptables on 開機自動啓動。
[root@xuegod63 ~]# iptables –nL 查看防火牆規則。
3、使用規則:
iptables -t 要操作的表
操作命令:
-A <鏈名> 追加一條規則(放到最後)
例:[root@xuegod63 ~]# iptables-t filter -A INPUT -j DROP 禁止所有進入
-D <鏈名> <規則號碼|具體內容規則> 刪除一條規則。
例:
[root@xuegod63~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 192.168.1.72 0.0.0.0/0
DROP all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@xuegod63~]# iptables -D INPUT 2 按號碼刪除
[root@xuegod63~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 192.168.1.72 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-P<鏈名> <動作>policy 設置某個鏈的默認規則
[root@xuegod63 ~]#iptables -P INPUT DROP
-F 刪除規則
例:
[root@xuegod63 ~]#iptables -F INPUT
[root@xuegod63 ~]#iptables -t nat -F
[root@xuegod63 ~]#iptables -t nat -F PREROUTING
4、匹配條件:
流入、流出接口(-i、-o)
來源、目的地址(-s、-d)
協議類型 (-p)
來源、目的端口(--sport、--dport)
-i eth0
匹配是否從網絡接口 eth0進來
-s 192.168.0.1 匹配來自 192.168.0.1 的數據包
-s192.168.1.0/24 匹配來自 192.168.1.0/24網絡的數據包
-d 202.106.0.20 匹配去往 202.106.0.20 的數據包
-d202.106.0.0/16 匹配去往 202.106.0.0/16網絡的數據包
-p<匹配協議類型>
可以是 TCP、UDP、ICMP等,也可爲空
例如:
-p tcp
-p udp
例如:禁用DNS
[root@xuegod63 ~]#iptables -A INPUT -p udp --dport 53 -j DROP
限制該網段訪問服務器:
[root@xuegod63 ~]#iptables -A INPUT -s 10.1.0.0/24 -d 172.17.0.0/16 -j DROP
限制該地址訪問百度80端口
[root@xuegod63 ~]#iptables -A INPUT -s 192.168.0.1 -d www.baidu.com -p tcp --dport 80 -j ACCEPT
動作(處理方式)
ACCEPT 接收
DROP 丟棄 不給對端任何迴應
REJECT 拒絕 拒絕後,給對端一個迴應
SNAT 源地址轉換
DNAT 目的地址轉換
例:
將內網192.168.0.0/24 的原地址修改爲 公網IP地址:1.1.1.1
[root@xuegod63~]# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to 1.1.1.1
從eth0接口進入訪問80端口的數據包,轉到192.168.0.1
[root@xuegod63 ~]#iptables -t nat -A PREROUTING -i eth0 -p tcp --dpor 80 -j DNAT --to 192.168.0.1
按包狀態匹配 (state)
-mstate --state 狀態
狀態:NEW、RELATED、ESTABLISHED、INVALID
NEW:有別於 tcp 的 syn
ESTABLISHED:連接態
RELATED:衍生態,與 conntrack關聯(FTP)
INVALID:不能被識別屬於哪個連接或沒有任何狀態
實戰1:使用iptables防火牆保護公司web服務器。
具體配置如下:
web服務器:xuegod63
客戶端:xuegod64
配置xuegod63防火牆
[root@xuegod63 ~]#iptables -A INPUT -i lo -j ACCEPT
[root@xuegod63 ~]#iptables -A INPUT -p tcp -m multiport --dports 22,80 -j ACCEPT
或者
[root@xuegod63 ~]#iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@xuegod63 ~]#iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@xuegod63 ~]#iptables -P INPUT DROP
[root@xuegod63 ~]#service httpd start
[root@xuegod63 ~]#yum -y install vsftpd
[root@xuegod63 ~]#service vsftpd start
測試機ftp://192.168.1.63/無法訪問