CentOS 7をはじめて使ってみて、あまりにもCentOS 6と違って戸惑いました。
詳しくは、以下の記事にまとめました。

今回は、CentOS 7から採用されたFirewalld (ファイアウォールのデーモン) でハマった話です。
目次
CentOS 7ではiptablesを使わない?
CentOS 7をインストール後にnginxをインストールしてnginxを起動しました。
外部からブラウザで
http://(ドメイン名 xxx)/
として開こうとしても表示されませんでした。
この種の問題は、過去の経験から iptables のせいだと確信して、サーバーで以下のコマンドを実行しました。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  | 
						# iptables --list ・・・ Chain INPUT_ZONES_SOURCE (1 references) target     prot opt source               destination          Chain INPUT_direct (1 references) target     prot opt source               destination          Chain IN_public (2 references) target     prot opt source               destination          IN_public_log  all  --  anywhere             anywhere             IN_public_deny  all  --  anywhere             anywhere             IN_public_allow  all  --  anywhere             anywhere             Chain IN_public_allow (1 references) target     prot opt source               destination          ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh ctstate NEW Chain IN_public_deny (1 references) target     prot opt source               destination          Chain IN_public_log (1 references) target     prot opt source               destination          Chain OUTPUT_direct (1 references) target     prot opt source               destination  | 
					
案の定、sshしかポートが空いていません。
しかし、よくよく調べてみたら、iptablesのサービスそのものは停止しているようです。
| 
					 1 2  | 
						# systemctl list-unit-files | grep iptables.service iptables.service                            disabled  | 
					
おかしい?
なんで、iptablesのサービスは止まっているのにiptablesそのものは動作しているように見えるのだろう。
CentOS 7からiptablesに代わり登場したFirewalld
調べたところ、CentOS 7ではiptablesに代わり、Firewalldという概念が登場したようです。
詳しくは、Firewalldはiptablesの代替というよりも、iptablesをバックエンドにゾーン(zone)という概念を入れて、フロントエンドで動く仕組みのようなものでしょうか。
そして、Firewalldの設定の方が、iptablesの設定よりも優先されるようです。
例えば、/etc/sysconfig/iptablesを書き換えて、
# systemctl restart iptables.service
としてみると、その時は、/etc/sysconfig/iptablesの設定を読み込んでくれます。
ただ、システムを再起動すると/etc/sysconfig/iptablesの設定を読み込んでくれません。
混乱するので、今後はCentOSのデフォルトの設定どおり、Firewalldをサービスとして起動させて、iptablesの方はサービスとしては停止させたままにしておきます。
Firewalldのよく使うコマンド
Firewalldで取り急ぎ覚えておくべきコマンドは以下の通りです。
# systemctl start firewalld   firewalldを起動
# systemctl stop firewalld    firewalldを停止
# systemctl enable firewalld    firewalldをサービスに登録
# systemctl disable firewalld    firewalldをサービスから解除
# firewall-cmd –list-all-zones   すべてのゾーンをリスト
# firewall-cmd –state    firewalldの状態を表示
# firewall-cmd –get-zones    ゾーンを取得
# firewall-cmd –list-all    現在のゾーンを表示
現在のゾーンを確認してみます。
| 
					 1 2 3 4 5 6 7 8 9 10  | 
						# firewall-cmd --list-all public (default, active)   interfaces: eno16777736   sources:    services: dhcpv6-client ssh   ports:    masquerade: no   forward-ports:    icmp-blocks:    rich rules:  | 
					
現在、publicがデフォルトの設定であるとわかります。
Firewalldの設定ファイル
Firewalldの設定ファイルは、
/etc/firewalld/zones
にゾーンごとにxml形式で保存します。
既存の
/etc/firewalld/zones/public.xml
を修正して、
<service name=”http”/>
を追加します。
| 
					 1 2 3 4 5 6 7 8  | 
						<?xml version="1.0" encoding="utf-8"?> <zone>   <short>Public</short>   <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>   <service name="dhcpv6-client"/>   <service name="ssh"/>   <service name="http"/> </zone>  | 
					
上記の場合、ssh(ポート22)とhttp(ポート80)が有効になります。
この後、
# systemctl restart firewalld
を実行するか、またはシステムを再起動したら、無事に
http://(ドメイン名 xxxx)/
でブラウザから接続できました。
CentOS7 のサーバー設定のおすすめの本
↓CentOS7のおすすめの本はコチラ
Systemd・firewalldの導入、NetworkManagerへの完全移行、新しいアプリケーションと仮想化をはじめ、すべてが新しくなったサーバ構築を徹底解説!
最後まで読んでいただきありがとうござました。
この記事が気に入っていただけたらシェアしてくれると嬉しいです。

  
  
  
  

コメント
[…] CentOS 7からデフォルトのFirewalldでハマった(iptablesは今後使わない?)CentOS7でポートを開放する方法。 […]
[…] CentOS 7からデフォルトのFirewalldでハマった(iptablesは今後使わない?) CentOS7でポートを開放する方法。 […]
[…] CentOS 7からデフォルトのFirewalldでハマった(iptablesは今後使わない?違い?) … […]
[…] CentOS 7からデフォルトのFirewalldでハマった(iptablesは今後使わない?違い?) … […]