← Networking and Remote Servers from Zero
Firewalls with ufw: status, allow, enable, deny and delete

Lesson 3 of 6

Firewalls with ufw: status, allow, enable, deny and delete

Decide which ports are open to the world. Learn five ufw commands, in the right order, with a security-guard analogy and a warning about locking yourself out.

The idea in plain words

A firewall is a security guard at the door. The guard has a guest list: allowed visitors walk in and everyone else is turned away. The safest rule is to block everything by default and allow only what you need.

ufw is the friendly tool that manages the guest list on Ubuntu. Its name stands for uncomplicated firewall. Mac and Windows have their own built-in firewalls with the same idea.

Commands in this lesson

sudo ufw status
Show whether the firewall is active and list its rules.
sudo ufw allow 22/tcp
Allow incoming connections on a port and protocol. Always allow SSH first.
sudo ufw enable
Turn the firewall on. Incoming traffic is blocked unless a rule allows it.
sudo ufw deny 5432/tcp
Block a port explicitly. Rules are checked in order and the first match wins.
sudo ufw delete allow 443/tcp
Remove a rule by repeating it after the word delete. ufw status numbered lists rules by number.

Written notes from the video

Why firewalls matter

Every open port is a door that anyone on the internet can try. This is Networking and Remote Servers from zero, episode three: firewalls. Five ufw commands to decide who gets in.

The security guard analogy

A firewall is a security guard at the door. The guard has a guest list. Allowed visitors walk in, and everyone else is turned away. The safest rule is to block everything by default, then allow only what you need. On Ubuntu, a friendly tool called ufw manages the rules. The name stands for uncomplicated firewall. Mac and Windows have their own built-in firewalls, with the same idea.

Command 1: ufw status

Command one: ufw status. It shows whether the firewall is on, and lists the rules. Right now it says inactive. Every port is reachable.

Command 2: ufw allow

Command two: ufw allow. Give it a port and a protocol, such as 22/tcp. Always allow SSH first. If you block port 22, you lose your way in. Then allow the web, with port four four three for secure web traffic. Each rule is added for both IP version four, and version six.

Command 3: ufw enable

Command three: ufw enable. This turns the firewall on. ufw asks you to confirm, because it can cut your SSH session. Since you allowed SSH first, your connection stays alive. By default, ufw blocks all incoming connections, and allows outgoing ones.

Command 4: ufw deny

Command four: ufw deny. It blocks a port explicitly. Say your database should never be public: ufw deny 5432. Rules are checked in order, so the first matching rule wins. Run ufw status again, to see the whole guest list.

Command 5: ufw delete

Command five: ufw delete. Delete a rule by repeating it after the word delete. Here we remove the rule for port 443. Use ufw status numbered, to list rules with numbers. Then you can delete by number.

Recap

Recap. ufw status shows the rules. ufw allow opens a port. ufw enable turns the firewall on. ufw deny blocks a port. ufw delete removes a rule. And always allow SSH before you enable.

Next episode

Next episode: SSH. You will log in to a remote server, safely, from your own terminal. Subscribe so you do not miss it, and find written notes for every lesson at changecv.com/skills.

Common questions

What happens if I enable ufw without allowing SSH?

You can cut off your own remote access, and the only fix may be your hosting provider’s web console. Allow port 22 (or your SSH port) before you run ufw enable.

Is ufw available on every Linux system?

It comes with Ubuntu, switched off by default, and can be installed on Debian. Fedora and Red Hat based systems use firewalld instead. The idea of allowing and denying ports is the same.

What is the default policy of ufw?

Deny incoming and allow outgoing. Run ufw status verbose to see the defaults, then add allow rules for the services you need.

Does a firewall replace strong passwords or keys?

No. A firewall controls which ports can be reached, but whatever is behind an open port still needs its own protection. SSH keys are covered in lesson 5.

Does ufw protect ports published by Docker?

Not always. Docker adds its own network rules when it publishes a port, which can bypass the rules you set in ufw. Check the Docker documentation if you run containers.