How to Set Up the UFW Firewall on Ubuntu
UFW (Uncomplicated Firewall) is the default firewall of Ubuntu. You block all incoming traffic, allow only the ports you need and switch the firewall on with ufw enable. Always allow SSH first before you enable the firewall, otherwise you lock yourself out.
Step 1: install UFW
On Ubuntu UFW is usually already present. On Debian you install it like this:
apt install ufw -y Step 2: set the default rules
Block all incoming traffic by default and allow outgoing traffic:
ufw default deny incoming
ufw default allow outgoing Step 3: allow SSH
ufw allow OpenSSH If you changed the SSH port, allow that port instead, for example ufw allow 2222/tcp.
Step 4: open the ports you need
ufw allow 80/tcp
ufw allow 443/tcp Want to open a port only for your own IP address, for example for a database:
ufw allow from JOUW-IP to any port 3306 Step 5: enable the firewall
ufw enable
ufw status verbose Managing rules
ufw status numberedshows all rules with a number.ufw delete 3deletes rule number 3.ufw disabletemporarily disables the firewall.Locked out?
If you can no longer log in, use the noVNC console in the client area and run ufw allow OpenSSH .
Related articles
- How to Install Fail2ban Against Brute-Force Attacks
- How to Install the CSF Firewall
- How to Change the SSH Port on Linux
Rent a Linux VPS with root access
Ubuntu, Debian or AlmaLinux with full root access via SSH, enterprise Ceph storage with high availability and hosting in Amsterdam.