CentOS7でのiptables設定方法について

執筆日:

更新日:

こんにちは、@mosuke5です。
最近、VPSのOSをCentOS7にしましたが、なかなか手付かずでiptablesの設定を放置していました。 sshの最低限の設定はしていたましたが、ほんとうに良くないですので、みなさんはこういうことがないように気をつけてください。
久しぶりに手が空いたので設定しようとしましたが、そもそも /etc/sysconfig/iptables がない。 CentOS7からのsystemctlでiptablesのサービスを確認してもでてこない。さてどうやって設定したらいいのかCentOS6の脳みそで困ってしまいました。

# systemctl status iptables
iptables.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

というわけで、調べてみると、そもそもCentOS7からはfirewalldが標準になっており、iptablesは入っていないとのことです。そのため、iptablesを利用する場合には、iptables.serviceをインスールしないといけません。 さらに、firewalldがデフォルトでオンになっているからオフにしないと競合してしまいます。

本当は、firewalldの設定方法を覚えたほうがいいですが、今までのやり方をそのままやりたい人むけにCentOS7でのiptablesの設定方法を残します。

まずは、iptables-serviceをインスールして、firewalldを停止、iptablesを起動します。

# yum install iptables-services
# systemctl status firewalld
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since Sat 2014-09-20 17:47:11 JST; 4s ago
 Main PID: 11162 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─11162 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid


# systemctl stop firewalld
# systemctl status firewalld
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: inactive (dead)

#systemctl disable firewalld
#systemctl enable iptables
#systemctl start iptables
#systemctl status iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled)
   Active: inactive (dead) since Sat 2014-09-20 17:47:10 JST; 2min 48s ago
  Process: 11139 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS)
  Process: 10096 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
 Main PID: 10096 (code=exited, status=0/SUCCESS)
....

iptablesの設定はいつもどおり。
CentOS7の新しいコマンドに戸惑ったので一部メモを以下に残します。

service, chkconfigコマンドはsystemctlコマンドへ

service, chkconfigコマンドは推奨されずsystemctlコマンドへ切り替わった。
試しにchkconfigコマンドを利用すると…

# chkconfig --list
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
pmcd           	0:off	1:off	2:off	3:off	4:off	5:off	6:off
pmie           	0:off	1:off	2:off	3:off	4:off	5:off	6:off
pmlogger       	0:off	1:off	2:off	3:off	4:off	5:off	6:off
pmmgr          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
pmproxy        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
pmwebd         	0:off	1:off	2:off	3:off	4:off	5:off	6:off

次のようなメッセージが出力されます。

If you want to list systemd services use 'systemctl list-unit-files'.

systemdのサービスを表示したければ、systemctl list-unit-filesを利用せよとのこと。 試しに打つと以下のように表示される

# systemctl list-unit-files
UNIT FILE                                   STATE
proc-sys-fs-binfmt_misc.automount           static
dev-hugepages.mount                         static
dev-mqueue.mount                            static
proc-fs-nfsd.mount                          static
proc-sys-fs-binfmt_misc.mount               static
sys-fs-fuse-connections.mount               static
sys-kernel-config.mount                     static
sys-kernel-debug.mount                      static
tmp.mount                                   disabled
var-lib-nfs-rpc_pipefs.mount                static
brandbot.path                               disabled
....
 

サービスの起動・停止・状態確認などはこちらでできます。

# service <service name> <start/stop/restart/status>
 ↓
# systemctl <start/stop/restart/status> <service name>

そして、サービスの起動オプションの設定はこちらです。

# chkconfig <service name> <on/off>
   ↓
# systemctl <enable/disable> <service name>

慣れてしまえば、CentOS7の新しいコマンド体系、systemdも怖くないです。
これからのことを考えて、いちはやく覚えてしまいましょう。

追記(2020/12/16)

先日、2020年12月8日に、CentOSプロジェクトの方針変更が発表されました。今までのRHELのリビルドとして活躍してきたCentOSは近く終わりを告げ、Red Hat Enterprise Linuxの出荷前のパッケージを共有するCentOS Streamに注力することが表明されました。こちらについては、さまざまな誤解が流れていたり、不安に思っている方も多いかと思います。 CentOS Streamとはなんなのか、以下が非常にわかりやすいので、正しい知識をつけて向き合っていってください。

記事の内容に関連した相談、仕事依頼したい New

記事の内容やクラウドネイティブ技術に関する相談、仕事依頼を開始しました。
仕事依頼、相談をしてみる

フィードバック

本記事に対して、フィードバックあればこちらのフォームからご記入ください。
記事の内容にフィードバックしてみる

このエントリーをはてなブックマークに追加