赞
踩
防火墙功能:限制访问(限制mac、IP、port),限制访问数量,给数据包打标签,控制网速,统计流量等
即过滤数据包
防火墙分类:硬件/软件防火墙, Linux中的防火墙偏硬件(因为是调用系统内核使用的)
Linux(CentOS)中的防火墙:iptables、firewall、SELinux
netfilter
iptables | firewall
,为指令集yum install iptables.x86_64 iptables-devel.x86_64 iptables-services.x86_64 -y
systemctl start iptables
iptables -L
iptables -F
raw
数据包跟踪mangle
标记数据包(只标记,不做其它动作)nat
网络地址转换,用于修改源地址和目的地址filter
数据包过滤PREROUTING
路由之前(即针对的是路由之前的数据包)INPUT
数据包流入网卡FORWARD
数据包流经网卡OUTPUT
数据包流出网卡POSTROUTING
路由之后raw
–>mangle
–>nat
–>filter
,链也是从上到下按顺序匹配)iptables [-t table_name] <动作> <链名> <匹配条件> <目标动作>
查看防火墙规则
iptables -L
查看4张表的全部规则
iptables -L -t table_name
查看指定表的规则
iptables -nL
把所有主机名用IP地址表示
iptables -S [-t table_name]
直接查看创建规则时的命令
[root@192 ~]# iptables -L -t filter
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
...
[root@192 ~]# iptables -S -t filter
-P INPUT ACCEPT
-P FORWARD ACCEPT
...
增加规则:-A
(add)或-I
(insert)
在指定表的最后面增加一条规则(若没有-t table_name
,默认为filter表)
iptables -t filter -A INPUT -p icmp -s IP -j REJECT
-A
表示动作为添加规则INPUT
表示添加的规则是针对INPUT链的(即进入网卡的数据包)-p icmp -s IP
匹配的条件
-p icmp
表示使用的协议是ICMP协议的(协议是protocol)-s IP
表示指定源IP地址(源地址是source IP)-j REJECT
表示拒绝匹配到的数据包(jump有快速行动的意思)在一张表中指定链的指定位置插入规则
iptables [-t table_name] -I [n] <链名> <匹配条件> <目标动作>
-I [n]
不加n时,是在指定表的指定链的最前面加入规则,使用n可以指定添加到第n条iptables -D INPUT 3
表示删除filter表的INPUT链的第3条规则自定义链:非自定义链(即那5个链)是即写即生效的,而自定义链写好后要被调用才生效
创建自定义链
iptables -N <chain_name>
iptables -E <old_name> <new_name>
调用自定义链,即将自定义链添加到5条链中去
iptables -t filter -A INPUT -j test
删除自定义链:注意,被调用的自定义链和写了规则的自定义链不能删除
iptables -X <chain_name>
不常用,比较麻烦,一般先删除再添加即可
iptables [-t table_name] -R n <chain_name> <匹配条件> <目标动作>`
-R n
表示修改指定表中的指定链的第n条规则iptables -F
iptables -F <chain_name>
iptables -D INPUT 3
表示删除filter(默认)的INPUT链的第3条规则所有数据包都会经过防火墙,当数据包匹配到了特点规则时,就按指定的动作执行;当未匹配到特点规则时,就按默认动作(规则)执行。默认规则只有ACCEPT
和DROP
查看一下默认规则
[root@192 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
...
修改默认规则
iptables -P INPUT DROP
iptables还可以查看流量
iptables -nL -v
# 查看流量
iptables -Z [chain_name n]
# 清空流量,可以指定链和链中第n条规则的流量
iptables可以匹配的条件非常多,可以在需要的时候查看手册,如查看设定一个范围的IP地址的用法
[dream@192 ~]$ iptables -m iprange --help
[dream@192 ~]$ iptables -m iprange --help
iprange: Could not determine whether revision 1 is supported, assuming it is.
iptables v1.4.21
...
iprange match options: # 这就是iprange的用法
[!] --src-range ip[-ip] Match source IP in the specified range
[!] --dst-range ip[-ip] Match destination IP in the specified range
-m range
m表示match,即扩展匹配man iptables -extensions
查看所有用法iptables有三个基本匹配条件:IP地址,协议protocol,端口port
IP地址匹配
-s IP_address[/mask] [, IP_address[/mask], ...] # 指定源IP地址,可以同时指定多个
-d IP_address[/mask] [, IP_address[/mask], ...] # 指定目的IP地址,可以同时指定多个
-s --source
-d --destination
mask
即子网掩码,可写可不写,可以写出数字,也可以写成掩码,如24 = 255.255.255.0
-m iprange --src-range 192.168.1.1-192.168.1.10
表示匹配条件为源IP地址在指定范围内端口匹配
-sport port_num # 匹配源端口(source port)
-dport port_num # 匹配目的端口(destination port)
也可以多端口匹配
[dream@192 ~]$ iptables -m multiport --help
multiport: Could not determine whether revision 1 is supported, assuming it is.
iptables v1.4.21
...
multiport match options:
[!] --source-ports port[,port:port,port...]
--sports ...
match source port(s)
[!] --destination-ports port[,port:port,port...]
--dports ...
match destination port(s)
[!] --ports port[,port:port,port]
match both source and destination port(s)
例子:
[dream@192 ~]$ sudo iptables -A INPUT -p tcp -m multiport --dports 22,21,80 -j REJECT
[sudo] dream 的密码:
[dream@192 ~]$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- anywhere anywhere multiport dports ssh,ftp,http reject-with icmp-port-unreachable
当匹配到了条件后,就要执行特定的动作
常用的动作如下
DROP
丢弃
REJECT
拒绝
ACCEPT
接收
SNAT
修改源IP地址,必须按如下格式书写
iptables -t nat -A POSTROUTING -s <old_IP> -j SNAT <old_IP> to <new_IP>
理解:计算机只能修改已经路由后,准备发出去的数据包的源IP(如果修改目标IP,就找不到了)
机理和网络地址转换协议NAT是一样的
DNAT
修改源目标P地址,必须按如下格式书写
iptables -t nat -A PREROUTING -d <old_IP> -j DNAT <old_IP> to <new_IP>
参考教程
B站视频:千峰
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。