linux防火牆——iptables與firewall


CentOS7 的防火牆配置跟以前版本有很大區別,CentOS7這個版本的防火牆默認使用的是firewall,與之前的版本Centos 6.x使用iptables不一樣

一、iptables防火牆

1、基本操作

# 查看防火牆狀態

service iptables status  

# 停止防火牆

service iptables stop  

# 啟動防火牆

service iptables start  

# 重啟防火牆

service iptables restart  

# 永久關閉防火牆

chkconfig iptables off  

# 永久關閉后重啟

chkconfig iptables on  

2、開啟80端口

vim /etc/sysconfig/iptables
# 加入如下代碼
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
保存退出后重啟防火牆

service iptables restart

二、firewall防火牆

1、查看firewall服務狀態

systemctl status firewalld

出現Active: active (running)切高亮顯示則表示是啟動狀態。

出現 Active: inactive (dead)灰色表示停止,看單詞也行。
2、查看firewall的狀態

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

# 參數解釋
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

查看默認防火牆狀態(關閉后顯示notrunning,開啟后顯示running)
firewall-cmd --state

2.安裝iptables-services

yum install iptables-services

3.修改防火牆配置文件

vi /etc/sysconfig/iptables

默認的文件為:

 

在修改之前使用telnet命令查看端口是否開放。

1.啟動telnet。控制面板-->程序和功能-->打開或關閉windows功能-->勾選Telnet的兩個選項。
2.打開cmd窗口,輸入telnet,如果端口關閉或者無法連接,則顯示不能打開到主機的鏈接,鏈接失敗;端口打開的情況下,鏈接成功,則進入telnet頁面(全黑的),證明端口可用。

(1)telnet IP 端口。

(2)telnet 域名 端口。

 

 

如果成功連接會進入的界面

 

連接失敗

添加端口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
————————————————
版權聲明:本文為CSDN博主「wade3015」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/wade3015/article/details/90725871


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM