Hello! Welcome back to our module on Advanced Networking and Core Services.
In our last lesson, we discussed how load balancers distribute incoming traffic across multiple servers to ensure high availability and performance. Now that we know how to manage the flow of legitimate traffic, it's time to focus on security by controlling what traffic can reach our servers in the first place. This is the role of a firewall.
Today, we will focus on the learning outcome: Implement basic firewall rules to allow or block traffic using ufw. You'll learn how to use the Uncomplicated Firewall (ufw), a standard tool on many Linux systems, to create a strong, basic defense for your servers. For a DevOps engineer, knowing how to configure a firewall is a non-negotiable, fundamental skill for securing any piece of infrastructure you deploy.
1. What is UFW (Uncomplicated Firewall)?
Most Linux systems use a powerful and complex firewall framework called netfilter, which is managed through the iptables command-line tool. While iptables is extremely flexible, its syntax can be difficult for beginners and even experienced users to manage correctly.
This is where ufw comes in. As its name implies, the Uncomplicated Firewall is a user-friendly interface designed to simplify the process of managing iptables. It provides a more intuitive command structure for common firewall tasks, making it an excellent and reliable choice for most server security needs.
To get a clearer picture of the relationship between ufw and iptables, let's start with a brief reading.
UFW - A Beginners Guide to Linux firewall
This document, 'UFW - A Beginners Guide to Linux firewall', introduces UFW and explains its purpose as a simplified front-end for the more complex IPtables firewall.
Please read the first page of the document, focusing on the section titled 'IPTABLES AND UFW, WHAT’S THE DIFFERENCE?'. This will give you the context for why UFW exists and what problem it solves.
On most modern Debian or Ubuntu systems, ufw is installed by default. You can check its status with the following command. Don't worry if it's inactive; we'll be enabling it shortly.
sudo ufw status
You will likely see:
Status: inactive
2. The Golden Rule: Default Policies and Enabling UFW
A core principle of server security is to deny everything by default and only allow what is explicitly needed. This "deny-by-default" approach minimizes the attack surface of your server. With ufw, we establish this by setting default policies for incoming and outgoing traffic.
A standard and secure configuration is to:
- Deny all incoming traffic by default.
- Allow all outgoing traffic by default (so your server can fetch updates, connect to APIs, etc.).
You can set these defaults with the following commands:
sudo ufw default deny incoming
sudo ufw default allow outgoing
Warning: The Most Important Step!
Before you enable the firewall, you must create a rule to allow SSH access. If you are connected to your server remotely via SSH and you enable a firewall that denies all incoming traffic without an exception for SSH, you will be locked out of your server.
To allow SSH traffic, you can use either the service name or the port number (22):
sudo ufw allow ssh
or
sudo ufw allow 22
Now that you've set your secure defaults and guaranteed your own access, you can safely enable the firewall:
sudo ufw enable
The system will warn you that this may disrupt existing connections. Type y and press Enter. Your firewall is now active.
For a great summary and set of examples for these initial steps, the following resource is an excellent reference.
Common Firewall Rules and Commands for Linux Security
This guide from DigitalOcean, titled 'Common Firewall Rules and Commands', provides a practical cheat-sheet for UFW. It's a fantastic resource to bookmark for future use.
Read the sections 'How to Enable UFW' (including its subsection on 'UFW Default Policies') and 'How to Allow SSH'. These sections reinforce the critical startup sequence we just discussed.
3. Building Your Ruleset: Allowing and Blocking Traffic
With the default policy in place, your server now blocks all incoming connections except for SSH. Let's learn how to open specific ports for the services you want to expose, like a web server.
Allowing Traffic by Port or Service
You can allow traffic by specifying a port number or a service name (if it's defined in /etc/services). For example, to allow traffic for a web server (HTTP on port 80 and HTTPS on port 443):
# Allow HTTP traffic
sudo ufw allow http
# Allow HTTPS traffic
sudo ufw allow https
Alternatively, you could use the port numbers:
sudo ufw allow 80
sudo ufw allow 443
Some applications also register "application profiles" with ufw. You can list them with sudo ufw app list. If you had Nginx installed, you could allow web traffic with:
sudo ufw allow 'Nginx Full'
Allowing Traffic from a Specific IP Address
A common task is to grant access to a service only from a trusted IP address, like your office network or a specific monitoring server. You can do this by adding a from clause to your rule.
For example, to allow SSH access only from the IP 198.51.100.5:
# First, you might delete the general 'allow ssh' rule if it exists
sudo ufw delete allow ssh
# Then, add the more specific rule
sudo ufw allow from 198.51.100.5 to any port 22 proto tcp
This command is very specific: it allows traffic from a source IP, to any local IP, on port 22, using the tcp protocol.
Blocking Traffic from a Specific IP Address
Conversely, if you identify a malicious IP address trying to attack your server, you can block it completely.
sudo ufw deny from 203.0.113.10
This rule will drop any and all packets coming from that IP address.
The "Common Firewall Rules and Commands" guide has excellent, clear examples for all these scenarios. I recommend reviewing these sections to solidify your understanding.
Common Firewall Rules and Commands for Linux Security
Let's revisit the DigitalOcean guide to see clear, practical examples for allowing and blocking traffic based on ports and IP addresses.
Please review the following sections: 'How to Block an IP Address', 'How to Allow an IP Address', 'How to Allow Incoming SSH from a Specific IP Address or Subnet', and 'How to Allow All Incoming HTTP and HTTPS'. Focus on the command syntax and the different ways you can construct a rule.
4. Managing and Viewing Your Firewall Rules
As you add rules, you need a way to see the current configuration and remove rules you no longer need.
- To check the status and see your rules:
sudo ufw status - For a more detailed view, including default policies:
sudo ufw status verbose - To see the rules with numbers (very useful for deletion):
This will output your rules with a number next to each one, likesudo ufw status numbered[ 1],[ 2], etc.
To delete a rule, you can either re-type the rule with delete instead of allow/deny, or you can use the number from the status numbered command. Using numbers is often easier and less error-prone.
For example, to delete rule number 3:
sudo ufw delete 3
Test your understanding!
You have just set up a new Ubuntu server that will host a public website using Nginx. You are managing it remotely from your work IP address, 208.67.222.222. What sequence of ufw commands would you run to establish a secure baseline firewall configuration for this server?
Show answer
Here is a recommended sequence of commands:
-
Set secure default policies:
sudo ufw default deny incoming sudo ufw default allow outgoing -
Allow SSH access specifically from your IP address:
sudo ufw allow from 208.67.222.222 to any port 22 -
Allow public access to the web server:
sudo ufw allow 'Nginx Full'(This command assumes Nginx is installed and has registered its profile. Alternatively, you could use
sudo ufw allow httpandsudo ufw allow https.) -
Enable the firewall:
sudo ufw enable -
Verify the configuration:
sudo ufw status
Conclusion
You have now learned the fundamentals of securing a Linux server's network perimeter using UFW. This is a critical first line of defense for any system you will manage in your DevOps career. By establishing a "deny by default" policy and explicitly allowing only necessary services, you dramatically reduce the machine's exposure to threats.
Key Takeaways:
- UFW is a user-friendly tool that simplifies firewall management on Linux.
- The best practice is to start with a deny-by-default policy for incoming traffic.
- Always allow SSH access before enabling the firewall on a remote server to avoid locking yourself out.
- Firewall rules can be created to allow or deny traffic based on port numbers, service names, and source IP addresses.
- You can manage your ruleset easily using
ufw status numberedto list rules andufw delete <number>to remove them.
In our next lesson, we will move from securing a host to controlling how it directs traffic to other networks. You will learn how to view and manage basic static routes on a Linux system, a key skill for configuring systems that operate in more complex network environments.