netfilter---->iptables(工具)vim
查看指定filter表的规则 -nvL(默认是filter表)bash
[root@wy ~]# iptables -t filter -nvL网络
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)tcp
pkts bytes target prot opt in out source destinationide
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)工具
pkts bytes target prot opt in out source destinationspa
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)rest
pkts bytes target prot opt in out source destinationip
另外两个表nat、mangle,但最经常使用的是filter。get
filter主要用来过滤包(进包、出包),做一些规则或限制。
添加一条规则
[root@wy ~]# iptables -t filter -I INPUT -p tcp --dport 80 -s 12.12.12.12 -j REJECT
[root@wy ~]# iptables -nvL
Chain INPUT (policy ACCEPT 36 packets, 2592 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT tcp -- * * 12.12.12.12 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 19 packets, 1992 bytes)
pkts bytes target prot opt in out source destination
说明:添加规则也能够用-A ,但它们的区别是-A是添加在全部规则的后面,-I是添加在最前面。
-I插入的规则比-A增长的规则优先生效
删除刚添加的规则
[root@wy ~]# iptables -t filter -D INPUT -p tcp --dport 80 -s 12.12.12.12 -j REJECT
还有另一种删除规则方法:
iptables -nvL --line-numbers先显示规则序号,而后iptables -D INPUT/OUTPUT 序号就能够了
清空计数器-Z
[root@wy ~]# iptables -Z
说明:也就是包和字节都变成了0
清空所有规则-F
[root@wy ~]# iptables -F
注:若指定port的时候要加上协议,不然会报错。
保存规则
[root@wy ~]# service iptables save
查看保存的规则
[root@wy ~]# cat /etc/sysconfig/iptables
备份与恢复
[root@wy ~]# iptables-save > 1.ipt
[root@wy ~]# iptables -F ###恢复前先清空
[root@wy ~]# iptables-restore < 1.ipt
filter 主要用来限制进入本机的包和出去的包
nat 主要网络地址转换
mangle 主要给数据包打标记
修改策略
[root@wy ~]# iptables -P INPUT ACCEPT #针对INPUT链
INPUT链实例:要求把预设策略设成DROP,其余两个是ACCEPT,针对192.168.0.0/24,22端口开放,对全部的Ip的80端口放行,全部的网段21端口放行。
[root@wy ~] vim 1.ipt.sh
#! /bin/bash
ipt="/sbin/iptables"
$ipt -F
$ipt -P INPUT DROP
$ipt -P OUTPUT ACCEPT
$ipt -P FORWARD ACCEPT
$ipt -A INPUT -s 192.168.0.0./24 -p tcp --dport 22 -j ACCEPT
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT
执行脚本
[root@wy ~] sh 1.ipt.sh