ubuntu中防火牆iptables配置

特別說明:此文章徹底轉載於http://www.javashuo.com/article/p-pgfybifv-bh.htmljavascript

1.查看系統是否安裝防火牆

root@localhost:/usr# which iptables /sbin/iptables root@localhost:/usr# whereis iptables iptables: /sbin/iptables /etc/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz 

若是是這樣的信息,那麼代表iptables就是安裝了的。
若是沒有安裝,那麼使用sudo apt-get install iptables 安裝。html

2.查看防火牆的配置信息

配置好了的,是這個樣子。java

root@localhost:/usr# sudo iptables -L Chain INPUT (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- anywhere anywhere state NEW tcp dpt: 22 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http ACCEPT icmp -- anywhere anywhere limit: avg 100/sec burst 100 ACCEPT icmp -- anywhere anywhere limit: avg 1/sec burst 10 syn-flood tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain syn-flood (1 references) target prot opt source destination RETURN tcp -- anywhere anywhere limit: avg 3/sec burst 6 REJECT all -- anywhere anywhere reject-with icmp-port-unreachable 

3.新建規則文件

mkdir /etc/iptables #先新建目錄,自己無此目錄 vim /etc/iptables/rules.v4 

/etc/iptables/rules.v4 中的內容是vim

*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:syn-flood - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood -A INPUT -j REJECT --reject-with icmp-host-prohibited -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN -A syn-flood -j REJECT --reject-with icmp-port-unreachable COMMIT 

4.使防火牆生效

iptables-restore < /etc/iptables/rules.v4 

5.建立文件,添加如下內容,使防火牆開機啓動

vim /etc/network/if-pre-up.d/iptables 
#!/bin/bash iptables-restore < /etc/iptables/rules.v4 

6.添加執行權限

chmod +x /etc/network/if-pre-up.d/iptables 

7.查看規則是否生效

iptables -L -n

Ubuntu中沒有直接中止關閉iptables的命令,像service iptables stop這類命令,是centos纔有的。關閉的話,能夠暫時開放全部端口做爲替代方案centos

iptables -P INPUT ACCEPT  
iptables -P OUTPUT ACCEPT

還能夠參考https://blog.csdn.net/langsim/article/details/42644451ruby

做者:yangyangrenren 連接:https://www.jianshu.com/p/6fe337cedbcb 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。
相關文章
相關標籤/搜索