VPS Hosting

VPS Hosting

Buy Now

How to open a port in ufw

If you’re running a server with Ubuntu or Debian, ufw (Uncomplicated Firewall) provides a simple yet powerful way to manage firewall rules. Opening a specific port in ufw allows external traffic to reach your server for a particular service such as SSH, HTTP, or a custom application.

The ufw command is generally considered a bit easier than applying the same changes via iptables.

In this guide learn how to open a port using UFW via the command line. Whether you’re setting up a web server, remote access, or other services, these steps will ensure your firewall is configured correctly.

Install & enable ufw

1. To start you’ll want to confirm whether ufw is already installed on your VPS, you can do this with the ufw status command. If it returns a status then ufw is installed and you can move onto opening a specific port.

sudo ufw status

2. Moving on, if ufw is not installed you may want to take this as an opportunity to update and upgrade whilst also installing ufw.

sudo apt update && sudo apt upgrade && sudo apt install ufw

Open a specific port

1. Before we can enable ufw and open a specific port, we’ll want to ensure that SSH connections are enabled.

sudo ufw allow ssh

2. Now assuming ufw is inactive, enable it with the command below. This will start the firewall using the default rules, allowing SSH access as it was enabled beforehand.

sudo ufw enable

3. Afterwards we can enable traffic through a specific port by using the allow command followed by the port number. The command below adds a rule allowing both TCP and UDP connections over port 2087, you can alter it further by adding a /tcp or /udp to the end if you want to specify protocol.

sudo ufw allow 2087
sudo ufw allow 2087/tcp

4. After adding a rule, you can check that it has been added to the active ruleset with the status command.

sudo ufw status

5. Finally, if you need to remove a rule at any point you can use delete followed by the rule. For instance if you wanted to remove the rule we have just added that would be:

sudo ufw delete allow 2087