Linux中國

Linux 防火牆入門教程

合理的防火牆是你的計算機防止網路入侵的第一道屏障。你在家裡上網,通常互聯網服務提供會在路由中搭建一層防火牆。當你離開家時,那麼你計算機上的那層防火牆就是僅有的一層,所以配置和控制好你 Linux 電腦上的防火牆很重要。如果你維護一台 Linux 伺服器,那麼知道怎麼去管理你的防火牆同樣重要,只要掌握了這些知識你才能保護你的伺服器免於本地或遠程非法流量的入侵。

安裝防火牆

很多 Linux 發行版本已經自帶了防火牆,通常是 iptables。它很強大並可以自定義,但配置起來有點複雜。幸運的是,有開發者寫出了一些前端程序來幫助用戶控制防火牆,而不需要寫冗長的 iptables 規則。

在 Fedora、CentOS、Red Hat 和一些類似的發行版本上,默認安裝的防火牆軟體是 firewalld,通過 firewall-cmd 命令來配置和控制。在 Debian 和大部分其他發行版上,可以從你的軟體倉庫安裝 firewalld。Ubuntu 自帶的是 簡單防火牆 Uncomplicated Firewall (ufw),所以要使用 firewalld,你必須啟用 universe 軟體倉庫:

$ sudo add-apt-repository universe
$ sudo apt install firewalld

你還需要停用 ufw:

$ sudo systemctl disable ufw

沒有理由不用 ufw。它是一個強大的防火牆前端。然而,本文重點講 firewalld,因為大部分發行版都支持它而且它集成到了 systemd,systemd 是幾乎所有發行版都自帶的。

不管你的發行版是哪個,都要先激活防火牆才能讓它生效,而且需要在啟動時載入:

$ sudo systemctl enable --now firewalld

理解防火牆的域

Firewalld 旨在讓防火牆的配置工作儘可能簡單。它通過建立 zone 來實現這個目標。一個域是一組的合理、通用的規則,這些規則適配大部分用戶的日常需求。默認情況下有九個域。

  • trusted:接受所有的連接。這是最不偏執的防火牆設置,只能用在一個完全信任的環境中,如測試實驗室或網路中相互都認識的家庭網路中。
  • homeworkinternal:在這三個域中,接受大部分進來的連接。它們各自排除了預期不活躍的埠進來的流量。這三個都適合用於家庭環境中,因為在家庭環境中不會出現埠不確定的網路流量,在家庭網路中你一般可以信任其他的用戶。
  • public:用於公共區域內。這是個偏執的設置,當你不信任網路中的其他計算機時使用。只能接收選定的常見和最安全的進入連接。
  • dmz:DMZ 表示隔離區。這個域多用於可公開訪問的、位於機構的外部網路、對內網訪問受限的計算機。對於個人計算機,它沒什麼用,但是對某類伺服器來說它是個很重要的選項。
  • external:用於外部網路,會開啟偽裝(你的私有網路的地址被映射到一個外網 IP 地址,並隱藏起來)。跟 DMZ 類似,僅接受經過選擇的傳入連接,包括 SSH。
  • block:僅接收在本系統中初始化的網路連接。接收到的任何網路連接都會被 icmp-host-prohibited 信息拒絕。這個一個極度偏執的設置,對於某類伺服器或處於不信任或不安全的環境中的個人計算機來說很重要。
  • drop:接收的所有網路包都被丟棄,沒有任何回復。僅能有發送出去的網路連接。比這個設置更極端的辦法,唯有關閉 WiFi 和拔掉網線。

你可以查看你發行版本的所有域,或通過配置文件 /usr/lib/firewalld/zones 來查看管理員設置。舉個例子:下面是 Fefora 31 自帶的 FedoraWorkstation 域:

$ cat /usr/lib/firewalld/zones/FedoraWorkstation.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Fedora Workstation</short>
  <description>Unsolicited incoming network packets are rejected from port 1 to 1024, except for select network services. Incoming packets that are related to outgoing network connections are accepted. Outgoing network connections are allowed.</description>
  <service name="dhcpv6-client"/>
  <service name="ssh"/>
  <service name="samba-client"/>
  <port protocol="udp" port="1025-65535"/>
  <port protocol="tcp" port="1025-65535"/>
</zone>

獲取當前的域

任何時候你都可以通過 --get-active-zones 選項來查看你處於哪個域:

$ sudo firewall-cmd --get-active-zones

輸出結果中,會有當前活躍的域的名字和分配給它的網路介面。筆記本電腦上,在默認域中通常意味著你有個 WiFi 卡:

FedoraWorkstation
  interfaces: wlp61s0

修改你當前的域

要更改你的域,請將網路介面重新分配到不同的域。例如,把例子中的 wlp61s0 卡修改為 public 域:

$ sudo firewall-cmd --change-interface=wlp61s0 --zone=public

你可以在任何時候、任何理由改變一個介面的活動域 —— 無論你是要去咖啡館,覺得需要增加筆記本的安全策略,還是要去上班,需要打開一些埠進入內網,或者其他原因。在你憑記憶學會 firewall-cmd 命令之前,你只要記住了關鍵詞 changezone,就可以慢慢掌握,因為按下 Tab 時,它的選項會自動補全。

更多信息

你可以用你的防火牆干更多的事,比如自定義已存在的域,設置默認域,等等。你對防火牆越了解,你在網上的活動就越安全,所以我們創建了一個備忘單便於速查和參考。

via: https://opensource.com/article/20/2/firewall-cheat-sheet

作者:Seth Kenlon 選題:lujun9972 譯者:lxbwolf 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出


本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive

對這篇文章感覺如何?

太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0
雨落清風。心向陽

    You may also like

    4 Comments

    1. nordvpn 350fairfax
      Definitely believe that which you said. Your favorite reason appeared to be on the web the simplest thing to be aware of.
      I say to you, I certainly get irked while people think about worries that they just do not
      know about. You managed to hit the nail upon the top and defined out
      the whole thing without having side-effects , people could take
      a signal. Will likely be back to get more.
      Thanks

      Here is my website: nord Vpn Coupon Codes

    2. I like the valuable info you provide in your
      articles. I’ll bookmark your weblog and check again here frequently.
      I’m quite sure I’ll learn many new stuff right here! Best of luck for the next!

      Take a look at my web blog :: nordvpn coupons
      inspiresensation (http://s.bea.sh/)

    3. Heya i am for the first time here. I found this board and I find It truly useful & it helped
      me out a lot. I hope to give something back and help others
      like you helped me.

      Also visit my blog: nordvpn coupons inspiresensation

    4. Its like you learn my thoughts! You seem to grasp so much about this, such as you
      wrote the e book in it or something. I feel that you
      just could do with a few p.c. to drive the message home a
      bit, but instead of that, that is wonderful blog.

      A great read. I will definitely be back.

      My page – nordvpn coupons inspiresensation, t.co,

    Leave a reply

    您的郵箱地址不會被公開。 必填項已用 * 標註

    這個站點使用 Akismet 來減少垃圾評論。了解你的評論數據如何被處理

    More in:Linux中國