完成防火墙的初始化后,添加链规则
基本语法如下
iptables -A FORWARD -i eth0 -j DROP
-A 匹配链的名字INPUT、OUTPUT、FORWARD等。可通过iptables -L查看链名称
-i 匹配网卡接口名称eth0、eht1等。可通过ifconfig查看网卡详细信息
-j 用于定义策略ACCEPT、DROP、REJECT、LOG等
-p 用于匹配网络协议:tcp、udp、icmp等
—-icmp-type type 匹配ICMP类型,和-p icmp 搭配使用。
-s 匹配来源主机的IP地址
—-sport 匹配来源主机的端口,和-s 配合使用。
-d 匹配目标主机IP地址
—-dport 匹配目标主机或网络的端口号,和-d 配合使用。
使用语法如下;
iptables -A INPUT -i eth0 -p ALL -j ACCEPT
iptables -A INPUT -i eth0 -p icmp —-icmp-type 8 -j ACCEPT //8对应的类型为ping
iptables -A INPUT -i eth0 -p tcp -s 10.99.1.100/24 --dport 22 -j ACCEPT
为链添加LOG(日志记录)策略
iptables -A INPUT -i eth0 -j LOG
默认情况下防火墙记录到的访问记录被保存在/var/log/messages中。可以使用任何文本查看命令查看,如:
cat /var/log/messages
常用的记录字节有IN(接收数据包的网络接口)、SEC(数据包来源的IP地址)、DST(数据包的目的IP地址)以及日期和时间。因为系统自动生成的日志总体不够友好,可以借助日子分析工具。swatch和logcheck等
如果配置错误或者想取消链策略,可通过iptables -D 删除链规则,例如删除上面所配置的规则,使用语法如下
方法1 iptables -D
iptables -D INPUT -i eth0 -p ALL -j ACCEPT //必须一个字母不差的拷贝上面所配置的命令
方法2通过—-line-numbers选项查看链规则编号
[root@liukai ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- 10.99.1.0/24 anywhere tcp dpt:ssh
2 LOG all -- anywhere anywhere LOG level warning
Chain FORWARD (policy DROP)
num target prot opt source destination
1 DROP all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
[root@liukai ~]# iptables -D INPUT 2 //删除了INPUT链编号为11的链规则
[root@liukai ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- 10.99.1.0/24 anywhere tcp dpt:ssh
Chain FORWARD (policy DROP)
num target prot opt source destination
1 DROP all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
案例1将来自任何地方的来源port 1:1023的主动连接到本端的port 1:1023连接丢弃
[root@liukai ~]# iptables -A INPUT -i eth0 -p tcp --sport 1:1023 \
> --dport 1:1023 -syn -j DROP
案例2:iptables -m 外挂模块
语法
iptables -A INPUT [-m state] [--state状态]
-m :一些iptables的外挂模块,主要常见有:
state:状态模块
mac:网卡硬件地址
--state:一些数据包状态,主要有:
INVALLD :无效的状态包,例如数据包破损的数据包状态
ESTABLEISHED:已经连接成功的连接状态
NEW :想要新建立连接的数据包状态
**RELATED :表示这个数据包是与主机发送出去的数据包有关**
案例:只要已建立连接或与已发出请求相关的数据包就予以通过。
[root@liukai ~]# iptables -A INPUT -m state \
> --state RELATED,ESTABLISHED -j ACCEPT
不合法的数据包丢弃
[root@liukai ~]# iptables -A INPUT -m state --state INVALid -j DROP
没有什么东西绝对可靠