Iptables的一些简单实例

[root@localhost ~]# iptables -L        //查看Iptables规则链中的所有规则;
[root@localhost ~]# iptables -F        //清空Iptables规则链中所有规则;
[root@localhost ~]# iptables -P INPUT DROP             //所有进入本机的数据包都将丢弃;
[root@localhost ~]# iptables -P INPUT ACCEPT         //允许所有的数据包进入本机;

[root@localhost ~]# iptables -A INPUT -p tcp --tcp-flags SYN,ACK SYN -j DROP        //允许本地主机对外发起TCP连接时,能收到外部主机的TCP应答并建立连接;
DROP       tcp  --  anywhere             anywhere            tcp flags:SYN,ACK/SYN
或者:
[root@localhost ~]# iptables -A INPUT -p tcp --syn -j DROP                                  
DROP       tcp  --  anywhere             anywhere            tcp flags:SYN,RST,ACK/SYN       //不但过滤SYN,还过滤reset;

[root@localhost ~]# iptables -A INPUT -s ! 192.168.100.0/24 -p icmp -j DROP             //禁止除192.168.100.0网段外的主机ping本地计算机;

[root@localhost ~]# iptables -A OUTPUT -d ! 192.168.100.0/24 -m owner --uid-owner 0 -j DROP    //把uid=0,也就是root用户的数据包除发往192.168.100.0网段外,全部drop掉;
Chain OUTPUT (policy ACCEPT)
target       prot opt source             destination
DROP       all  --  anywhere            !192.168.100.0/24    OWNER UID match root

转换源IP地址:
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j MASQUERADE   //将来自192.168.100.0网段的数据包中来源IP地址,转换成eth0网卡的IP地址;
如果eth0有固定ip,写固定IP会比较好:
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -j SNAT --to-source 10.0.1.254

转换目的IP地址:
[root@localhost ~]# iptables -t nat -A PREOUTING -d [url]www.example.com[/url] -p tcp --dport 80 -j DNAT --to-dest 192.168.100.1
//将所有到目的地[url]www.example.com[/url]的、使用TCP协议的、端口为80的数据包的目的地IP地址,转换为192.168.100.1

[root@localhost ~]# iptables -A INPUT -p all -s 0.0.0.0/0.0.0.0 -i lo -j ACCEPT     //允许来自lo网络接口的所有数据包;

你可能感兴趣的:(职场,iptables,休闲)