国別のIPアドレス制限(firewalld,ipset)

メールサーバが海外からのアクセスがうっとおしいので、日本だけの制限する。

いろいろググっていろんなパターンがあるのを参考にさせてもらいましたが、ゾーンを新しく作成してipsetを作ってファイルから読み込ませるのが自分的にすっきりしていたので、以下のような感じ。

#!/bin/sh
set -x

IPLIST=cidr.txt

#ホワイトリストなどあれば
touch ip.txt
#echo 127.0.0.1          > ip.txt
#echo xxx.xxx.xxx.xxx/24 >> ip.txt


#cidr.txt あれば一応退避
if [ -e $IPLIST ]; then
    mv $IPLIST "${IPLIST}_${date}"
fi

# IPリストを取得する
wget http://nami.jp/ipv4bycc/$IPLIST.gz
gunzip -d $IPLIST.gz

# JPだけ抜き出す
sed -n 's/^JP\t//p' $IPLIST >> ip.txt

# 新しいゾーンを作成
firewall-cmd --permanent --new-zone=JP
firewall-cmd --permanent --zone=JP --set-target=ACCEPT

# 制限したいサービスを作成したzoneにいれる
firewall-cmd --permanent --zone=JP --add-service=smtp
firewall-cmd --permanent --zone=JP --add-service=submission
firewall-cmd --permanent --zone=JP --add-service=pop3

# ipsetを作成し、ファイルから取り込む
# deleteはエラーがでますが作り直す用にいれてあります
firewall-cmd --permanent --delete-ipset=jp_ip
firewall-cmd --permanent --new-ipset=jp_ip --type=hash:net
firewall-cmd --permanent --ipset=jp_ip --add-entries-from-file=ip.txt
firewall-cmd --permanent --zone=JP --add-source=ipset:jp_ip

# firewalldの再読み込み
firewall-cmd --reload
firewall-cmd --list-all-zone

<参考URL>
firewalldで国内IPのみ許可(システムエクスプレス株式会社)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA