ラズパイで作る自宅WEBサーバ:nginx編
第6回 ファイアウォール設定
2026.10.30
ブロードバンドルータ側で必要なポートのみを解放すれば
済んでしまうのですが、ブロードバンドルーター内側のラズベリーパイでもファイアウォールを設置して二重化しています。
●ufw の無効化
ufw(Uncomplicated FireWall)は、ファイアウォールの設定を行うコマンドですが、iptablesのラッパーなので無効化しておきます。
$ sudo ufw disable
Firewall stopped and disabled on system startup
●iptables インストール
$ dpkg --get-selections | grep iptables
iptables install
既にインストールされています。手動でインストールする場合は
$ sudo apt install iptables
初期設定の確認
$ sudo /usr/sbin/iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
何も設定されていないので、すべてのポートがACCEPTになっています。
iptables 設定用スクリプト(iptables.sh)の作成
iptablesはコマンドなので、シェルスクリプトを用意して、起動時に実行するようにします。
iptables コマンドをリモートで直接実行する場合は要注意です。間違えるとポートが塞がって回線が切断されてしまいます。
$ mkdir /home/ubuntu/scripts
$ cd /home/ubuntu/scripts
$ vi iptables.sh
#!/bin/bash
iptables -F ← 設定をクリア(chainのルールをすべて削除)
iptables -X ← ユーザー定義チェインを削除
iptables -P INPUT DROP ← ポリシー設定
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT ← SSH
iptables -A INPUT -p tcp --dport 25 -j ACCEPT ← SMTP
iptables -A INPUT -p tcp --dport 53 -j ACCEPT ← DNS
iptables -A INPUT -p tcp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT ← WEB(http)
iptables -A INPUT -p tcp --dport 110 -j ACCEPT ← POP
iptables -A INPUT -p udp --dport 123 -j ACCEPT ← NTP
iptables -A INPUT -p udp --sport 123 -j ACCEPT
iptables -A INPUT -p tcp --dport 143 -j ACCEPT ← IMAP
iptables -A INPUT -p tcp --dport 443 -j ACCEPT ← WEB(https)
iptables -A INPUT -p tcp --dport 587 -j ACCEPT ← SMTP Submission
iptables -A INPUT -i lo -j ACCEPT ← 自端末からの入力許可
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
↑こちらから求めたパケットは許可する
※iptablesは上から順番にルールが適用されます。
※iptables -F:これをリモート端末から直接実行すると通信が遮断されることがあります。
ping を飛ばして サーバを探索して、悪意ある攻撃をされることがあるので解放することは推奨されません。
解放するのであれば下記を追加します。
iptables -A INPUT -p icmp -j ACCEPT
ICMP ( Internet Control Message Protocol ) はインターネット層(OSI参照モデルのネットワーク層)で動作するプロトコルです。
パソコンからRealVNCで接続する場合は、5900番ポートを開放します。
iptables -A INPUT -p tcp --dport 5900 -j ACCEPT
Sambaのファイル共有を行う場合は下記の4ポートも開放します。
iptables -A INPUT -p tcp --dport 139 -j ACCEPT
iptables -A INPUT -p tcp --dport 445 -j ACCEPT
iptables -A INPUT -p udp --dport 137 -j ACCEPT
iptables -A INPUT -p udp --dport 138 -j ACCEPT
また、必要に応じて下記の許可も検討してみてください。
iptables -A INPUT -p tcp --dport 20 -j ACCEPT ← FTP(非推奨)
iptables -A INPUT -p tcp --dport 21 -j ACCEPT ← FTP(非推奨)
iptables -A INPUT -p tcp --dport 465 -j ACCEPT ← SMTPS(SMTP SSL)
iptables -A INPUT -p tcp --dport 993 -j ACCEPT ← IMAP(SSL)
iptables -A INPUT -p tcp --dport 995 -j ACCEPT ← POP(SSL)
スクリプトファイルのアクセス権限を設定します
$ sudo chmod 700 iptables.sh
フィルタを適応してみます
$ sudo ./iptables.sh
接続が遮断された場合は、再度、SSH接続します。
設定を確認します
$ sudo /sbin/iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:smtp
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp spt:domain
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT udp -- anywhere anywhere udp spt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp dpt:pop3
ACCEPT udp -- anywhere anywhere udp dpt:ntp
ACCEPT udp -- anywhere anywhere udp spt:ntp
ACCEPT tcp -- anywhere anywhere tcp dpt:imap2
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT tcp -- anywhere anywhere tcp dpt:submissions
ACCEPT tcp -- anywhere anywhere tcp dpt:submission
ACCEPT tcp -- anywhere anywhere tcp dpt:netbios-ssn
ACCEPT tcp -- anywhere anywhere tcp dpt:microsoft-ds
ACCEPT udp -- anywhere anywhere udp dpt:netbios-ns
ACCEPT udp -- anywhere anywhere udp dpt:netbios-dgm
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
システム起動時にスクリプトが実行されるように、サービス用のファイルを設定します。
$ sudo vi /lib/systemd/system/iptables.service
[Unit]
Description=iptables startup script
[Service]
Type=simple
ExecStart=/home/ubuntu/scripts/iptables.sh
Restart=no
[Install]
WantedBy=multi-user.target
※ファイル名は末尾が.serviceになるようにします。
サービスを有効にします
$ sudo systemctl enable iptables
Created symlink '/etc/systemd/system/multi-user.target.wants/iptables.service' → '/usr/lib/systemd/system/iptables.service'.
有効になると、/etc/systemd/system/multi-user.target.wants 配下に iptables.service のシンボリックリンクが作成されます
システムを再起動して、ステータスを確認します
$ sudo reboot
$ sudo systemctl status iptables
○ iptables.service - iptables startup script
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; preset: enabled)
Active: inactive (dead) since Mon 2026-07-06 16:31:35 JST; 1min 42s ago
Duration: 1.389s
Invocation: 7c12a7dc4a474df68434959d721a218a
Main PID: 591 (code=exited, status=0/SUCCESS)
CPU: 319ms
7月 06 16:31:33 ns.example.com systemd[1]: Started iptables.service - iptables startup script.
7月 06 16:31:35 ns.example.com systemd[1]: iptables.service: Deactivated successfully.
※備考 iptables.sh で、Restart=always を指定した場合
start request repeated to quickly
のエラーになります。
解放しているポートを調べるツールを組込みます
$ apt-cache show nmap
Package: nmap
Version: 7.95+dfsg-3
$ sudo apt-get -y install nmap
TCP ポートをスキャンします
$ sudo nmap localhost
Starting Nmap 7.94SVN ( https://nmap.org ) at 2026-07-03 19:30 JST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00015s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 996 closed tcp ports (reset)
PORT STATE SERVICE
22/tcp open ssh
53/tcp open domain
80/tcp open http
3306/tcp open mysql
|