Ubuntu 24.04 Firewall: A Quick Guide

Understanding and managing the firewall on your Ubuntu 24.04 system is crucial for ensuring the security and efficiency of your network. A firewall acts as a gatekeeper, controlling inbound and outbound traffic based on predetermined security rules. Ubuntu 24.04 comes equipped with ‘ufw’ (Uncomplicated Firewall), a user-friendly interface for managing iptables, the default firewall tool on Linux. Whether you’re a seasoned system administrator or a curious user, this guide will walk you through the essentials of using the ufw firewall, including installation, configuration, testing, and more.

In this tutorial you will learn:

  • How to install and enable ufw on Ubuntu 24.04
  • Methods to configure and test firewall rules
  • Steps to reset the firewall to default settings
  • Using a GUI alternative for firewall management
Ubuntu 24.04 Firewall: A Quick Guide
Ubuntu 24.04 Firewall: A Quick Guide

Understanding Ubuntu 24.04 Firewall

Before diving into the steps, it’s important to understand the role of ufw in Ubuntu 24.04. Ufw is designed to simplify firewall management, offering an easier way to configure iptables. It allows you to manage network traffic by controlling which services and ports on your system are exposed to the internet or other networks.

  1. Installing and Enabling UFW: First, ensure that ufw is installed on your Ubuntu system. It usually comes pre-installed, but if it’s not, you can easily install it using the command:
    $ sudo apt install ufw

    Once installed, enable ufw to start managing network traffic. Enabling ufw will activate the firewall and apply any pre-defined rules. To enable ufw, use:

    $ sudo ufw enable
  2. Configuring Firewall Rules: With ufw enabled, the next step is to configure firewall rules. These rules define how incoming and outgoing traffic should be handled. For example, to allow HTTP traffic on port 80, you would use:
    $ sudo ufw allow 80

    Configuring Firewall Rules
    Configuring Firewall Rules

    Similarly, to allow a specific service like SSH, the command would be:

    $ sudo ufw allow ssh

    Remember, you can also specify protocols, deny traffic, or create rules for specific IP addresses for more granular control.

  3. Testing Firewall Settings: After configuring rules, it’s important to test them to ensure they’re working as expected. You can check the status of ufw and view all active rules with:
    $ sudo ufw status verbose
    Testing Firewall Settings
    Testing Firewall Settings

    This command provides detailed information about the rules and their statuses, helping you verify your configurations.

  4. Resetting Firewall to Default Settings: If you need to revert your firewall settings to their defaults, ufw makes this easy. Resetting is useful when you want to start fresh or clear all custom configurations. To reset ufw, use:
    $ sudo ufw reset

    This command will disable the firewall and delete all active rules, allowing you to start from a clean slate.

  5. Removing a Specific UFW Rule
    To remove a specific rule from UFW, you first need to identify the rule by its number. This can be done by listing all the current rules with their numbers. Once you have identified the rule number, you can easily delete it. This is particularly useful when updating or cleaning your firewall configuration.

    $ sudo ufw status numbered
    $ sudo ufw delete [NUMBER]
    Removing a Specific UFW Rule
    Removing a Specific UFW Rule

    First, use ‘sudo ufw status numbered’ to display all rules with their respective numbers. Find the number of the rule you wish to remove. Then, use sudo ufw delete [NUMBER] replacing [NUMBER] with the actual number of the rule. This command will remove the specified rule from your firewall settings.

  6. Graphical User Interface Alternative: If you prefer a graphical interface for managing the firewall, ‘gufw’ is an excellent choice. Gufw is a user-friendly GUI for ufw, making it easier for those who are not comfortable with command-line interfaces. Install it with:
    $ sudo apt install gufw
    GUFW - Graphical User Interface Alternative
    GUFW – Graphical User Interface Alternative

    Once installed, you can access gufw from your applications menu and manage your firewall settings through its intuitive interface.

Top 20 UFW Examples: From Beginner to Pro

Learning to use UFW (Uncomplicated Firewall) on Ubuntu effectively is essential for both security and network management. These top 20 examples cover a range of scenarios from basic to advanced, helping you understand and implement various firewall rules and configurations. Whether you’re new to UFW or looking to expand your knowledge, these examples will guide you through different aspects of firewall management.

  1. Enabling UFW: Start by enabling the firewall to ensure it’s actively managing network traffic.
    $ sudo ufw enable

    This command activates UFW and applies any existing rules.
  2. Disabling UFW: To turn off the firewall and stop all its rules, use:
    $ sudo ufw disable

    This is useful when troubleshooting network issues or configuring new services.

  3. Allowing SSH Connections: Allow incoming SSH connections to manage your server remotely.
    $ sudo ufw allow ssh

    This rule permits SSH traffic, typically on port 22.

  4. Denying Incoming Traffic by Default: Set the default policy to deny all incoming connections for security.
    $ sudo ufw default deny incoming

    This command blocks all incoming traffic unless a specific rule allows it.

  5. Allowing Outgoing Traffic by Default: For general internet usage, allow all outgoing connections.
    $ sudo ufw default allow outgoing

    This ensures that the system can initiate web requests and other outgoing communications.

  6. Allowing Specific Port (HTTP): Open a specific port, like port 80 for HTTP.
    $ sudo ufw allow 80

    This allows web server traffic, essential for hosting a website.

  7. Denying Specific Port: To block traffic on a particular port, for example, port 8080:
    $ sudo ufw deny 8080

    This can be used to close ports that are no longer needed.

  8. Allowing a Specific IP Address: Grant access to a specific IP address, such as
    $ sudo ufw allow from

    This rule allows all traffic from this IP address.

  9. Denying a Specific IP Address: To block a specific IP address:
    $ sudo ufw deny from

    Useful for blocking suspicious or unwanted traffic from a particular source.

  10. Allowing a Range of IP Addresses: Allow traffic from a range of IP addresses using CIDR notation.
    $ sudo ufw allow from

    This permits connections from any IP address in the 192.168.1.x range.

  11. Limiting Connections to Prevent Brute-Force Attacks: Limit connections to a service like SSH to prevent brute-force attacks.
    $ sudo ufw limit ssh

    This rule limits the rate of incoming connections to the SSH port.

  12. Allowing a Specific Port for a Specific IP Address: Restrict a service port to a single IP address for enhanced security.
    $ sudo ufw allow from to any port 22

    Only allows SSH connections from the specified IP address.

  13. Denying Outgoing Traffic to a Specific IP: Block all outgoing traffic to a particular IP address.
    $ sudo ufw deny out to

    Useful for preventing data leaks or communications with known malicious hosts.

  14. Allowing Traffic on a Specific Network Interface: Control traffic based on the network interface, such as eth0.
    $ sudo ufw allow in on eth0 to any port 80

    Allows HTTP traffic only on the eth0 interface.

  15. Rejecting Traffic on a Specific Port: Reject traffic on a port rather than silently dropping it, which sends a response back to the sender.
    $ sudo ufw reject 25

    Useful for making it clear that a service is unavailable or blocked, as in the case of SMTP port 25.

  16. Allowing Protocols on Specific Ports: Allow specific protocols like TCP or UDP on particular ports.
    $ sudo ufw allow 53/tcp

    This example allows TCP traffic on port 53, typically used for DNS.

  17. Logging Firewall Activity: Enable logging to monitor and troubleshoot firewall activity.
    $ sudo ufw logging on

    Logs can be found in /var/log/ufw.log for reviewing traffic and rule enforcement.

  18. Resetting Firewall to Defaults: Reset UFW to its default settings, clearing all active rules.
    $ sudo ufw reset

    Useful for starting over or clearing a complex set of rules.

  19. Blocking All Traffic Except: Set a default deny policy and then allow specific services.
    $ sudo ufw default deny
    $ sudo ufw allow http
    $ sudo ufw allow https

    This setup blocks all traffic except for HTTP and HTTPS, ideal for a web server.

  20. Advanced Port Ranges and Protocols: Allow port ranges and specify protocols, useful for certain applications or services that use multiple ports.
    $ sudo ufw allow 6000:6007/tcp

    Allows TCP traffic on ports 6000 to 6007, commonly used for X11 applications.


Mastering the firewall settings on Ubuntu 24.04 is a critical step towards securing your system and network. Whether you’re using ufw’s command-line interface or gufw’s graphical interface, the key is to understand the implications of the rules you set. Always test your configurations and remember that the firewall is a powerful tool that, when used correctly, can significantly enhance your system’s security.