CentOS7 firewalld 事始め

f:id:arcright:20150215143711p:plain
これまでiptablesを用いたファイアーウォール構築の際は、iptables設定ファイルを直接書き換えたりして管理していたがCentOS7からはfirewalldというwrapperを用いたiptables管理ができるようになった。

firewalldについてわかったことをとりあえず書くと、定義されているzonesとNICが関連付き、zonesには通信を許可するservicesを関連付けてパケットフィルタリングを行う仕組みのようである。
つまりNICごとにファイアーウォールを管理することができる?ということである。

HTTP通信を許可したい場合の例

設定の確認

firewall-cmdというコマンドラインインターフェースが用意されているので徐々に覚えていく。

# firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

eth0というNICはpuclic zoneになっているのでpuclic zoneで許可されているserviceであるdhcpv6-client,sshが記載されていることがわかる。

設定ファイルの場所

デフォルト設定

/usr/lib/firewalld配下にzonesとservicesというディレクトリがあるがこれがデフォルトの設定ファイル群。

/usr/lib/firewalld/zones/public.xml

<?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="ssh"/>
  <service name="dhcpv6-client"/>
</zone>

拡張子からもわかる通りxmlファイルである。ここにssh,dhcpv6-clientがあるということで大体意味はわかっただろう。
そしてsevicesディレクトリにはサービスの設定ファイルがある。

/usr/lib/firewalld/services/ssh.xml

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="22"/>
</service>

通信プロトコルと使用するポートが書いてある。これが許可する通信及びポート設定ということになる。分かりやすい。

ユーザ設定

設定を変更したい場合どうするか。その手段は用意されているので作法に従おう。

/etc/firewalld配下に同じディレクトリ構成でzones,servicesディレクトリがあると思うのでここに元ファイルと同名のファイル名で配置するとこちらが優先される。

ユーザ設定する場合はコマンドで行う場合と直接ファイルを作成して配置する場合がある。

設定コマンド
# firewall-cmd --permanent --add-service=http

permanentは永久にといった意味だが、ここでは設定ファイルの更新を行いたい場合にはこのオプションを使う。つけないとサーバ再起動した際には戻ってしまう一時的な変更にしかならないということだ。

このコマンドを実行したところ/etc/firewalld/zones配下にpublic.xml及びpublic.xml.oldが作成されていた。
add-serviceオプションで指定する文字列はservicesディレクトリに存在するサービス名でないとエラーになるようだ。

/etc/firewalld/zones/public.xml

<?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="http"/>
  <service name="ssh"/>
</zone>

httpが追加された設定ファイルが作成されていた。

設定反映,確認
# systemctl reload firewalld

これで設定反映完了。firewall-cmdにも設定反映コマンドがあるみたいだけどこっちでもできた。どっちがいいのかはよくわからない。

# firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client http ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

httpサービスが追加されたことがわかる。これで外部から80ポートにアクセスしてみるとページが表示されるはずだ。