Marcio Cunha

How to configure UFW on Linux to secure your server

Learn how to harden your Linux server using UFW. Follow this practical guide to close unnecessary ports and prevent unauthorized access effectively.

Marcio Cunha•2 min
Also available in:PortuguêsEspañol
Summary
  • UFW simplifies iptables management by offering an intuitive command-line interface for system administrators.
  • The default deny policy ensures that no traffic enters or leaves the server unless explicitly authorized.
  • Allowing SSH connections is the most critical rule to prevent locking the administrator out during configuration.
  • Monitoring system logs via journalctl allows for identifying malicious access attempts in real time.
  • Using application profiles makes it easy to open specific ports for services like web servers or databases.

What is UFW and why use it for your server?

UFW, or Uncomplicated Firewall, is a tool designed to ease the management of Linux's firewall. Practically, it acts as an abstraction layer that simplifies complex iptables rules, allowing you to control network traffic without needing deep knowledge of low-level protocols. For those managing servers, using a user-friendly tool like UFW significantly reduces the risk of human errors that would otherwise leave security holes wide open.

Setting the default security policy

Before opening any ports, the golden rule is to block everything. When starting a firewall configuration, we want the server to be a locked fortress by default, where only authorized services can talk to the outside world. By denying incoming connections by default, you ensure that any background service running doesn't stay exposed accidentally. For handling more complex system issues, I recommend reading on how to audit Systemd logs on Linux to spot errors fast to diagnose unexpected behaviors.

Step-by-step guide to hardening your server

Initial configuration requires extra care, especially not to lose remote access. If you are connected via SSH, the first rule must be to ensure the connection stays open. Follow the sequence below:

  1. Install UFW (if not already present):
    sudo apt install ufw
  2. Set default policies:
    sudo ufw default deny incoming && sudo ufw default allow outgoing
  3. Allow SSH access before enabling the firewall:
    sudo ufw allow ssh
  4. Enable the firewall:
    sudo ufw enable

Managing ports and services

With the firewall active, you will need to open specific ports according to your project requirements. For example, if you run a web server, you will need to allow traffic on ports 80 (HTTP) and 443 (HTTPS). UFW allows using service names instead of just port numbers, which avoids confusion. For cases where you need more complex traffic redirection, consult our guide on how to configure NAT and port redirection in iptables, which complements basic firewall usage.

Validating and monitoring access

Configuring the firewall is not a one-time task; it requires continuous monitoring. Use the sudo ufw status verbose command frequently to verify which rules are active. If you suspect unauthorized attempts trying to scan closed ports, you can analyze system messages. Using filtering tools like journalctl to filter critical Linux logs is an essential practice for any admin who wants to maintain server integrity in the long run.

Final thoughts

Hardening your Linux server doesn't require paid or complex tools; it just takes a sound deny-all policy and correct UFW usage. By keeping only the essentials open and constantly monitoring logs, you eliminate the vast majority of automated attacks sweeping the internet daily.

Always remember to test your rules in a staging environment before applying them to production. Security is a continuous process, and your attention to detail when configuring ports is what separates a vulnerable server from a stable and protected environment.