CentOS7.9 firewalldの設定を行う

[root@ホスト名 ~]# systemctl status firewalld
Unit firewalld.service could not be found. 👈サービス無い
[root@ホスト名 ~]#

CentOSのFaireWallを利用しようとstatus確認コマンドを入力したところ、上記のようなエラーになりました。これはインストールされてない状態となります。

インストールする

まずはインストールを行いましょう。

[root@ホスト名 ~]# yum -y install firewalld 👈FWをインストール
[root@ホスト名 ~]#
[root@ホスト名 ~]# systemctl start firewalld  👈FWを起動
[root@ホスト名 ~]#
[root@ホスト名 ~]# systemctl status firewalld  👈FW状態確認
[root@ホスト名 ~]#
[root@ホスト名 ~]# systemctl enable firewalld 👈サーバ再起動時に自動で起動する設定
[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --list-all  👈FW設定確認
[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --permanent --zone=public --add-service=http  👈http(Port番号80)を許可
[root@ホスト名 ~]# firewall-cmd --permanent --zone=public --add-service=https   👈https(Port番号443)を許可
[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --list-all  👈FW設定確認
[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --reload  👈FW再起動
[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --list-all  👈FW設定確認
[root@ホスト名 ~]#

実行すると下記のようになります。

[root@ホスト名 ~]# yum -y install firewalld 👈FWをインストール
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 5.6 kB 00:00
base: ftp.jaist.ac.jp
epel: ftp.jaist.ac.jp
extras: ftp.jaist.ac.jp
remi-safe: ftp.riken.jp
updates: ftp.jaist.ac.jp
base | 3.6 kB 00:00
epel | 4.7 kB 00:00
extras | 2.9 kB 00:00
mariadb | 3.4 kB 00:00
remi-safe | 3.0 kB 00:00
updates | 2.9 kB 00:00
(1/3): epel/x86_64/updateinfo | 1.0 MB 00:00
(2/3): epel/x86_64/primary_db | 7.0 MB 00:01
(3/3): updates/7/x86_64/primary_db | 15 MB 00:02
Resolving Dependencies
--> Running transaction check
---> Package firewalld.noarch 0:0.6.3-13.el7_9 will be installed
--> Processing Dependency: python-firewall = 0.6.3-13.el7_9 for package: firewalld-0.6.3-13.el7_9.noarch
--> Processing Dependency: firewalld-filesystem = 0.6.3-13.el7_9 for package: firewalld-0.6.3-13.el7_9.noarch
--> Processing Dependency: ipset for package: firewalld-0.6.3-13.el7_9.noarch
--> Processing Dependency: ebtables for package: firewalld-0.6.3-13.el7_9.noarch
--> Running transaction check
---> Package ebtables.x86_64 0:2.0.10-16.el7 will be installed
---> Package firewalld-filesystem.noarch 0:0.6.3-13.el7_9 will be installed
---> Package ipset.x86_64 0:7.1-1.el7 will be installed
--> Processing Dependency: ipset-libs(x86-64) = 7.1-1.el7 for package: ipset-7.1-1.el7.x86_64
--> Processing Dependency: libipset.so.13(LIBIPSET_4.8)(64bit) for package: ipset-7.1-1.el7.x86_64
--> Processing Dependency: libipset.so.13(LIBIPSET_2.0)(64bit) for package: ipset-7.1-1.el7.x86_64
--> Processing Dependency: libipset.so.13()(64bit) for package: ipset-7.1-1.el7.x86_64
---> Package python-firewall.noarch 0:0.6.3-13.el7_9 will be installed
--> Processing Dependency: python-slip-dbus for package: python-firewall-0.6.3-13.el7_9.noarch
--> Running transaction check
---> Package ipset-libs.x86_64 0:7.1-1.el7 will be installed
---> Package python-slip-dbus.noarch 0:0.4.0-4.el7 will be installed
--> Processing Dependency: python-slip = 0.4.0-4.el7 for package: python-slip-dbus-0.4.0-4.el7.noarch
--> Running transaction check
---> Package python-slip.noarch 0:0.4.0-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================

Package Arch Version Repository Size

Installing:
firewalld noarch 0.6.3-13.el7_9 updates 449 k
Installing for dependencies:
ebtables x86_64 2.0.10-16.el7 base 123 k
firewalld-filesystem noarch 0.6.3-13.el7_9 updates 51 k
ipset x86_64 7.1-1.el7 base 39 k
ipset-libs x86_64 7.1-1.el7 base 64 k
python-firewall noarch 0.6.3-13.el7_9 updates 355 k
python-slip noarch 0.4.0-4.el7 base 31 k
python-slip-dbus noarch 0.4.0-4.el7 base 32 k

Transaction Summary
Install 1 Package (+7 Dependent packages)

Total download size: 1.1 M
Installed size: 4.5 M
Downloading packages:
(1/8): firewalld-0.6.3-13.el7_9.noarch.rpm | 449 kB 00:00
(2/8): ebtables-2.0.10-16.el7.x86_64.rpm | 123 kB 00:00
(3/8): firewalld-filesystem-0.6.3-13.el7_9.noarch.rpm | 51 kB 00:00
(4/8): python-firewall-0.6.3-13.el7_9.noarch.rpm | 355 kB 00:00
(5/8): ipset-libs-7.1-1.el7.x86_64.rpm | 64 kB 00:00
(6/8): python-slip-0.4.0-4.el7.noarch.rpm | 31 kB 00:00
(7/8): ipset-7.1-1.el7.x86_64.rpm | 39 kB 00:00
(8/8): python-slip-dbus-0.4.0-4.el7.noarch.rpm | 32 kB 00:00

Total 1.8 MB/s | 1.1 MB 00:00

Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ebtables-2.0.10-16.el7.x86_64 1/8
Installing : ipset-libs-7.1-1.el7.x86_64 2/8
Installing : ipset-7.1-1.el7.x86_64 3/8
Installing : python-slip-0.4.0-4.el7.noarch 4/8
Installing : python-slip-dbus-0.4.0-4.el7.noarch 5/8
Installing : python-firewall-0.6.3-13.el7_9.noarch 6/8
Installing : firewalld-filesystem-0.6.3-13.el7_9.noarch 7/8
Installing : firewalld-0.6.3-13.el7_9.noarch 8/8
Verifying : ipset-7.1-1.el7.x86_64 1/8
Verifying : python-slip-dbus-0.4.0-4.el7.noarch 2/8
Verifying : firewalld-filesystem-0.6.3-13.el7_9.noarch 3/8
Verifying : firewalld-0.6.3-13.el7_9.noarch 4/8
Verifying : python-slip-0.4.0-4.el7.noarch 5/8
Verifying : python-firewall-0.6.3-13.el7_9.noarch 6/8
Verifying : ipset-libs-7.1-1.el7.x86_64 7/8
Verifying : ebtables-2.0.10-16.el7.x86_64 8/8

Installed:
firewalld.noarch 0:0.6.3-13.el7_9

Dependency Installed:
ebtables.x86_64 0:2.0.10-16.el7
firewalld-filesystem.noarch 0:0.6.3-13.el7_9
ipset.x86_64 0:7.1-1.el7
ipset-libs.x86_64 0:7.1-1.el7
python-firewall.noarch 0:0.6.3-13.el7_9
python-slip.noarch 0:0.4.0-4.el7
python-slip-dbus.noarch 0:0.4.0-4.el7

Complete!

[root@ホスト名 ~]#
[root@ホスト名 ~]# systemctl start firewalld  👈FWを起動
[root@ホスト名 ~]#
[root@ホスト名 ~]# systemctl status firewalld  👈FW状態確認
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-05-09 00:25:02 JST; 5min ago
Docs: man:firewalld(1)
Main PID: 514 (firewalld)
CGroup: /system.slice/firewalld.service
mq514 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopidMay 09 00:25:01 v133-18-236-15.vir.kagoya.net systemd[1]: Starting firewalld …
May 09 00:25:02 v133-18-236-15.vir.kagoya.net systemd[1]: Started firewalld -…
May 09 00:25:02 v133-18-236-15.vir.kagoya.net firewalld[514]: WARNING: AllowZ…
Hint: Some lines were ellipsized, use -l to show in full.
[root@ホスト名 ~]#
[root@ホスト名 ~]# systemctl enable firewalld 👈サーバ再起動時に自動で起動する設定
[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --list-all  👈FW設定確認
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --permanent --zone=public --add-service=http  👈http(Port番号80)を許可
success

[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --permanent --zone=public --add-service=https  👈https(Port番号443)を許可
success
[root@v133-18-236-15 ~]#
[root@v133-18-236-15 ~]# firewall-cmd --list-all  👈FW設定確認
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client ssh  👈httpとhttpsはFWを再起動しないと反映されない
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --reload  👈FW再起動し、設定を反映させる
success
[root@ホスト名 ~]#
[root@ホスト名 ~]# firewall-cmd --list-all  👈FW設定確認
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client http https ssh  👈httpとhttpsが反映されていることを確認
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@ホスト名 ~]#

以上です。最低限のhttpとhttpsは追加しました。

おまけ

その他の一般的に使うポート開放するのも記載しておきます。

firewall-cmd --permanent --zone=public --add-service=smtp
firewall-cmd --permanent --zone=public --add-service=pop3
firewall-cmd --permanent --zone=public --add-service=ssh
firewall-cmd --permanent --zone=public --add-port=22/tcp


ポート開放したものを削除するコマンドです。

firewall-cmd --permanent --zone=public --remove-service=smtp
firewall-cmd --permanent --zone=public --remove-service=pop3
firewall-cmd --permanent --zone=public --remove-service=ssh
firewall-cmd --permanent --zone=public --remove-port=22/tcp


FW機能を一時的に止める

systemctl stop firewalld

サーバが再起動後に自動起動しないようにするコマンドです。

systemctl disable firewalld

自動起動の設定が[enable]か[disable]か確認します。
enable:自動起動設定されている
disable:自動起動設定となっていない

systemctl is-enabled firewalld

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です