MirrorNest
← すべてのガイド

UFW firewall setup on Ubuntu

Lock down an Ubuntu server to only the ports it actually needs, using UFW (Uncomplicated Firewall) -- a friendly frontend for iptables that ships with Ubuntu.

対象: Ubuntu 22.04 LTS / 24.04 LTSSecurityFirewall公式ホームページ ↗公式ドキュメント ↗
このガイドは上記リンク先にあるUFW firewall setup on Ubuntu公式ドキュメントをもとに作成されています。ディストリビューションやプロジェクトのバージョンは時間とともに変わるため、本番サーバーでこれらのコマンドを実行する前に、必ず公式ドキュメントで正確なパッケージ名・バージョン名を確認してください。
  1. 1Set default policies

    Deny everything inbound by default, then explicitly allow only what you need in the next steps.

    sudo ufw default deny incoming
    sudo ufw default allow outgoing
  2. 2Allow SSH before enabling the firewall

    sudo ufw allow OpenSSH

    Skipping this step locks you out of a remote server the moment UFW is enabled -- always allow SSH (or your custom SSH port) FIRST.

  3. 3Allow any other services you're running

    Add a rule per port/service actually in use -- e.g. 80/443 for a web server, or a specific app port.

    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
  4. 4Enable the firewall

    sudo ufw enable
  5. 5Check the active rules

    sudo ufw status verbose