Linux中iptables防火牆的簡單配置

byfree發表於2008-05-05

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部落格 ” ,連結:http://blog.itpub.net/17997/viewspace-260235/,如需轉載,請註明出處,否則將追究法律責任。

相關文章