iptables入门操作

一、iptables的增删查改

1.查

[root@mysql ~]# iptables -t filter -L -nv --line-numbers
Chain INPUT (policy ACCEPT 40 packets, 3056 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all – * * 10.0.0.66 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 30 packets, 3592 bytes)
num pkts bytes target prot opt in out source destination

-t 指定表
-L 查看-t指定表的规则
-v 显示详细信息
pkts:规则匹配到的报文个个数
bytes:报文包的大小总和
target: 动作
prot:协议
opt:规则的选项
in:表示数据包从哪个接口(网卡)流入
out:表示数据包从哪个接口(网卡)流出
source:规则的源地址
destination:规则的目标地址
policy ACCEPT:默认规则的策略,表示默认动作的ACCEPT
-n 不对IP地址进行反解
–line-numbers 显示规则序号

2.增

[root@mysql ~]# iptables -t filter -A INPUT -s 10.0.0.66 -j DROP

[root@mysql ~]# iptables -t filter -L -nv --line-numbers
Chain INPUT (policy ACCEPT 6 packets, 396 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all – * * 10.0.0.66 0.0.0.0/0
2 0 0 DROP all – * * 10.0.0.66 0.0.0.0/0

-A 追加
-I 为第一行插入,可以 -I INPUT 2表示在第二行插入
-s 源地址,表示匹配来自10.0.0.66的报文
-j 动作,DROP为丢弃,REJECT拒绝,ACCEPT允许
注:如果报文被前面的规则匹配到了,则会被执行对应的动作,及时后面也能匹配到当前的报文,也没有机会再对报文执行相应的动作了。如上两条规则,第一条是允许,第二条是拒绝,当匹配的第一条允许后就不会再匹配拒绝了。

3.删

方法1;根据规则的编码删除

[root@mysql ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all – 10.0.0.66 anywhere
2 DROP all – 10.0.0.66 anywhere
[root@mysql ~]# iptables -t filter -D INPUT 2
[root@mysql ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all – 10.0.0.66 anywhere

-D 删除
方法2:根据匹配条件与动作删除

[root@mysql ~]# iptables -t filter -D INPUT -s 10.0.0.66 -j ACCEPT
[root@mysql ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination

方法3:删除所有规则

[root@mysql ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all – 10.0.0.66 anywhere
2 ACCEPT all – 10.0.0.88 anywhere
3 ACCEPT all – 10.0.0.99 anywhere
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination

[root@mysql ~]# iptables -t filter -F

[root@mysql ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination

-F flush,冲刷指定的链

4.改

修改表规则

[root@mysql ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all – 10.0.0.66 anywhere

[root@mysql ~]# iptables -t filter -R INPUT 1 -s 10.0.0.66 -j DROP

[root@mysql ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP all – 10.0.0.66 anywhere

修改默认策略

[root@mysql ~]# iptables -L
Chain FORWARD (policy ACCEPT)
[root@mysql ~]# iptables -t filter -P FORWARD DROP
Chain FORWARD (policy DROP)

5.保存规则

在默认情况下,我们队防火墙的修改都是临时的,当重启iptables时,所做的修改就会失效。
为了防止这种情况发生,我们需要将规则保存
方法1:

[root@mysql ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]

方法2:

[root@mysql ~]# iptables-save>/etc/sysconfig/iptables
重载规则
[root@mysql ~]# iptables-restore </etc/sysconfig/iptables

二、匹配更多条件

1.源地址与目标地址

匹配多个源地址

[root@mysql ~]# iptables -t filter -F INPUT
[root@mysql ~]# iptables -t filter -A INPUT -s 10.0.0.66,10.0.0.88 -j ACCEPT
[root@mysql ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all – 10.0.0.66 anywhere
ACCEPT all – 10.0.0.88 anywhere

匹配一个网段

[root@mysql ~]# iptables -t filter -I INPUT -s 10.0.0.0/24 -j DROP

取反

[root@mysql ~]# iptables -t filter -A INPUT -s ! 10.0.0.66 -j ACCEPT
注:这条规则的意思是只要不是10.0.0.66的报文都允许,但是不代表10.0.0.66的报文就会被拒绝,因为没有指定对.66的执行的动作。
目标地址
源地址表示报文从哪里来,目标地址表示报文到哪去
目标地址也可以使用取反,逗号隔IP,网段。
[root@mysql ~]# ifconfig |grep 10.0.|grep -v coll
inet addr:10.0.0.132 Bcast:10.0.0.255 Mask:255.255.255.0
inet addr:10.0.0.133 Bcast:10.0.0.255 Mask:255.255.255.0
丢弃10.0.0.66报文通过本机的10.0.0.33
[root@mysql ~]# iptables -t filter -F
[root@mysql ~]# iptables -t filter -A INPUT -s 10.0.0.66 -d 10.0.0.133 -j DROP
[root@mysql ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all – 10.0.0.66 10.0.0.133
[root@liang ~]# ping -c 2 10.0.0.133
PING 10.0.0.133 (10.0.0.133) 56(84) bytes of data.
— 10.0.0.133 ping statistics —
2 packets transmitted, 0 received, 100% packet loss, time 999ms
[root@liang ~]# ping -c 2 10.0.0.132
PING 10.0.0.132 (10.0.0.132) 56(84) bytes of data.
64 bytes from 10.0.0.132: icmp_seq=1 ttl=64 time=0.833 ms
64 bytes from 10.0.0.132: icmp_seq=2 ttl=64 time=0.480 ms
— 10.0.0.132 ping statistics —
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.480/0.656/0.833/0.178 ms

2.协议类型

丢弃10.0.0.66报文通过本机的10.0.0.33的tcp类型请求

[root@liang ~]# iptables -F INPUT
[root@liang ~]# iptables -A INPUT -s 10.0.0.66 -d 10.0.0.133 -p tcp -j REJACE
[root@liang ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp – 10.0.0.66 10.0.0.133
[root@liang ~]# ssh 10.0.0.133
ssh: connect to host 10.0.0.133 port 22: Connection refused
[root@liang ~]# ping -c 2 10.0.0.133
PING 10.0.0.133 (10.0.0.133) 56(84) bytes of data.
64 bytes from 10.0.0.133: icmp_seq=1 ttl=64 time=0.476 ms
64 bytes from 10.0.0.133: icmp_seq=2 ttl=64 time=0.481 ms

-p 指定协议,不使用则所有协议类型都会匹配。centos6支持
centos6支持tcp,udp,udplite,icmp,esp,ah,sctp;centos7支持tcp,udp,udplite,icmp,esp,ah,sctp,icmpv6,mh

3.网卡接口

当本机有多个网卡时

[root@liangl ~]# ifconfig |grep eth
eth0 Link encap:Ethernet HWaddr 00:0C:29:28:84:39
eth1 Link encap:Ethernet HWaddr 00:0C:29:28:84:43

拒绝eth1网卡的报文流入

[root@mysql ~]# iptables -F INPUT
[root@mysql ~]# iptables -I INPUT -i eth1 -p icmp -j REJECT
eth1的网卡是源主机的网卡

4.扩展匹配条件

  • 匹配条件分为扩展匹配条件与基本匹配条件,基本匹配条件我们可以直接使用,而扩展匹配条件则需要依赖一些扩展模块,或者说在使用扩展匹配条件之前,需要指定相应的模块。

4.1源端口与目标端口

源端口 --sport
目标端口 --dport
可以使用感叹号取反,端口范围使用冒号22:55,:22表示0到22,22:表示22到65535
–dports 指定多个端口 22,23,24或者22,80: 不过需要指定扩展模块-m multiport
拒绝10.0.0.66连接本机的22端口

[root@mysql ~]# iptables -F INPUT
[root@mysql ~]# iptables -A INPUT -s 10.0.0.66 -p tcp -m tcp --dport 22 -j REJECT
[root@mysql ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp – 10.0.0.66 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
[root@liang ~]# ssh 10.0.0.132
ssh: connect to host 10.0.0.132 port 22: Connection refused

–dport属于扩展匹配条件,指定匹配报文的目标端口,而在使用–dport之前,使用-m来指定扩展模块为tcp,也就是说如果想使用–dport这个扩展匹配条件则必须依靠tcp扩展模块来完成。
当使用-p选项指定报文协议时,不指定-m,默认会使用与报文协议名称相同的扩展模块。也就是说这条规则指定了-p tcp后可以不指定-m tcp。

4.2源地址与目标地址

-m iprange 指定一段连续的IP范围
–src-range 源地址范围
–det-range 目标地址范围

[root@liang ~]# iptables -A INPUT -m iprange --src-range 10.0.0.66-10.0.0.77 -j DROP
[root@liang ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all – anywhere anywhere source IP range 10.0.0.66-10.0.0.77

4.3string模块

拒绝带有字符串”aaa“的报文进入本机。

[root@liang ~]# iptables -A INPUT -m string --algo bm --string “aaa” -j REJECT
[root@liang ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT all – anywhere anywhere STRING match “aaa” ALGO name bm TO 65535
–algo bm 指定匹配算法
–string 要匹配的字符

4.4time模块

禁止本机每天09到10点访问80端口

[root@mysql ~]# iptables -R OUTPUT 1 -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 10:00:00 -j REJECT
[root@mysql ~]# iptables -L
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp – anywhere anywhere tcp dpt:http TIME from 09:00:00 to 10:00:00 reject-with icmp-port-unreachable

禁止本机周六周日访问80端口

[root@mysql ~]# iptables -R OUTPUT 1 -p tcp --dport 80 -m time --weekdays 6,7 -j REJECT
[root@mysql ~]# iptables -L
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp – anywhere anywhere tcp dpt:http TIME on Sat,Sun reject-with icmp-port-unreachable

禁止本机每月10号与11号访问80端口

root@mysql ~]# iptables -R OUTPUT 1 -p tcp --dport 80 -m time --monthdays 10,11 -j REJECT
[root@mysql ~]# iptables -L
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp – anywhere anywhere tcp dpt:http TIME on 10th,11st reject-with icmp-port-unreachable

4.5state状态扩展模块

state的五个状态
NEW:新连接的第一个包。
ESTABLISHED:NEW后的状态,表示已建立连接
RELATED:启动新连接,但有旧的连接与新的连接相关联。
INVALID:没法被识别的包或无状态的包
UNTRACKED:表示为被追踪的报文
只允许本机连接其他机器,不允许其他机器主动连接。

[root@mysql ~]# iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@mysql ~]# iptables -A INPUT -j REJECT
[root@mysql ~]# ssh 10.0.0.66
root@10.0.0.66’s password:
Last login: Tue Oct 23 03:38:07 2018 from 10.0.0.1
[root@liang ~]# ssh 10.0.0.132
ssh: connect to host 10.0.0.132 port 22: Connection refused

笔记来源 http://www.zsythink.net/archives/category/运维相关/防火墙/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值