UFW (Uncomplicated Firewall) is a user-friendly frontend for the iptables firewall system on Linux. It provides a simple command-line interface to configure UFW rules and helps secure Linux servers by controlling incoming and outgoing traffic based on predefined policies.
In this article, we’ll guide you through the process of installing, configuring, and managing UFW on a Linux server.
What is UFW?

UFW is a straightforward way to configure and manage a firewall on Linux systems. It acts as a frontend to the more complex iptables
tool, making it easier for system administrators to configure and enforce security policies. UFW is most commonly used on Ubuntu and Debian-based systems, but can also be used on other Linux distributions like CentOS and RHEL.
It helps secure a Linux server by blocking unwanted incoming traffic while allowing essential services (e.g., SSH, HTTP, etc.) to function normally. UFW is often preferred for its simplicity, especially when managing basic firewall rules.
Prerequisites
Before starting the configuration of UFW on your Linux server, ensure that you meet the following prerequisites:
- A Linux server running a distribution like Ubuntu, Debian, or CentOS.
- Root or sudo privileges on the server.
- Access to the terminal or SSH to configure the server remotely.
Installing UFW
Before configuring, you need to install UFW(Uncomplicated Firewall) on your Linux machine. Follow the steps below.
Update System Packages
Before installing any new software, it’s always a good practice to ensure your system is up-to-date. To update the system, use the following commands:
- For Ubuntu/Debian:
sudo apt update && sudo apt upgrade
This ensures that your package list is up to date and all your existing packages are upgraded to the latest versions.
- For CentOS/RHEL (using EPEL repository):
sudo yum install epel-release sudo yum update
CentOS and RHEL require the EPEL repository to install UFW. Use the above commands to install it and update your system.
Install UFW
Once the system is updated, proceed with the installation of UFW.
- For Ubuntu/Debian:
sudo apt install ufw
This command installs UFW on your system.
- For CentOS/RHEL (after enabling the EPEL repository):
sudo yum install ufw
Verifying Installation
After installation, you can verify if UFW is installed correctly by checking its status:
sudo ufw status
This will return a message indicating whether UFW is active and whether the default firewall rules are in place.
Configuring UFW
Let’s configure UFW on your Linux machine step by step:
Enabling UFW
To start using UFW, you’ll need to enable it:
sudo ufw enable
This will activate the firewall and apply the default rules. If it’s your first time using UFW, it may ask you to confirm that you want to proceed.
Setting Default Policies
The default policies are the basic rules that determine what traffic should be allowed or denied if no specific rule matches.
- Default Incoming Policy:
Block all incoming connections by default.
sudo ufw default deny incoming
This means that, by default, any incoming traffic is denied unless explicitly allowed.
- Default Outgoing Policy:
Allow all outgoing connections by default.
sudo ufw default allow outgoing
This ensures that the server can freely initiate connections to external services.
Allowing Specific Services
Once UFW is enabled, you’ll need to allow specific services to ensure that your server remains accessible. Here are some of the most common services:
- Allow SSH:
Allow SSH connections to manage the server remotely. This is essential if you’re connecting to the server via SSH:
sudo ufw allow ssh
Alternatively, you can specify the port explicitly:
sudo ufw allow 22
- Allow HTTP/HTTPS:
If you’re hosting a website, you should allow HTTP (port 80) and HTTPS (port 443) traffic:
sudo ufw allow http sudo ufw allow https
- Allow Custom Ports:
If you’re running a service on a custom port, such as FTP on port 21, you can allow it like this:
sudo ufw allow 21
Restricting Access Based on IP
In some cases, you may want to restrict access to a service based on the source IP. This can add an extra layer of security.
- Allow Specific IP:
Allow a specific IP address to access SSH or any other service:
sudo ufw allow from <IP_ADDRESS> to any port 22
Replace <IP_ADDRESS>
with the IP you want to allow.
- Deny Specific IP:
You can also deny access from a specific IP address to your server:
sudo ufw deny from <IP_ADDRESS>
Managing UFW Rules
Listing Active Rules
To view the rules that are currently active, run the following command:
sudo ufw status verbose
This command shows all the active rules, including any allowed or denied ports and services.
Deleting Rules
If you need to remove a rule, such as allowing SSH, you can use the delete
command:
sudo ufw delete allow ssh
This will remove the rule allowing SSH access.
Modifying Rules
You may want to modify a rule, such as changing the port for SSH. Here’s how you can do it:
- First, delete the old SSH rule:
sudo ufw delete allow ssh
- Then, add a new rule for the desired port:
sudo ufw allow 2222
3.4 Enabling and Disabling Rules Temporarily
If you want to temporarily disable UFW for troubleshooting or maintenance, you can run:
sudo ufw disable
To re-enable UFW:
sudo ufw enable
Advanced UFW Configurations
UFW with IPv6
By default, UFW supports IPv4. To enable IPv6 support, you need to edit the UFW configuration file:
- Open the UFW configuration file for editing:
sudo nano /etc/default/ufw
- Change the
IPV6
variable fromno
toyes
:
IPV6=yes
- Save the file and restart UFW:
sudo ufw disable sudo ufw enable
Limiting Connections
To prevent brute-force attacks, you can limit the number of connections allowed for a specific service. For example, to limit SSH connections:
sudo ufw limit ssh
This limits the rate of SSH connections and helps prevent excessive login attempts.
UFW Logging
To monitor blocked or allowed connections, you can enable UFW logging:
sudo ufw logging on
This logs UFW activity to the system log, which can be viewed using:
sudo tail -f /var/log/ufw.log
Allowing Access to Specific Subnets
If you need to allow access to a service from a specific subnet (for example, an internal network), you can specify the subnet like this:
sudo ufw allow from 192.168.1.0/24 to any port 80
This allows all IP addresses within the 192.168.1.0/24
subnet to access port 80 (HTTP).
Troubleshooting UFW
Checking UFW Status and Logs
If you’re encountering issues, check the status of UFW with:
sudo ufw status
Additionally, you can view the logs to identify any blocked or allowed traffic:
sudo tail -f /var/log/ufw.log
Common Errors
- SSH Locked Out: If you accidentally block SSH access, you can quickly fix the issue by allowing SSH again:
sudo ufw allow 22
- UFW Not Starting: If UFW isn’t starting, you can check the status of the service:
sudo systemctl status ufw
UFW and Other Firewall Management Tools
UFW vs iptables
While UFW is a frontend for iptables
, it abstracts away much of the complexity. UFW simplifies the management of firewall rules, while iptables
provides more granular control over firewall configurations. UFW is best suited for users who need simple rule management, while iptables
offers more flexibility for complex configurations.
UFW with Firewalld
Although firewalld
is another firewall management tool used by CentOS and RHEL, you should not use UFW and firewalld
simultaneously. If both are enabled, they may conflict. It’s recommended to choose one firewall management tool based on your needs.
Conclusion
Configuring UFW on your Linux server is an essential step in securing it against unauthorized access and attacks. By following this guide, you should now be able to install, configure, and manage UFW to control inbound and outbound traffic, protect your services, and monitor your server’s firewall activity. Regular audits of your firewall settings will ensure your server remains protected over time. For more information, visit official UFW documnetation.