firewalld の設定(ホワイトリスト・実際編)
firewalls の設定(基本編) の続きです。引き続き、同じお題で説明します。
次に進む前に、基本編の例題をやった方で、ゾーン domestic
の --add-source
の設定がそのまま残っている場合は、消しておいてください。
$ sudo firewall-cmd --permanent --zone=domestic --remove-source=192.168.1.0/24 success $
日本国内に割り当てられた IP を firewalld
に設定するときに、--add-source
を繰り返すのはダルいし、パフォーマンス的にもよろしくないので、ipset
を使うと便利という話です。
$ sudo yum install -y ipset パッケージ ipset-6.19-6.el7.x86_64 はインストール済みか最新バージョンです $
わざわざインストールする必要がなくなっています。ピンと来た方もいるでしょう。
ネットで見つかる記事では、`ipset` を直にいじって設定していますが、今は `firewall-cmd` を使って `ipset` を設定できるようになっています。こちらを使う方がはるかに便利です。`ipset` の設定がサーバ再起動時に失われるので、その対策のためにこれまでは色々と設定する必要がありましたが、今では `firewalld` の各種設定と同様に `--permanent` を付けるだけで保存されるようになりました。
まずはセットの作り方です。日本国内に割り当てられた IP アドレスのセットとして、新しいセット `DOMESTIC` を作ります。 IP アドレスのリストは、テキストファイル `domestic.txt` から読み込ませます。
$ sudo firewall-cmd --permanent --new-ipset=DOMESTIC --type=hash:net success $ sudo firewall-cmd --permanent --ipset=DOMESTIC --add-entries-from-file=domestic.txt success $
`domestic.txt` は次のようなファイルです。このファイルの作り方は皆さんご存知ですよね?
1.0.16.0/20 1.0.64.0/18 1.1.64.0/18 1.5.0.0/16 1.21.0.0/16 ...
設定できているか確認します。
$ sudo firewall-cmd --permanent --info-ipset=DOMESTIC DOMESTIC type: hash:net options: entries: 103.224.8.0/22 103.8.48.0/22 124.159.0.0/16 42.144.0.0/13 103.199.220.0/22 203.120.45.0/24 157.7.128.0/17 160.248.128.0/19 163.209.0.0/16 103.37.116.0/22 203.174.64.0/20 202.164.240.0/20 61.86.0.0/16 27.134.188.0/24 192.47.184.0/21 161.96.0.0/16 192.244.0.0/16 183.181.76.0/22 221.96.0.0/12 163.49.0.0/16 ...
長いので、適当なところで割愛しますが、セット `DOMESTIC` は正しく作られています。
次は `firewalld` の設定です。セット `DOMESTIC` にマッチするアドレスからのアクセスは、ゾーン `domestic` で扱う設定にします。
$ sudo firewall-cmd --permanent --zone=domestic --add-source=ipset:DOMESTIC success $
あとは、設定を反映させましょう。
$ sudo firewall-cmd --reload success $ sudo firewall-cmd --list-all-zones ... domestic (active) target: default icmp-block-inversion: no interfaces: sources: ipset:DOMESTIC services: ssh ports: protocols: masquerade: no forward-ports: sourceports: icmp-blocks: rich rules: public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: dhcpv6-client http https ports: protocols: masquerade: no forward-ports: sourceports: icmp-blocks: rich rules: ...
はい、できました。簡単ですよね?
急いで書いたので分かりにくいところがあるかもしれません。遠慮なくコメントを付けてくださいね。