Teknologi, AI, internet, dan keamanan digitalKunjungi Nazuaf.com ↗
NNazuaf.
Keamanan Siber

UFW on Ubuntu: How to Configure Basic Firewall Rules Safely

Practical UFW guide for Ubuntu: check firewall status, allow necessary services, avoid SSH lockouts, and test rules safely before applying changes.

Redaksi Nazuaf··5 menit
UFW on Ubuntu: How to Configure Basic Firewall Rules Safely

Firewalls help restrict which network connections can reach a device. On Ubuntu, UFW (Uncomplicated Firewall) provides a simpler interface for managing Linux-based firewall rules. UFW works well for personal computers and small servers, provided rules are created only for services that are actually needed.

Illustration of how a firewall restricts network connections in this UFW Ubuntu guide
Concept illustration of a firewall. Source: Christian Glaeser, Wikimedia Commons, CC BY-SA 3.0.

The most important thing is not to activate as many rules as possible, but to understand their impact before applying them. A mistake on a remote server, for example, can cut off SSH access. This guide explains a workflow for checking and basic configuration with steps that can be tested and reverted.

What is UFW and when should you use it?

UFW simplifies firewall rule management on systems using netfilter. Users can allow or deny connections based on port, protocol, IP address, or available application profiles. UFW is not an antivirus and does not replace security updates, strong authentication, or firewalls that may be provided by cloud providers.

UFW is useful when you want to limit which services can be accessed from the network. For example, a web server might only need HTTP and HTTPS access from the internet, while administrative access should be restricted as needed. On a laptop, appropriate rules depend on whether the device is running services that need to accept incoming connections.

1. Check status before changing rules

Open a terminal and run:

sudo ufw status verbose
sudo ufw app list

The first command displays status, default policies, and active rules. The second command shows available application profiles. If UFW is not yet active, first check whether the system is a remote server and what services are currently in use. Do not enable the firewall immediately without knowing which administrative ports are required.

2. Set default policies carefully

For many general servers, a common starting approach is to deny unsolicited incoming connections and allow outgoing connections. However, this policy does not automatically suit all machines—for example, devices functioning as routers or forwarding traffic require additional consideration.

sudo ufw default deny incoming
sudo ufw default allow outgoing

These commands set policies for incoming and outgoing connections. More specific rules are still needed for services that must accept connections. Before applying policies, note critical services and ensure the required rules are prepared.

3. Allow SSH before enabling the firewall

If managing a VPS via SSH, this step is critical. Ensure the correct SSH port is allowed before UFW is activated. For installations using the OpenSSH profile, check the profile first:

sudo ufw app info OpenSSH
sudo ufw allow OpenSSH

If the profile is unavailable or SSH uses a custom port, create a rule for the correct TCP port, for example:

sudo ufw allow 2222/tcp

The number 2222 is just an example; use the port actually configured on the server. If possible, keep the current SSH session open and prepare console access from the VPS provider as a recovery path. Do not copy example rules without adjusting them to the actual configuration.

4. Allow only necessary services

For web servers that must be publicly accessible, HTTP and HTTPS rules can be added as follows:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

If an application is only used from an internal network, consider restricting connection sources to trusted addresses or subnets rather than opening the port to all addresses. Avoid opening database ports, admin panels, or debugging services to the internet unless necessary.

Once the required rules are ready, enable UFW:

sudo ufw enable

Read the confirmation prompt before approving it. On remote servers, ensure SSH rules are correct first. If unsure, test through a console or test environment before changing production servers.

5. Verify and manage rules

After activation, check the results:

sudo ufw status numbered
sudo ufw status verbose

Compare the rule list with service requirements. Rule numbers help when deleting a specific rule, but check the current numbers before running deletion commands since the list can change. To remove unnecessary rules, use commands appropriate to your review—do not accidentally delete SSH rules.

Test services from relevant locations: SSH connections from a new session, HTTPS access from a browser, and connectivity for applications that should be available. UFW status alone does not prove services work; applications must also be running and listening on the correct addresses and ports.

Common mistakes

  • Enabling the firewall too quickly: SSH rules not yet created, cutting off remote access.
  • Opening all ports: overly broad rules expand the attack surface.
  • Assuming UFW alone is sufficient for security: vulnerable services still need updates and proper configuration.
  • Forgetting other network layers: cloud firewalls, routers, or network policies can block connections even when UFW rules are correct.
  • Not testing changes: rules that look correct may not match application requirements.

Conclusion

UFW makes Ubuntu firewall management easier, but security still depends on appropriate rules. Start by checking status, setting suitable policies, allowing only needed services, then testing connections after changes. For remote servers, protect SSH access first and prepare a recovery path. Official Ubuntu documentation and UFW manual pages can serve as further references before applying configurations to production systems.

References: Ubuntu Community documentation on UFW and UFW manual page on Ubuntu.

Terus jelajahi Nazuaf

Baca topik lain atau kunjungi situs utama untuk melihat publikasi Nazuaf.

Artikel kategori ini →

Komentar (0)

Belum ada komentar. Jadilah yang pertama berkomentar.

Komentar langsung tampil setelah dikirim. Mohon berkomentar dengan sopan.