1.清除filter中所有规则链的规则
# iptables -F
2.设置INPUT链的策略
# iptables -A INPUT -i lo -j ACCEPT
# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A INPUT -s 10.10.10.10 -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
这里的IP地址10.10.10.10表示源地址,也就是要访问服务器的客户端地址,--dport 22是指开放的服务器的端口号,22是SSH的端口。
最后再在INPUT链中加入reject,拒绝未指明的所有其它的端口。
# iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
3.列出表/链中的所有规则
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- 10.10.10.10 anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- 10.10.10.10 anywhere state NEW tcp dpt:1521
ACCEPT udp -- 10.10.10.10 anywhere state NEW udp dpt:snmp
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
以上信息表示在INPUT链中,允许IP地址为10.10.10.10的用户连接到本服务器的22、1521TCP端口和161UDP(snmp)端口,其它所以端口均关闭。
4.增加INPUT链的策略
# iptables -A INPUT -s 10.10.10.11 -p tcp -m state --state NEW -m tcp --dport 1521 -j ACCEPT
这里表示增加了一个IP地址为10.10.10.11、1521TCP端口的访问策略;
# iptables -A INPUT -s 10.10.10.12 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
这里表示增加了一个IP地址为10.10.10.12、80TCP端口的访问策略;
5.插入INPUT链的策略
# iptables -I INPUT 3 -s 10.10.10.12 -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
由于上面例子中INPUT链的最后有REJECT,所以要想让策略生效就必须在REJECT之前插入,这里例子中INPUT 3是指在链中的第三个位置插入策略。
6.替换INPUT链的策略
# iptables -R INPUT 3 -s 10.10.10.13 -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
这里是对INPUT链的第三个位置的策略的替换,修改三号位置的IP地址。
下面让我们看看INPUT链的简单操作:
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere (INPUT链的第一号位置)
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- 10.10.10.10 anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- 10.10.10.10 anywhere state NEW tcp dpt:1521
ACCEPT udp -- 10.10.10.10 anywhere state NEW udp dpt:snmp
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
# iptables -I INPUT 3 -s 10.10.10.12 -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- 10.10.10.12 anywhere state NEW tcp dpt:ssh (INPUT链的第三号位置)
ACCEPT tcp -- 10.10.10.10 anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- 10.10.10.10 anywhere state NEW tcp dpt:1521
ACCEPT udp -- 10.10.10.10 anywhere state NEW udp dpt:snmp
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
# iptables -R INPUT 3 -s 10.10.10.13 -p tcp -m state --state NEW -m tcp --dport 1521 -j ACCEPT
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- 10.10.10.13 anywhere state NEW tcp dpt:1521
ACCEPT tcp -- 10.10.10.10 anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- 10.10.10.10 anywhere state NEW tcp dpt:1521
ACCEPT udp -- 10.10.10.10 anywhere state NEW udp dpt:snmp
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/17997/viewspace-260235/ ,如需转载,请注明出处,否则将追究法律责任。