linux如果剛安裝好防火墻時我們常用的端口是沒有增加的,也就是說不能訪問,那么要怎么把常用端口增加到防火墻通過狀態呢,下面我們以80端口為例子吧。
最近自己在學習Linux。搭建一個LNMP環境。在測試時一切都好。然后重啟Linux后。再次訪問網站無法打開。最終原因是在防火墻中沒有加入 80 端口的規則。具體方法如下:
在CentOS下配置iptables防火墻,是非常必要的。來我們學習如何配置!,其它版本一下:
1.打開iptables的配置文件:
代碼如下
1
|
vi /etc/sysconfig/iptables |
通過/etc/init.d/iptables status
命令查詢是否有打開80端口,如果沒有可通過兩種方式處理:
1.修改vi /etc/sysconfig/iptables命令添加使防火墻開放80端口
代碼如下
1
|
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT |
2.關閉/開啟/重啟防火墻
代碼如下
1
2
3
|
/etc/init.d/iptables stop #start 開啟 #restart 重啟 |
添加好之后防火墻規則如下所示:
代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT -A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT -A INPUT -j REJECT –reject-with icmp-host-prohibited -A FORWARD -j REJECT –reject-with icmp-host-prohibited COMMIT /etc/init .d /iptables restart |
補充,有些朋友喜歡這樣做
代碼如下
1
2
3
|
vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允許80端口通過防火墻) -A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT(允許3306端口通過防火墻) |
按照這種方法測試,發現重啟防火墻的時候,回報這兩行錯誤。
1
2
3
4
5
6
7
8
|
[root@localhost ~]# /etc/init.d/iptables restart iptables:清除防火墻規則: [確定] iptables:將鏈設置為政策 ACCEPT:filter [確定] iptables:正在卸載模塊: [確定] iptables:應用防火墻規則:Bad argument `–-state' Error occurred at line: 11 Try `iptables-restore -h' or 'iptables-restore --help' for more information. [失敗] |
發現這種方法并不好使,于是嘗試另外一種,通過命令去添加端口的方法。
代碼如下
1
2
3
|
[root@centos httpd]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT [root@centos httpd]# /etc/rc.d/init.d/iptables save [root@centos httpd]# /etc/init.d/iptables restart |
這樣就搞定了,查看效果
代碼如下
1
|
[root@centos httpd]# /etc/init.d/iptables status |
總結
以上就是本文關于Linux中在防火墻中開啟80端口方法示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站:Linux企業運維人員常用的150個命令分享、淺談Linux的庫文件等,有什么問題可以隨時留言,小編會及時回復大家的。
原文鏈接:http://blog.csdn.net/panpan639944806/article/details/24969707