防火墙filter表
环境准备
• client1:eth0 -> 192.168.4.10/24 网关:192.168.4.11
• proxy1:eth0 -> 192.168.2.5/24,eth1 -> 192.168.4.5/24
• iptables的表和链。我们只关心nat表和filter表。filter表是默认的表,它实现防火墙,也就是包过滤的功能。nat表实现网络地址转换。
ptables操作
常用选项:
-A 追加规则–>iptables -A INPUT
-D 删除规则–>iptables -D INPUT 1(编号)
-R 修改规则–>iptables -R INPUT 1 -s 192.168.12.0 -j DROP 取代现行规则,顺序不变(1是位置)
-I 插入规则–>iptables -I INPUT 1 --dport 80 -j ACCEPT 插入一条规则,原本位置上的规则将会往后移动一个顺位
-L 查看规则–>iptables -L INPUT 列出规则链中的所有规则
通用参数:
-p 协议 例:iptables -A INPUT -p tcp
-s源地址 例:iptables -A INPUT -s 192.168.1.1
-d目的地址 例:iptables -A INPUT -d 192.168.12.1
-sport源端口 例:iptables -A INPUT -p tcp --sport 22
-dport目的端口 例:iptables -A INPUT -p tcp --dport 22
-i指定入口网卡 例:iptables -A INPUT -i eth0
-o指定出口网卡 例:iptables -A FORWARD -o eth0
-j 指定要进行的处理动作
常用的ACTION:
DROP:丢弃
REJECT:明示拒绝
ACCEPT:接受
查看规则
[root@proxy ~]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@proxy ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
起动服务时,iptables将会出现一些默认规则
[root@proxy ~]# systemctl start iptables
默认规则往往不合我们的要求,可以先将所有的规则清空
[root@proxy ~]# iptables -F
[root@proxy ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
iptables的语法
iptables [-t 表名] 选项 [链名] [条件] [-j 满足条件的操作]
• 示例
– 可以设置默认拒绝,然后明确允许
– 也可以设置默认允许,然后明确拒绝
向INPUT链追加规则,192.168.4.1发来的包全部接受
A是追加,-s是匹配源地址,-j为jump,采取的行为,ACCEPT是接受
[root@node1 ~]# iptables -A INPUT -s 192.168.4.1 -j ACCEPT
将INPUT链的默认规则改为DROP丢弃。-P设置默认规则
[root@proxy ~]# iptables -P INPUT DROP
[root@proxy ~]# iptables -L INPUT
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- 192.168.4.1 anywhere
在192.168.4.10上访问node1,将会被拒绝
[root@client ~]# ping 192.168.4.5
PING 192.168.4.5 (192.168.4.5) 56(84) bytes of data.
允许192.168.4.0网络的主机ssh连接node1
-I是插入到INPUT链的第1个位置。-p指定协议,–dport指定目标端口号。-j是执行的操作
[root@proxy ~]# iptables -I INPUT 1 -s 192.168.4.0/24 -p tcp --dport 22 -j ACCEPT
查看规则
[root@proxy ~]# iptables -L INPUT
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 192.168.4.0/24 anywhere tcp dpt:ssh
ACCEPT all -- 192.168.4.1 anywhere
配置任何地址访问node1的80端口,即http协议,都接受
[root@proxy ~]# yum -y install httpd
[root@proxy ~]# systemctl start httpd
[root@proxy ~]# iptables -I INPUT 2 -s 192.168.4.0/24 -p tcp --dport 80 -j ACCEPT
测试
[root@client ~]# curl 192.168.4.5
icmp就是ping命令底层用到的协议,叫Internet控制消息协议
[root@proxy ~]# iptables -A INPUT -s 192.168.4.10 -p icmp -j REJECT
[root@proxy ~]# iptables -nL INPUT
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 192.168.4.0/24 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 192.168.4.0/24 0.0.0.0/0 tcp dpt:80
ACCEPT all -- 192.168.4.1 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
REJECT icmp -- 192.168.4.10 0.0.0.0/0 reject-with icmp-p
删除第5条规则
[root@proxy ~]# iptables -nL INPUT --line-numbers
Chain INPUT (policy DROP)
num target prot opt source destination
1 ACCEPT tcp -- 192.168.4.0/24 0.0.0.0/0 tcp dpt:22
2 ACCEPT tcp -- 192.168.4.0/24 0.0.0.0/0 tcp dpt:80
3 ACCEPT all -- 192.168.4.1 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
5 REJECT icmp -- 192.168.4.10 0.0.0.0/0 reject-with icmp-port-unreachable
[root@proxy ~]# iptables -D INPUT 5
拒绝192.168.4.10 ping 。-I不指定位置,默认插到最上面
[root@proxy ~]# iptables -I INPUT -s 192.168.4.10 -p icmp -j REJECT
DROP是直接丢弃,REJECT是明确拒绝。
保存规则。不保存规则,重启iptables服务,自定义规则将消失
[root@proxy ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]