2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raspberry Piのセキュリティ強化:ufw、Fail2Ban、不要なサービスの停止

Posted at

Raspberry Piをインターネットに接続して使用する際、セキュリティを強化することが重要です。本記事では、ufw(Uncomplicated Firewall)とFail2Banを使用したセキュリティ設定、および不要なサービスの無効化方法を紹介します。

記事を書くのが面倒だったのでGPT先生に編集してもらっています。

前提条件

  • Raspberry Piがインターネットに接続されている
  • 基本的なLinuxコマンドの知識がある

1. ufwの設定

ufwのインストールと有効化

まず、ufwをインストールし、有効化します。

sudo apt-get update
sudo apt-get install ufw
sudo ufw enable

SSHアクセスの制限

次に、SSHアクセスを自宅ネットワークからのみ許可する設定を行います。現在のSSHアクセスルールを削除し、自宅ネットワークのサブネット(例:192.168.1.0/24)からのみアクセスを許可します。

sudo ufw delete allow 22/tcp
sudo ufw allow from 192.168.1.0/24 to any port 22

設定の確認

設定が正しく適用されているか確認します。

sudo ufw status

2. Fail2Banの設定

Fail2Banのインストール

次に、Fail2Banをインストールします。

sudo apt-get install fail2ban

Fail2Banの設定

設定ファイルを編集し、sshdジェイルを有効にします。

sudo nano /etc/fail2ban/jail.local

以下の内容を追加または修正します。

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/syslog
backend = systemd
maxretry = 3

Fail2Banの再起動とステータス確認
設定を保存し、Fail2Banサービスを再起動します。

sudo systemctl restart fail2ban
sudo fail2ban-client status

3. 不要なサービスの無効化

不要なサービスを無効化することで、システムのセキュリティとパフォーマンスを向上させます。

実行中のサービスの確認

まず、現在実行中のサービスをリストします。

sudo systemctl list-units --type=service --state=running

不要なサービスの無効化

以下は、一般的に不要とされるサービスの無効化例です。

avahi-daemon,cups,bluetooth の無効化

sudo systemctl disable avahi-daemon
sudo systemctl stop avahi-daemon
sudo systemctl disable cups
sudo systemctl stop cups
sudo systemctl disable bluetooth
sudo systemctl stop bluetooth

サービスの状態確認

無効化したサービスが停止していることを確認します。

sudo systemctl status avahi-daemon
sudo systemctl status cups
sudo systemctl status bluetooth

まとめ

以上の手順で、Raspberry Piのセキュリティを強化することができます。ufwによるファイアウォール設定、Fail2Banによる不正アクセスの防止、そして不要なサービスの無効化を行うことで、システムの安全性を高めることができます。これらの設定を定期的に見直し、最新の状態に保つことが重要です。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?