長年 サーバのフィルタリングにはiptablesを使ってきた。SentOS7ではフィルタリングはiptablesではなくfirewalldが標準で使用される。そこで長年愛用してきたiptablesを止めて firewalldを試してみた。
参照URL:https://centossrv.com/iptables.shtml
iptablesの設定は 上の参照先の設定をほぼそのまま利用させてもらっていた。このipteblesの設定で気に入ってるのは 怪しい国or危ない国からのアクセスをシャッタアウトできる所だ。firewalldでもこの機能は使いたい。調べてみるとipsetなるアプリケーションをfirewalldと組み合わせて使うと出来るらしい。ipsetのインストール&使用方法は後日別記事で紹介するとして今日はfirewalldを取りあえず使ってみる所まで紹介する。
注: この記事の内容を100%信用しないように! 我が家のサーバの仕様は CentOS7 + yumはベースリポジトリに加え epel とrpmforgeのリポジトリが追加されて自動的にインストールできるように設定されている。 Linuxやアプリケーションのバージョンや yumのリポジトリの設定の違いなどにより インストール方法や使用方法は違ってくる・・・ かもしれない。
・iptablesのアンインストールとfirewalldのインストール+実行
まずは今まで使ってきたiptablesをアンインストールするのだが、正確にはiptablesを制御するiptables-serviceというアプリを削除する。iptablesその物を削除してはいけない。実際のフィルタリングは今現在もiptablesで行われていてfirewalldで制御している。
[bash]
[]# systemctl disable iptables.service
[]# systemctl stop iptables.service
[]# yum erase iptables-service
[/bash]
次にfirewalldをインストールする。たぶんCentOSをインストールした状態で 既にfirewalldをインストールされてると思うが ここではここでは全く何もない状態からインストールして起動するまでの情報を書いておく。
[bash]
[]# yum install firewalld
[]# systemctl start firewalld
[]# systemctl enable firewalld
[/bash]
特に何も設定しなくても この状態ですべてのポートを閉じて dhcpv6-client,sshの二つのポートを待ち受け状態になる。この状況の確認や設定は firewall-cmdコマンドでできる。
[bash]
[]# firewall-cmd –list-all
public (default, active)
interfaces: enp0s3
sources:
services: dhcpv6-client ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
[]#
[/bash]
さらにwww,メール等の必要なポートを開けてやる。(メール関係は全てSSLで暗号化したポートを開けている。)
[bash]
[]# firewall-cmd –permanent –add-service=http
success
[]# firewall-cmd –permanent –add-service=https
success
[]# firewall-cmd –permanent –add-service=smtps
Error: INVALID_SERVICE: smtps
[]# firewall-cmd –permanent –add-service=pop3s
success
[]# firewall-cmd –permanent –add-service=imaps
success
[]# firewall-cmd –reload
[/bash]
最後の–reloadのコマンドで 設定が反映される。 ただし「smtpsというサービスが存在してない」って言ってるので これだけsmtp用の設定ファイルを修正して登録する。
[bash]
[root@lakhes services]# cp /usr/lib/firewalld/services/smtp.xml /etc/firewalld/services/smtps.xml
[root@lakhes services]# vi /etc/firewalld/services/smtps.xml
[/bash]
で以下のように内容を修正する。ポートを465に修正してあれば 名前とか説明はどうでもいい。
[code]
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>Mail (SMTPs)</short>
<description>This option allows incoming SMTP mail delivery. If you need to allow remote hosts to connect directly to your machine to deliver mail, enable this option. You do not need to enable this if you collect your mail from your ISP’s server by POP3 or IMAP, or if you use a tool such as fetchmail. Note that an improperly configured SMTP server can allow remote machines to use your server to send spam.</description>
<port protocol="tcp" port="465"/>
</service>
[/code]
ファイルの修正ができたら もう一度 smtpsを登録してみる。で ちゃんと登録できたかどうか確認してみる。
[bash]
[]# firewall-cmd –permanent –add-service=smtps
success
[]# firewall-cmd –reload
[]# firewall-cmd –list-all
public (default, active)
interfaces: enp0s3
sources:
services: dhcpv6-client http https imaps pop3s smtps ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
[]#
[/bash]
これで基本的な設定はできたんじゃないかな? 回線入り口のルーターでフィルタリングされているのでfirewalldは無くても良いっちゃ良いんだが・・・ 防壁を2重にしておけば少しは安全(かなぁ~~??)になるのかなと思って設定してみた。