CentOS各個版本中的命令有所不同,使用起來比較麻煩,感覺有點亂糟糟的,在此總結(jié)一下防火墻在不同CentOS中的相關(guān)指令。
CentOS6.5查看防火墻的狀態(tài):
service iptable status
顯示結(jié)果:
Redirecting to /bin/systemctl status iptable.service
iptable.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead) --表示防火墻已經(jīng)關(guān)閉
CentOS 6.5關(guān)閉防火墻
[root@localhost ~]#servcie iptables stop --臨時關(guān)閉防火墻
[root@localhost ~]#chkconfig iptables off --永久關(guān)閉防火墻
CentOS 7.2關(guān)閉防火墻
CentOS 7.0默認(rèn)使用的是firewall作為防火墻,這里改為iptables防火墻步驟。
firewall-cmd --state
查看默認(rèn)防火墻狀態(tài)(關(guān)閉后顯示notrunning,開啟后顯示running)
檢查防火墻的狀態(tài):
從centos7開始使用systemctl來管理服務(wù)和程序,包括了service和chkconfig。
systemctl list-unit-files|grep firewalld.service
或者
systemctl status firewalld.service
** 關(guān)閉防火墻:**
systemctl stop firewalld.service
停止firewall
systemctl disable firewalld.service
禁止firewall開機啟動
啟動一個服務(wù):systemctl start firewalld.service
關(guān)閉一個服務(wù):systemctl stop firewalld.service
重啟一個服務(wù):systemctl restart firewalld.service
顯示一個服務(wù)的狀態(tài):systemctl status firewalld.service
在開機時啟用一個服務(wù):systemctl enable firewalld.service
在開機時禁用一個服務(wù):systemctl disable firewalld.service
查看服務(wù)是否開機啟動:systemctl is-enabled firewalld.service;echo $?
查看已啟動的服務(wù)列表:systemctl list-unit-files|grep enabled
Centos 7 firewall 命令:
查看已經(jīng)開放的端口:
firewall-cmd --list-ports
開啟端口
firewall-cmd --zone=public --add-port=80 /tcp --permanent
命令含義:
–zone #作用域
–add-port=80/tcp #添加端口,格式為:端口/通訊協(xié)議
–permanent #永久生效,沒有此參數(shù)重啟后失效
重啟防火墻
firewall-cmd --reload
重啟firewall
systemctl stop firewalld.service
停止firewall
systemctl disable firewalld.service
禁止firewall開機啟動
firewall-cmd --state
查看默認(rèn)防火墻狀態(tài)(關(guān)閉后顯示notrunning,開啟后顯示running)
CentOS 7 以下版本 iptables 命令
如要開放80,22,8080 端口,輸入以下命令即可
/sbin/iptables
-I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables
-I INPUT -p tcp --dport 22 -j ACCEPT
/sbin/iptables
-I INPUT -p tcp --dport 8080 -j ACCEPT
然后保存:
/etc/rc .d /init .d /iptables save
查看打開的端口:
/etc/init .d /iptables status
關(guān)閉防火墻
1) 永久性生效,重啟后不會復(fù)原
開啟: chkconfig iptables on
關(guān)閉: chkconfig iptables off
2) 即時生效,重啟后復(fù)原
開啟: service iptables start
關(guān)閉: service iptables stop
查看防火墻狀態(tài): service iptables status
下面說下CentOS7和6的默認(rèn)防火墻的區(qū)別
CentOS 7默認(rèn)使用的是firewall作為防火墻,使用iptables必須重新設(shè)置一下
1、直接關(guān)閉防火墻
systemctl stop firewalld.service
停止firewall
systemctl disable firewalld.service
禁止firewall開機啟動
2、設(shè)置 iptables service
yum -y install iptables-services
如果要修改防火墻配置,如增加防火墻端口3306
vi /etc/sysconfig/iptables
增加規(guī)則
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
保存退出后
systemctl restart iptables.service
重啟防火墻使配置生效
systemctl
enable
iptables.service
設(shè)置防火墻開機啟動
最后重啟系統(tǒng)使設(shè)置生效即可。
systemctl start iptables.service
打開防火墻
systemctl stop iptables.service
關(guān)閉防火墻
解決主機不能訪問虛擬機CentOS中的站點
防火墻將80端口屏蔽引起的錯誤。
檢查是不是服務(wù)器的80端口被防火墻堵了,可以通過命令:telnet server_ip 80 來測試。
解決方法如下:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
然后保存:
/etc/rc .d /init .d /iptables save
重啟防火墻
/etc/init .d /iptables restart
CentOS防火墻的關(guān)閉,關(guān)閉其服務(wù)即可:
查看CentOS防火墻信息:/etc/init.d/iptables status
關(guān)閉CentOS防火墻服務(wù):/etc/init.d/iptables stop