簡單使用CentOS7防火牆及開放端口

概述(官方有更爲詳細的說明哦)

Firewalld提供動態管理的防火牆,支持網絡/防火牆區域,用於定義網絡鏈接或接口的信任級別。它支持IPv4,IPv6防火牆設置,以太網橋和IP集。運行時和永久配置選項分開。它還爲服務或應用程序提供了直接添加防火牆規則的接口。
既然是簡單使用, 那必須教會你怎麼查看防火牆狀態, 以及防火牆的關閉和開啓之類的, 咱們都知道那既然防火牆都開啓了, 那麼它一定是有一些端口的限制, 不能說你想經過哪些端口就經過哪些端口, 就能訪問到咱們的主機(也就是服務器了), 換句說話, 我開放了哪些端口, 你才能夠經過我開放的這些端口對個人主機進行訪問, 要否則咱們還要防火牆幹嗎, 誰想進來就進來(仍是經過任意端口), 防火牆不就成了擺設不成, 說到防火牆要還要多說一句, 防火牆有軟件層面的和物理設置層面的(網絡內的一臺專業物理設備), 那咱們這裏是所說的是軟件層面的防火牆也就是 Firewalld, CentOS7如下版本廣泛使用的軟件防火牆是 Firewalld, 爲何說是CentOS7如下版本廣泛使用的呢? 這是由於以前的CentOS版本都是使用的 iptables防火牆, 那要往深處說, 這就要追溯到CentOS7的發展歷程及RedHad之間的關係啦, 扯遠了哈, 咱們使用防火牆的主要目的是對咱們的主機進行安全管理, 其中主要的一部分就是端口管理了!

查詢防火牆狀態

shell
# systemctl status firewalld
execute
[warnerwu@localhost ~]$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

啓動防火牆

注意:防火牆只有管理員或管理員用戶組有權限進行管理操做, 普通用戶則不能夠python

shell
# systemctl start firewalld
execute
[root@localhost~]# systemctl start firewalld
[root@localhost~]#

你會發現什麼也沒有, 玩 linuxosx 系統的小夥伴都知道, 沒有提示那就是最好的提示, 說明已經成功啦, 有提示時則一會有問題哦linux

再次查看防火牆狀態

shell
# systemctl status firewalld
execute
[root@localhost ~]# sudo systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since 五 2018-09-07 00:04:55 CST; 5min ago
     Docs: man:firewalld(1)
 Main PID: 11339 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─11339 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

9月 07 00:04:55 izj6c3bcx7adgva5dda2e0z systemd[1]: Starting firewalld - dynamic firewall daemon...
9月 07 00:04:55 izj6c3bcx7adgva5dda2e0z systemd[1]: Started firewalld - dynamic firewall daemon.
9月 07 00:04:55 izj6c3bcx7adgva5dda2e0z firewalld[11339]: WARNING: ICMP type 'beyond-scope' is not supported by the kernel for ipv6.
9月 07 00:04:55 izj6c3bcx7adgva5dda2e0z firewalld[11339]: WARNING: beyond-scope: INVALID_ICMPTYPE: No supported ICMP type., ignorin...-time.
9月 07 00:04:55 izj6c3bcx7adgva5dda2e0z firewalld[11339]: WARNING: ICMP type 'failed-policy' is not supported by the kernel for ipv6.
9月 07 00:04:55 izj6c3bcx7adgva5dda2e0z firewalld[11339]: WARNING: failed-policy: INVALID_ICMPTYPE: No supported ICMP type., ignori...-time.
9月 07 00:04:55 izj6c3bcx7adgva5dda2e0z firewalld[11339]: WARNING: ICMP type 'reject-route' is not supported by the kernel for ipv6.
9月 07 00:04:55 izj6c3bcx7adgva5dda2e0z firewalld[11339]: WARNING: reject-route: INVALID_ICMPTYPE: No supported ICMP type., ignorin...-time.
Hint: Some lines were ellipsized, use -l to show in full.

嗯嗯, 你會發如今已經在運行啦, 就是這樣子哦, 還要多說一句, 那若是你對 CentOS7RedHat7 足夠了解的話, 你會很熟悉 systemctl是個什麼東東, 她呀, 就是系統服務管理工具, 是系統工具, 用來管理系統服務的, 就這麼理解就能夠不會錯的! 以前的CentOS版本都是使用的 service 進行系統服務管理的哦, 關於更多 systemctlservice 的信息請動手 百度Google, 咱們簡單的說完了, 查看防火牆狀態以及怎麼啓動防火牆, 那咱們的就來講說怎麼來開放端口!shell

查看防火牆開放端口列表

防火牆的端口管理是經過 firewall-cmd 命令來進行管理的哦, 這一點必需要明確, 也是你管理系統端口的關鍵所在, 嗯嗯, 是她, 是她, 就是她, 她就是這麼的如此風騷, 哈哈, 你拿她一點辦法都沒有, 不你能夠把她玩的服服貼貼的, 什麼呀!你想多了, 我什麼也沒有說哦, 是你本身這樣想的~安全

shell
# firewall-cmd --list-all
execute
[root@localhost ~]# firewall-cmd --list-all
public
  target: default
  icmp-block-inversion: no
  interfaces:
  sources:
  services: ssh dhcpv6-client
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

你會發現它如此空曠, 沒有任何開放端口, 嗯, 這是一臺新裝linux系統, 你的也許會跟我這個同樣, 也許會不同, 這個要注意一下服務器

添加開放端口到防火牆

好比咱們讓80端口開放, 其它用戶能夠訪問個人站點網絡

shell
// Step1: 加入開放端口到配置文件

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

--zone=public 添加時區

--add-port=80/tcp 添加端口

--permanent 永久生效

// 加載防火牆新配置文件( 以 root 身份輸入如下命令,從新加載防火牆,並不中斷用戶鏈接,即不丟失狀態信息. )

firewall-cmd --reload

再次查看防火牆開放端口列表

shell
# firewall-cmd --list-all
execute
[root@localhost ~]# firewall-cmd --list-all
public
  target: default
  icmp-block-inversion: no
  interfaces:
  sources:
  services: ssh dhcpv6-client
  ports: 80/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

此次你會發現 ports 對應的多了一個 80/tcp, 那說明就已經添加到了防火牆開放列表中了ssh


更新: 2018-11-10

開機自動啓動firewalld防火牆服務

檢測firewalld服務是否開機自動啓動

$ systemctl list-unit-files | grep firewall
firewalld.service                             disabled

開啓開機自動啓動firewalld服務

$ systemctl enable firewalld.service

檢測firewalld服務是否開機自動啓動

$ systemctl list-unit-files | grep firewall
firewalld.service                             enabled

關閉開機自動啓動firewalld服務

$ systemctl disable firewalld.service

但願本文對你的工做和學習有所幫助

若是以爲還不錯怎麼感謝我呢? 媽呀! 點贊啊!tcp

Good Luck! from warnerwu at 2018.09.07 AM工具

相關文章
相關標籤/搜索