原文鏈接:https://linuxize.com/post/how-to-install-iptables-on-centos-7/
原文作者:Linuxize
從CentOS 7開始,firewalld取代了iptables作為默認的防火墻管理工具。
FirewallD 是一個完整的防火墻解決方案,可以通過一個名為firewall-cmd的命令行工具來控制。如果您更熟悉Iptables命令行語法,那么您可以禁用FirewallD并退回到經典的iptables設定。
本教程將向您展示如何禁用FirewallD服務并安裝iptables。

先決條件
在開始本教程之前,您需要確保已登錄帶有sudo權限的用戶。
禁用FirewallD
在CentOS 7系統上禁用FirewallD,需要以下幾步:
- 輸入下面的命令,停止FirewallD服務:
$ sudo systemctl stop firewalld
- 禁用FirewallD服務,以免開機自動啟動:
$ sudo systemctl disable firewalld
- 屏蔽FirewallD服務,防止它被其他服務啟動:
$ sudo systemctl mask --now firewalld
安裝并啟用Iptables
在CentOS 7系統中,安裝Iptables的方法如下:
- 運行以下命令,安裝CentOS存儲庫中的
iptables-service包:
$ sudo yum install iptables-services
- 安裝完成后,啟動Iptables服務:
$ sudo systemctl start iptables
$ sudo systemctl start ip6tables
- 啟用Iptables服務,使其在系統啟動時自動啟動:
$ sudo systemctl enable iptables
$ sudo systemctl enable ip6tables
- 用下面的命令檢查Iptables服務:
$ sudo systemctl status iptables
$ sudo systemctl status ip6tables
- 使用下面的命令,檢查當前iptables規則:
$ sudo iptables -nvL
$ sudo ip6tables -nvL
默認情況下,只有SSH端口22是開放的,輸出應該如下:
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 5400 6736K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 2 148 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 3 180 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 4298 packets, 295K bytes) pkts bytes target prot opt in out source destination
至此,您已經成功地啟用了iptables服務,您可以構建您的防火墻了。這些變更將在機器重啟后持續存在。
結論
在本教程中,您學習了如何禁用FirewallD服務和安裝iptables。
如果您有任何問題或評論,請在下方留言。
譯者注:
這兩個服務確實不常用,有興趣的墨友可以試試看:
[shawnyan@centos7 ~]$ systemctl status iptables.service ● iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled) Active: inactive (dead) since Tue 2022-03-29 15:10:21 JST; 20h ago Process: 76583 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS) Process: 76560 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 76560 (code=exited, status=0/SUCCESS) [shawnyan@centos7 ~]$ systemctl status ip6tables.service ● ip6tables.service - IPv6 firewall with ip6tables Loaded: loaded (/usr/lib/systemd/system/ip6tables.service; disabled; vendor preset: disabled) Active: inactive (dead)
「喜歡這篇文章,您的關注和贊賞是給作者最好的鼓勵」
關注作者
【版權聲明】本文為墨天輪用戶原創內容,轉載時必須標注文章的來源(墨天輪),文章鏈接,文章作者等基本信息,否則作者和墨天輪有權追究責任。如果您發現墨天輪中有涉嫌抄襲或者侵權的內容,歡迎發送郵件至:contact@modb.pro進行舉報,并提供相關證據,一經查實,墨天輪將立刻刪除相關內容。




