linux若是剛安裝好防火牆時咱們經常使用的端口是沒有增長的,也就是說不能訪問,那麼要怎麼把經常使用端口增長到防火牆經過狀態呢,下面咱們以80端口爲例子吧。linux
最近本身在學習Linux。搭建一個LNMP環境。在測試時一切都好。而後重啓Linux後。再次訪問網站沒法打開。最終緣由是在防火牆中沒有加入 80 端口的規則。具體方法以下:centos
在CentOS下配置iptables防火牆,是很是必要的。來咱們學習如何配置!,其它版本一下:tcp
1.打開iptables的配置文件:學習
代碼以下 | 複製代碼 |
vi /etc/sysconfig/iptables測試 |
經過/etc/init.d/iptables status網站
命令查詢是否有打開80端口,若是沒有可經過兩種方式處理:this
1.修改vi /etc/sysconfig/iptables命令添加使防火牆開放80端口spa
代碼以下 | 複製代碼 |
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT.net |
2.關閉/開啓/重啓防火牆rest
代碼以下 | 複製代碼 |
/etc/init.d/iptables stop #restart 重啓 |
添加好以後防火牆規則以下所示:
# 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 |
補充,有些朋友喜歡這樣作
代碼以下 | 複製代碼 |
vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(容許80端口經過防火牆) |
按照這種方法測試,發現重啓防火牆的時候,回報這兩行錯誤。
[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.
[失敗]
發現這種方法並很差使,因而嘗試另一種,經過命令去添加端口的方法。
代碼以下 | 複製代碼 |
[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 |
這樣就搞定了,查看效果
代碼以下 | 複製代碼 |
[root@centos httpd]# /etc/init.d/iptables status |