Table of Contents
UFW (Uncomplicated Firewall) is the default firewall management tool on Ubuntu. It provides a simple way to secure a server by allowing or blocking incoming network traffic.
Most administrators enable UFW immediately after deploying a Linux VPS server to reduce unauthorized access and protect public services such as SSH, web servers, and databases.
Step 1: Connect to the Ubuntu Server
Access the server using SSH:
ssh root@your_server_ip
Step 2: Update the Server Packages
Refresh the package index:
apt update && apt upgrade -y
Keeping the server updated helps improve security and compatibility before configuring the firewall.
Step 3: Install UFW on Ubuntu 24.04
Install the firewall package:
apt install ufw -y
Verify the installation:
ufw version
Step 4: Allow SSH Connections
Before enabling the firewall, allow SSH access to avoid disconnecting from the server:
ufw allow OpenSSH
You can also allow a custom SSH port manually:
ufw allow 2222/tcp
Step 5: Enable the Firewall
Activate UFW:
ufw enable
Type:
y
to confirm the operation.
Check the firewall status:
ufw status verbose
Step 6: Allow Web Traffic
If the server hosts websites, allow HTTP and HTTPS traffic:
ufw allow 80/tcp ufw allow 443/tcp
For servers running Nginx, Apache, or web applications, these ports are required for browser access.
If you are deploying web applications on Ubuntu, learning how to install Nginx on Ubuntu can help complete the server setup.
Step 7: View Firewall Rules
Display all active firewall rules:
ufw status numbered
Example output:
[ 1] OpenSSH ALLOW IN Anywhere [ 2] 80/tcp ALLOW IN Anywhere [ 3] 443/tcp ALLOW IN Anywhere
Step 8: Remove a Firewall Rule
Delete a rule by its number:
ufw delete 2
You can also remove rules directly by port:
ufw delete allow 80/tcp
Step 9: Disable or Restart UFW
Disable the firewall:
ufw disable
Reload firewall rules:
ufw reload
After configuration, UFW helps secure Ubuntu 24.04 servers by filtering unwanted traffic while allowing only necessary services and ports.
