防火牆設置與配置開放端口

一、iptables防火牆

1、 查看防火牆狀態

service iptables status

出現Active: active (running)高亮顯示則表示是啓動狀態。
出現 Active: inactive (dead)灰色表示停止狀態。

2、 CentOS6關閉防火牆使用以下命令:

# 臨時關閉
service iptables stop
# 禁止開機啓動
chkconfig iptables off
# 重啓防火牆
service iptables restart  

3、 CentOS7關閉防火牆使用以下命令:

// 臨時關閉
systemctl stop firewalld
// 禁止開機啓動
systemctl disable firewalld

4、 開啓80等端口

vim /etc/sysconfig/iptables

加入如下代碼

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

或者使用此命令:

iptables -A INPUT -p tcp --dport 8080 -j ACCEPT 

保存退出後重啓防火牆

service iptables restart

查看打開的端口

/etc/init.d/iptables status

打開49152~65534之間的端口

iptables -A INPUT -p tcp --dport 49152:65534 -j ACCEPT  

【注】參數說明:

–A 參數就看成是添加一條規則
–p 指定是什麼協議,我們常用的tcp 協議,當然也有udp,例如53端口的DNS
–dport 就是目標端口,當數據從外部進入服務器爲目標端口
–sport 數據從服務器出去,則爲數據源端口使用
–j 就是指定是 ACCEPT -接收 或者 DROP 不接收

二、firewall防火牆

1、查看firewall服務狀態

systemctl status firewalld

出現Active: active (running)高亮顯示則表示是啓動狀態。
出現 Active: inactive (dead)灰色表示停止狀態。

2、查看firewall的狀態

systemctl status firewalld 或者 firewall-cmd --state

3、開啓、重啓、關閉 firewalld.service服務

開啓

service firewalld start

重啓

service firewalld restart

關閉

service firewalld stop

4、查看防火牆規則

firewall-cmd --list-all 

5、查詢、開放、關閉端口

查詢端口是否開放

firewall-cmd --query-port=8080/tcp

開放80端口

firewall-cmd --permanent --add-port=80/tcp

移除端口

firewall-cmd --permanent --remove-port=8080/tcp

重啓防火牆(修改配置後要重啓防火牆)

firewall-cmd --reload

6.自啓動

開機啓動

systemctl enable firewalld

停止並禁用開機啓動

sytemctl disable firewalld

7.查看版本

firewall-cmd --version

8.查看幫助

firewall-cmd --help

9.查看區域信息

firewall-cmd --get-active-zones

10.查看指定接口所屬區域信息

firewall-cmd --get-zone-of-interface=eth0

11.拒絕所有包

firewall-cmd --panic-on

12.取消拒絕狀態

firewall-cmd --panic-off

13.查看是否拒絕

firewall-cmd --query-panic

14.將接口添加到區域(默認接口都在public)
(永久生效再加上 --permanent 然後重啓防火牆)

firewall-cmd --zone=public --add-interface=eth0

15.設置默認接口區域
(立即生效,無需重啓)

firewall-cmd --set-default-zone=public

16.更新防火牆規則
(兩者的區別就是第一個無需斷開連接,就是firewalld特性之一動態添加規則,第二個需要斷開連接,類似重啓服務)

firewall-cmd --reload或firewall-cmd --complete-reload

17.查看指定區域所有打開的端口

firewall-cmd --zone=public --list-ports

18.在指定區域打開端口(記得重啓防火牆)
(永久生效再加上 --permanent)

firewall-cmd --zone=public --add-port=80/tcp

【注】:「 參數解釋」

–zone 作用域
–add-port=8080/tcp 添加端口,格式爲:端口/通訊協議
–permanent #永久生效,沒有此參數重啓後失效

1、firwall-cmd:是Linux提供的操作firewall的一個工具;
2--permanent:表示設置爲持久;
3--add-port:標識添加的端口;

【注】CentOS7 默認使用firewalld防火牆

如果想換回iptables防火牆,可關閉firewalld並安裝iptables。

1、關閉firewall:

  • 停止firewall:systemctl stop firewalld.service
  • 禁止firewall開機啓動:systemctl disable firewalld.service
  • 查看默認防火牆狀態:firewall-cmd --state(關閉後顯示notrunning,開啓後顯示running)

2.安裝iptables-services

yum install iptables-services

3.修改防火牆配置文件

vi /etc/sysconfig/iptables

添加端口80、8080、3306、3690端口:

esc :wq! 退出保存修改。

【注】:添加在端口22上面或者下面,不要放在最後,不然不起作用。

4.重啓防火牆使配置生效

systemctl restart iptables.service

剛剛yum install iptables.service之後系統如果沒有重啓,iptables.service是找不到的,會報unit not fount。
設置防火牆開機啓動:

systemctl enable iptables.service
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章