A new Ubuntu server accepts connections on every port where a program listens. The firewall ufw blocks everything you have not allowed, and Fail2ban blocks addresses that keep trying wrong logins. The commands below were tested on Ubuntu 26.04 LTS; run them as root or with sudo.


1. Switch On the Firewall

⚠ Allow SSH first

Run the ufw allow OpenSSH line before ufw enable, or your SSH connection is blocked. If you lock yourself out, use Console Access under Information → Console for a VPS, or IPMI / noVNC for a dedicated server, and run ufw disable.

ufw allow OpenSSH
ufw enable

On Debian, ufw is not installed after the installation: run apt install ufw first. The same commands then work on Debian 13.

ufw enable asks Proceed with operation (y|n)?: answer y. The firewall stays on after a restart. Check it:

ufw status verbose

Default: deny (incoming), allow (outgoing) means that everything coming in is blocked except the rules listed below it. Your server can still connect out, for example for updates.

2. Open a Port for a Program

When you install a program that others should reach, open its port. For example a web server on port 8080:

ufw allow 8080/tcp

Use /udp for UDP, or leave out the protocol to allow both. To remove a rule, list the rules with numbers and delete the one you no longer need:

ufw status numbered
ufw delete allow 8080/tcp
Docker

Ports that Docker publishes with -p are not controlled by ufw. See Docker on Your Linux Server.

3. Allow SSH Only From Your Addresses

If you always connect from the same addresses (home, office, a VPN), allow SSH only from them. Add your addresses first, then remove the general rule (replace the address with yours; a whole range is written like 198.51.100.0/24):

ufw allow from 203.0.113.10 to any port 22 proto tcp
ufw delete allow OpenSSH

Other addresses can no longer reach port 22 at all. Many home connections get a new address from time to time; you can check yours at myip.com.

4. Block Password Guessing With Fail2ban

apt install fail2ban

On Ubuntu, Fail2ban watches SSH right after the installation. By default, an address that fails 5 times within 10 minutes is blocked for 10 minutes. See what it has found:

fail2ban-client status sshd

Total failed counts the failed logins and Banned IP list shows the blocked addresses. A new server often has its first entries within minutes.

To block for longer and never block your own address, create /etc/fail2ban/jail.local with this content (your address after ::1):

[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
bantime = 1h

Then apply it with systemctl restart fail2ban.

If you blocked yourself, connect from another address or through the console and unblock your address:

fail2ban-client set sshd unbanip 203.0.113.10
💡 Even better: SSH keys

With password login switched off, guessing passwords has no chance at all: Log In to Your Linux Server With an SSH Key.


Related Guides

View all Ubuntu and Debian guides

Was this answer helpful? 0 Users Found This Useful (0 Votes)