示例;放行来自于172.16.0.0/16网络的主机对本机ssh服务的请求;下面这个示例是一对出现的; 172.16.34.30允许所有来自172.16.0.0/16网段发送ssh服务的请求 iptables -A INPUT -s 172.16.0.0/16 -d 172.16.34.30 -p tcp --dport 22 -j ACCEPT 172.16.34.30主机的ssh服务,只允许响应给172.16.0.0/16这个网段 iptables -A OUPOUT -s 172.16.34.30 -d 172.16.0.0/16 -p tcp --sport 22 -j ACCEPT 设置默认策略,拒绝所有未知的服务,仅放行规则所匹配的服务 iptables -P INPUT DROP iptables -P OUTPUT DROP 输入这两条件命令我的主机并没有中断,是因为上面事先写好了两条允许本机的ssh服务规则,所没有中断,但是本机的其它服务,别的主机是访问不了的。 放行本机的80端口给172.16.0.0/16的所有地址访问 (这时明确了所有地址,因此在写规则时源地址省略不写,直接写上目标地址即可) iptables -A INPUT -d 172.16.34.30 -p tcp --dport 80 -j ACCEPT只写这一条的话,访问的报文可以进来,但是无法响应给客户,因此还有写一条响兴给客户端主机的规则 iptables -A OUTPUT -s 172.16.34.30 -p tcp --sport 80 -j ACCEPT此时的源地址是172.16.34.30是向客户端主机响应 -p icmp --icmp-type 8: ping请求 0:ping响应 例子:放行ping其它主机 iptables -A OUTPUT -s 172.16.100.7 -p icmp --icmp-type 8 -j ACCEPT iptables -A INPUT -d 172.16.100.7 -p icmp --icmp-type 0 -j ACCEPT 放行其它主机ping本机 iptables -A INPUT -d 172.16.100.1 -P icmp --icmp-type 8 -j ACCEPT iptables -A OUTPUT -s 172.16.100.7 -p icmp --icmp-type 0 -j ACCEPT -p udp --dport --sport 放行本机的tftp服务:(还有两条tcp的没有写) iptables -A INPUT -s 172.16.0.0/16 -d 172.16.100.7 -p udp --dport 69 -j ACCEPT iptables -A OUTPUT -s 172.16.100.7 -d 172.16.0.0/16 -p udp --sport 69 -j ACCEPT 放行本机dns服务:(DNS总共有八条规则,还有tcp的四条没有写) iptables -A INPUT -s 172.16.0.0/16 -d 172.16.100.7 -p udp --dport 53 -j ACCEPT iptables -A OUTPUT -s 172.16.100.7 -d 172.16.0.0/16 -p udp --sport 53 -j ACCEPT iptables -A OUTPUT -s 172.16.100.7 -p udp --dport 53 -j ACCEPT iptables -A INPUT -d 172.16.100.7 -p udp --sport 53 -j ACCEPT 显示扩展 规则命令; iptables -D INPUT 2 表示删除input链上的第二条规则 记住如果删除了第二条而原来的的第三条会自动变成第二条 iptables -I 插入规则 iptables -I INPUT 2 -i lo -j ACCEPT 表示将规则插入到第二行 iptables -R INPUT 1 -s 172.16.0.0/16 -d 172.16.34.30 -i eth0 -p tcp --dport 22 -j ACCEPT 完整替换第一条规则 明确规定172.16网段连接目标地址的22号服务必须从eth0网卡进来 iptables -S INPUT 显示指定的链的规则 显示扩展;必须明确指定的扩展模块; -m 扩展模块名称 --专用选项1 --专用选项2 iptables -I OUTPUT -s 172.16.34.30 -p tcp -m multiport --sports 22,80 -j ACCEPT iptables -A INPUT -i eth0 -m multiport -p tcp --dports 53,113,135,137,139,445 -j DROP 本机通过这个eth0网卡进来的端口都被拒绝 示例;允许本机的telnet服务给指定的主机ip string;对字符串匹配 1 iptables -I OUTPUT -s 172.16.34.30-p tcp --sport 80-m string --algo kmp --string 'hello'-f DROP 写完这个匹配字符串的规则后再次访问时显示是无法打开被匹配到的网页
|
|