茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671956 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
WAVE_LS_SW_00042.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]firewalld - CentOS 7 的動態防火牆

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]firewalld - CentOS 7 的動態防火牆

firewalld - CentOS 7 的動態防火牆

如同先前在 [完全用 GNU/Linux 工作] 07. 簡易的防火牆一文提到,Ubuntu 從 8.04 LTS 開始多了 ufw 這個簡單的防火牆,CentOS 也在 7.0 導入可以動態管理防火牆的 firewalld。Weithenn 前輩的 CentOS 7.3 基礎設定 (10) - 調整 Firewalld 防火牆規則一文很好的解說了 firewalld 與 iptables 的差異,想深入了解的伙伴不妨參考一下。

firewalld 還有個跟 Windows 7 類似的網路共用設定機制,在官方文件中是使用界域 1 (zone) 來敘述這項依據不同環境所設計的網路區域功能。我們可以藉由在各種不同的 zone 進行設置,來達到公開網域 (public network zone) 不開 ssh 埠口,而內部網域 (internal network zone) 則允許等較複雜的信任等級配置。這部份凍仁還未深入研究,在此就不多加說明。


1. 安裝與初始化

1.1. 若系統未預載,請手動安裝 firewalld
[ jonny @CentOS ~ ]
$ sudo yum install firewalld [Enter]

1.2. 檢查 firewalld 服務運行狀態。
[ jonny @CentOS ~ ]
$ systemctl status firewalld [Enter]

● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2017-09-24 09:03:55 UTC; 14min ago
Docs: man:firewalld(1)
Main PID: 4376 (firewalld)
CGroup: /system.slice/firewalld.service
└─4376 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid


1.3. 若狀態不為 active,則啟動 (start) 它。firewalld 預設會開 ssh 服務,不用再擔心啟動防火牆時,會被 iptables 擋在牆外了!
[ jonny @CentOS ~ ]
$ sudo systemctl start firewalld [Enter]

1.4. 若需開機自動啟動,則還需啟用 (enable) 它。
[ jonny @CentOS ~ ]
$ sudo systemctl enable firewalld [Enter]

Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.


2. 基礎操作

2.1. 狀態 (state) 查詢,running 表示正常運行中。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --state [Enter]
running

2.2. 新增允許 HTTP 服務連線規則。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --add-service= http --permanent [Enter]
success

2.3. 新增允許 10050 埠口 TCP 連線規則。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --add-port= 10050/tcp --permanent [Enter]
success

2.4. 刪除允許 dhcpv6-client 服務連線規則。
[
jonny @CentOS ~ ]
$ sudo firewall-cmd --remove-service= dhcpv6-client \
    --permanent [Enter]
success

2.5. 刪除允許 10050 埠口的 TCP 連線規則。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --remove-port= 10050/tcp --permanent [Enter]
success

2.6. 重新讀取防火牆規則。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --reload [Enter]
success

2.7. 列出當前的防火牆規則:眼尖的客官可以發現,以下範例為 public 這個 network zone 的配置。
[ jonny
@CentOS ~ ]
$ sudo firewall-cmd --list-all [Enter]
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources:
  services: http ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  sourceports:
  icmp-blocks:
  rich rules:

3. 進階使用

3.1. 允許 (accept) 特定 IP 連線

3.1.1. 允許來自 192.168.1.2 的所有連線。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --add-rich-rule 'rule family="ipv4" \
    source address=" 192.168.1.2/32" accept' --permanent [Enter]

3.1.2. 允許來自 192.168.2.1 ~ 192.168.2.254 的所有 ssh 連線。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --add-rich-rule 'rule family="ipv4" \
    source address=" 192.168.2.0/24" service name=" ssh" accept' \
   --permanent [Enter]

3.2. 拒絕 (reject) 或阻擋 (drop) 特定 IP 連線

3.2.1. 拒絕來自 110.88.4.5 的連線。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --add-rich-rule 'rule \
    family="ipv4" source address=" 110.88.4.5/32" reject' \
    --permanent [Enter]

3.2.2. 阻擋來自 27.16.3.1 ~ 27.16.3.254 的 22 埠口連線。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --add-rich-rule 'rule \
    family="ipv4" source address=" 27.16.3.0/24" port port=" 22" \
    drop' --permanent [Enter]

3.3. 允許特定埠口連線

3.3.1. 允許 32700 到 33000 埠口的 TCP 連線規則。
[ jonny @CentOS ~ ]
$ sudo firewall-cmd --add-port= 32700-33000/tcp --permanent [Enter]

3.4. 圖形化介面

大家可另外安裝 firewall-config 套件進行操作。
[ jonny @CentOS
~ ]
$ sudo yum install firewall-config [Enter]

近來凍仁總覺得 CentOS 7 的 firewalld 有些用不慣,索性參照之前 ufw 的筆記,把常用的 firewalld 防火牆指令整理了一下,將來有遇上比較特殊的用法再補上了。

1 經友人 Cheng-Chia Tseng 回饋,GNU/Linux 官方是使用「界域」一詞來形容此項功能。然而很棒的一點是在 20.1.4 防火牆簡易設定 | 鳥哥的 Linux 私房菜一文也有很完整的解說呢!


原文出處:firewalld - CentOS 7 的動態防火牆 | 凍仁的筆記
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|