Skip to content

How to Install UFW on Your Linux VPS

UFW (Uncomplicated Firewall) is a simple frontend for iptables that significantly simplifies the management of your server firewall.

Install UFW

  1. Update system
    First, update the package lists:

    bash
    sudo apt update
    sudo apt update
  2. Install UFW
    Install UFW with the following command:

    bash
    sudo apt install ufw -y
    sudo apt install ufw -y

Configure UFW

  1. Set default rules
    Block all incoming connections and allow all outgoing:

    bash
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
  2. Allow SSH port
    Allow the SSH port so you can still connect:

    bash
    sudo ufw allow ssh
    sudo ufw allow ssh

    Important

    Do not skip this step, otherwise you will lock yourself out of the server! If you are using a different SSH port, allow that port instead, e.g. sudo ufw allow 2222/tcp.

  3. Allow additional ports (optional)
    Allow additional ports as needed:

    bash
    # Web server
    sudo ufw allow http
    sudo ufw allow https
    
    # Minecraft (default port 25565)
    sudo ufw allow 25565
    
    # TeamSpeak 3
    sudo ufw allow 9987/udp    # Voice
    sudo ufw allow 10011/tcp   # Query
    sudo ufw allow 30033/tcp   # File Transfer
    # Web server
    sudo ufw allow http
    sudo ufw allow https
    
    # Minecraft (default port 25565)
    sudo ufw allow 25565
    
    # TeamSpeak 3
    sudo ufw allow 9987/udp    # Voice
    sudo ufw allow 10011/tcp   # Query
    sudo ufw allow 30033/tcp   # File Transfer
  4. Enable UFW
    Activate the firewall:

    bash
    sudo ufw enable
    sudo ufw enable

    Confirm with y when prompted.

  5. Check status
    Verify that UFW is running correctly and which rules are active:

    bash
    sudo ufw status
    sudo ufw status

Manage rules

Show all rules with numbers:

bash
sudo ufw status numbered
sudo ufw status numbered

Remove a rule (e.g. HTTP):

bash
sudo ufw delete allow http
sudo ufw delete allow http

Block a specific port:

bash
sudo ufw deny 8080
sudo ufw deny 8080

Disable UFW:

bash
sudo ufw disable
sudo ufw disable

Tip

For additional protection against brute-force attacks, also set up Fail2Ban.