CentOSはインストール時に標準でfirewall機能が有効になっています。
新しくCentOS 7をインストールした時に、
firewall無効化の手順を調べてメモしたので書きおこしておきます。
環境
今回設定を変更する対象は、以下の環境で動作しているVirtual Machineとなります。
- 仮想化環境 : VMware Fusion 8.5.8
- ホストOS : OSX 10.13.1
- ゲストOS : CentOS 7
firewallが有効かどうかを確認
以下のコマンドを使って確認をしてみます。
1 |
$ sudo systemctl status firewalld.service |
1 2 3 4 5 6 7 8 9 10 11 12 |
[masa@localhost ~]$ sudo systemctl status firewalld.service [sudo] password for masa: ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 水 2017-12-06 08:11:11 JST; 2min 50s ago Docs: man:firewalld(1) Main PID: 711 (firewalld) CGroup: /system.slice/firewalld.service └─711 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 12月 06 08:11:10 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 12月 06 08:11:11 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon. [masa@localhost ~]$ |
やはり、何も設定しなくても有効なんですね。。。
今後検証環境を作成したり、Webアプリケーションなどの動作確認をする上で
トラブルの原因になりやすい箇所なので、検証目的であれば一時的に無効にしましょう。
ただし、本番環境に移行する際などは適切な設定を行ってからリリースしましょう。
firewalldを無効化する
一時的にfirewalldを無効化しておきたい場合は、以下のコマンドを利用するとよいでしょう。
1 |
$ sudo systemctl stop firewalld.service |
以下のメッセージのように、「inactive (dead)」と出ていれば停止しています。
1 2 3 4 5 6 7 8 9 10 11 12 |
[masa@localhost ~]$ sudo systemctl stop firewalld.service [masa@localhost ~]$ sudo systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: inactive (dead) since 水 2017-12-06 08:25:37 JST; 9s ago Docs: man:firewalld(1) Main PID: 711 (code=exited, status=0/SUCCESS) 12月 06 08:11:10 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 12月 06 08:11:11 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon. 12月 06 08:25:36 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon... 12月 06 08:25:37 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon. [masa@localhost ~]$ |
恒久的にfirewalldを無効化する設定
先ほどの設定のままでは、
サーバが再起動されるたびにfirewalldが起動されます。
私は推奨しませんが、もし自動起動を無効化したければ以下のコマンドを叩いてください。
1 |
$ sudo systemctl disable firewalld.service |
以下のようなメッセージが出力されたら、無事にfirewalldが無効化されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[masa@localhost ~]$ sudo systemctl disable firewalld.service Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [masa@localhost ~]$ sudo systemctl status firewalld.service ● 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) 12月 06 08:11:10 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 12月 06 08:11:11 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon. 12月 06 08:25:36 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon... 12月 06 08:25:37 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon. [masa@localhost ~]$ |
Loadedの行にdisabledが記述されているので、次回再起動時よりfirewalldは起動しなくなります!
無効にしてもサーバが起動しないときは。。。

CentOSではfirewalldとSELinuxが標準で動作するようになっています。
下記設定のように、SELinuxを無効化するように変更してみましょう。
1 |
# vim /etc/selinux/config |
1 2 3 4 5 6 7 8 9 10 11 12 |
# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. #SELINUX=enforcing # enforcingは遮断も行うモード SELINUX=permissive # permissiveはログだけ出力するモード , disabledは完全停止モード # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted |
この設定を保存したら、さらにコマンドを使って即時反映します。
1 2 |
# setenforce 0 # getenforce |
setenforceがSELinuxの設定を反映するコマンドで、
getenforceが設定状況を確認するコマンドです。