?
?
一、Netfilter 簡介
(1)?Netfilter 是 Linux 內置的一種防火墻機制,我們一般也稱之為數據包過濾機制,而 iptables 只是操作 netfilter 的一個命令行工具
(2) Netfilter 是 Linux CentOS 6 內置的防火墻機制,Firewall 是 Linux CentOS 7 內置的防火墻機制,如果想在 CentOS 7 中使用 netfilter 而不是 firewall,操作如下
?
[root@MongoDB ~]# systemctl stop firewalld.service // 停止firewalld [root@MongoDB ~]# systemctl disable firewalld.service // 禁止firewall開機啟動 [root@MongoDB ~]# yum install iptables-services -y [root@MongoDB ~]# systemctl start iptables.service //啟動防火墻 [root@MongoDB ~]# systemctl enable iptables.service // 設置防火墻開機啟動
?
?
?
?
?
centos7 iptables服務
1.查看iptables服務狀態
[root@MongoDB ~]#systemctl status iptables
?
?
systemctl start iptables // 啟動iptables服務 systemctl restart iptables // 重啟iptables服務 systemctl stop iptables // 關閉iptables服務
?
centos6 iptables 服務
service iptables start // 啟動 iptables服務 service iptables restart // 重啟iptables服務 service iptables stop // 關閉iptables服務 service iptables status // 查看iptables服務狀態
?
/etc/init.d/iptables stop // 關閉iptables服務 /etc/init.d/iptables status // 查看iptables狀態
?
2.查看規則,默認查看filter表的規則
iptables -nvL
針對INPUT鏈 默認規則ACCEPT通過
iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) // 針對INPUT鏈 默認規則 // INPUT鏈 規則pkts bytes target prot opt in out source destination 7589 858K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 42 2520 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 2 104 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:220 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:27017 30806 3205K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited// FORWARD鏈 默認規則 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited// OUTPUT鏈 默認規則 Chain OUTPUT (policy ACCEPT 6732 packets, 950K bytes)pkts bytes target prot opt in out source destination
?
二、iptables 常見用法:
iptables -F # 清空規則,默認清空filter表的規則 iptables -X # 刪除用戶自定義規則 iptables -Z # 清空鏈的計數器 service iptables save # 保存規則,會把當前規則保存到/etc/sysconfig/iptables iptables-save > my.ipt # 備份規則,這里指定備份到my.ipt文件 iptables-restore < my.ipt # 恢復規則,這里指定使用my.ipt文件進行恢復
?
?
清空所有的規則,只留下默認規則
iptables -F??清空規則,默認清空filter表的規則
只留下默認規則 默認規則都是ACCEPT動作
[root@MongoDB ~]# iptables -F [root@MongoDB ~]# iptables -nvL Chain INPUT (policy ACCEPT 6 packets, 480 bytes)pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 6 packets, 528 bytes)pkts bytes target prot opt in out source destination
iptables -X? 刪除用戶定義規則
iptables -Z 清空鏈的計數器
?
?
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 放通80端口 iptables -A INPUT -p tcp --dport 22 -j DROP # 禁用22端口 iptables -A INPUT -p tcp -m multiport --dport 80,843,443 -j ACCEPT # 放通多個端口 iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT # 只允許某個IP訪問22端口 iptables -A INPUT -s 192.168.1.1/32 -p tcp -m multiport --dport 3873,4507,3306 -j ACCEPT # 允許某個IP訪問多個端口
?
?添加一條規則 到filter表 允許8080端口
[root@MongoDB ~]# iptables -t filter -A INPUT -p tcp --dport 8080 -j ACCEPT
刪除一條規則
先執行 iptables -nvL --line-numbers 查看規則的序列號
[root@MongoDB ~]# iptables -nvL --line-numbers Chain INPUT (policy ACCEPT 93 packets, 7775 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 59 packets, 6900 bytes) num pkts bytes target prot opt in out source destination
-D 刪除規則
然后執行 iptables -D INPUT <number> 刪除規則
[root@MongoDB ~]# iptables -D INPUT 1 [root@MongoDB ~]# [root@MongoDB ~]# iptables -nvL --line-numbers Chain INPUT (policy ACCEPT 14 packets, 1020 bytes) num pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 6 packets, 728 bytes) num pkts bytes target prot opt in out source destination
?
?
-A # 添加規則,默認添加到最后一條規則 -I # 插入規則,默認插入到第一條規則 -D # 刪除規則,先執行 iptables -nvL --line-numbers 查看規則的序列號,然后執行 iptables -D INPUT <number> 刪除規則 -n # 數字 -s # 用于指定源IP地址,用法如:iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT -d # 用于指定目標IP地址,用法如:iptables -A INPUT -d 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT -p # 用于指定檢查哪個協議,用法如:iptables -A INPUT -p tcp --dport 80 -j ACCEPT -i # 用于指定入口網卡,用法如:iptables -A INPUT -i eth0 -j ACCEPT -o # 用于指定出口網卡,用法如:iptables -A FORWARD -o eth0 -j ACCEPT -j # 用于指定要進行的處理動作,ACCEPT表示接受數據包進來 DROP直接丟棄數據包 用法如:iptables -A INPUT -p tcp --dport 80 -j ACCEPT -P # 用于設置默認規則,用法如:iptables -P INPUT DROP -t # 用于指定操作哪張表,用法如:iptables -t nat -nvL , iptables -t filter ,如果沒有指定默認是filter表 -Z # 用于清空計數器,用法如:iptables -Z -L # 用于列出規則鏈中的所有規則 --sport # 用于指定源端口,用法如:iptables -A INPUT -p tcp --sport 1234 -j ACCEPT --dport # 用于指定目標端口,用法如:iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT ! # 取反
?
?
?
?
?