本文共 1108 字,大约阅读时间需要 3 分钟。
1、简介
iptables或netfilter(网络过滤器) 是一个用户态的防火墙应用软件,允许系统管理员可以调整设定X表(Xtables)提供相关的系统表格(目前主要是在 iptables/netfilter 底下)以及相关的“链”与“规则”来管理网络封包的流动与转送的动作。因为相关动作上的需要,所以 iptables 的操作需要用到系统管理员的权限,不然就无法运作下去。在大部份的 Linux 系统上面, iptables 是使用/usr/sbin/iptables 来操作,文件则放置在手册页(Man page)(参考: [1])底下,也可以透过 man iptables 的方式来取得这份文件。通常 iptables 都需要核心层级(kernel)的模组来配合运作,Xtables 是主要在核心层级里面 iptables API 运作功能的模组。
2、参数说明
-A 向规则链中添加一条规则,默认被添加到末尾 -T指定要操作的表,默认是filter -D从规则链中删除规则,可以指定序号或者匹配的规则来删除 -R进行规则替换 -I插入一条规则,默认被插入到首部 -F清空所选的链,重启后恢复 -N新建用户自定义的规则链 -X删除用户自定义的规则链 -p用来指定协议可以是tcp,udp,icmp等也可以是数字的协议号, -s指定源地址 -d指定目的地址 -i进入接口 -o流出接口 -j采取的动作,accept,drop,snat,dnat,masquerade --sport源端口 --dport目的端口,端口必须和协议一起来配合使用
3、iptable配置实例 3.1 设定默认规则 在iptables规则中没有匹配到规则则使用默认规则进行处理 iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -P FORWARD DROP 3.2 配置SSH规则 iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT 如果你把OUTPUT 设置成DROP,就需要加上这个规则,否则SSH还是不能登录,因为SSH服务之进不能出。 只允许192.168.0.3的机器进行SSH连接 iptables -A INPUT -s 192.168.0.3 -p tcp --dport 22 -j ACCEPT 如果要允许,或限制一段IP地址可用 192.168.0.0/24 表示192.168.0.1-255端的所有IP.
转载地址:http://gedoi.baihongyu.com/