Saturday, October 12, 2024

How to Setting UFW Firewall on Ubuntu 24.10

Uncomplicated Firewall (UFW) is a powerful yet user-friendly command-line tool that simplifies firewall management on Ubuntu and Ubuntu-based distributions. It allows you to quickly create and manage rules for controlling incoming and outgoing traffic on your system, ensuring your system is protected from unauthorized access. This article will guide you through setting up UFW on Ubuntu and using it to secure your system.

Installing UFW on Ubuntu

While UFW is typically preinstalled on Ubuntu, it might be absent in minimal or stripped versions. To ensure UFW is installed, run the following command in your terminal:

      sudo apt install ufw
    

This will download and install UFW on your system.

Enabling and Configuring UFW

After installation, verify that UFW is active by checking its status:

      sudo ufw status
    

If UFW is not active, enable it with the following command:

      sudo ufw enable
    

Supporting IPv6

If your network supports IPv6, ensure UFW is configured to handle both IPv4 and IPv6 traffic. Open the UFW configuration file:

      sudo nano /etc/default/ufw
    

Inside the file, locate the line IPV6=no and change it to IPV6=yes. Save the file and exit the editor.

Creating Firewall Rules

UFW allows you to create rules to allow or deny traffic based on ports, protocols, and applications.

Allowing Specific Ports

To allow traffic on specific ports, such as ports 80, 443, and 22 for Apache and SSH services, run the following commands:

sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 22
    

Using Application Profiles

UFW provides built-in application profiles that simplify the process of creating rules for common services. For example, to allow traffic for the HTTP/HTTPS protocol (ports 80 and 443) and the default SSH port (port 22), use:

      sudo ufw allow "Apache FULL"
sudo ufw allow "OpenSSH"
    
To see a list of available application profiles, use:
      sudo ufw app list
    
You can also get details about a specific profile:
sudo ufw app info "Apache Full"
sudo ufw app info "OpenSSH"
    
Viewing Firewall Rules

To view the current firewall rules, use the following command:

      sudo ufw status verbose
    
Deleting Firewall Rules

To remove a firewall rule, first identify its index number using:

      sudo ufw status numbered
    
Then, use the ufw delete command with the corresponding index number. For example, to remove the rules for port 80 on both IPv4 and IPv6:
      sudo ufw delete 1 4
    
Limiting SSH Port Access

To restrict access to the SSH port and prevent unauthorized attempts, use:

      sudo ufw limit ssh
    
Adding Comments to Firewall Rules

To make your firewall rules more understandable, add comments explaining the purpose of each rule. For instance, to allow access to a Portainer service running on port 8080:

      sudo ufw allow 8080 comment 'Portainer Service'
    
Managing Connections

UFW allows you to control connections from specific IP addresses.

  • Allow connections from a specific IP address:

      sudo ufw allow from <IP address>
    

  • Allow connections from a specific IP address to a particular port:

      sudo ufw allow from <IP address> to any port <port number>
    

  • Allow access to a range of ports:

# Allow TCP traffic
sudo ufw allow 2000:2004/tcp

# Allow UDP traffic
sudo ufw allow 2000:2004/udp
    

Denying Incoming Connections

You can also deny connections from specific IP addresses or to specific ports:

  • Restrict all connections from a specific IP address:

      sudo ufw deny from <IP address>
    
  • Restrict connections from a specific IP address to a particular port:

      sudo ufw deny from <IP address> to any port <number>
    
Resetting UFW to Default Settings

If you need to reset your firewall rules to their default settings, use:

      sudo ufw reset
    
This will prompt you to confirm the action.

Disabling UFW

To temporarily disable UFW, run:

      sudo ufw disable
    
Conclusion

UFW provides a straightforward way to secure your Ubuntu system by managing firewall rules. Its user-friendly interface and intuitive commands make it accessible to both beginners and experienced users. By following the steps outlined in this guide, you can confidently implement basic and advanced firewall configurations, ensuring your system is protected from unwanted traffic and potential security threats.

0 comments:

Post a Comment