How To Ubuntu Linux Firewall Open Port Command

How do I open port 80 and 443 on a Ubuntu Linux LTS version 14.04 server?

Ubuntu Linux server comes with firewall configuration tool called ufw (Uncomplicated Firewall). It is the default tool. ufw is very easy to use and configure host based firewall settings.

ADVERTISEMENTS

Ubuntu Linux firewall open port command

The program is for managing a Linux firewall is ufw. It aims to provide an easy to use interface for the user/sysadmins and developers. For example:

Tutorial details
DifficultyEasy (rss)
Root privilegesYes
Requirementsufw
Time5m
  1. You can open/close ports with ufw allow command.
  2. Block an IPv4/IPv6 address.
  3. Delete existing firewall rules.
  4. Turn on or off firewall logs.
  5. And more.

Let us see some examples of ufw firewall to open port on Ubuntu server.

How do I see the current status of my firewall?

Type the following command:
sudo ufw status verbose
Sample outputs:

Fig.01: Check the status of UFW on a Ubuntu Linux

Fig.01: Check the status of UFW on a Ubuntu Linux

Here is another outputs showing various open ports and their status via the ufw command:

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       139.xxx.yy.zzz             # Open TCP SSH PORT for SG_OFFICE
22/tcp                     ALLOW       35.yyy.zzz.ttt             # Open TCP SSH PORT for BACKUP_SERVICE
22/tcp                     ALLOW       172.aaa.bb.ccc             # Open TCP SSH PORT for MUM_OFFICE_VPN
192.168.206.142 642/tcp    ALLOW       192.168.113.184            # Open tinc TCP port pmdb1:642 for pmdb2 host
192.168.206.142 642/udp    ALLOW       192.168.113.187            # Open tinc UDP port pmdb1:642 for pmdb2 host
192.168.206.142 642/tcp    ALLOW       192.168.171.219            # Open tinc TCP port pmdb1:642 for pmdb3 host
192.168.206.142 642/udp    ALLOW       192.168.171.218            # Open tinc UDP port pmdb1:642 for pmdb3 host
10.100.28.1                ALLOW       10.100.28.0/24             # Allow full communication 
10.101.28.2                ALLOW       10.101.28.0/24             # Allow full communication 
10.102.28.3                ALLOW       10.102.28.0/24             # Allow full communication 

How do I open tcp port # 22?

To allow incoming tcp packets on port 22, enter:
sudo ufw allow 22/tcp
Verify it:
sudo ufw status verbose

How do I open tcp port # 80 and 443?

The service specific syntax is as follows to open http and https service ports:
sudo ufw allow http
sudo ufw allow https

OR
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

How do I open tcp and udp port # 53?

To allow incoming tcp and udp packet on port 53, enter:
sudo ufw allow 53
Verify it:
sudo ufw status verbose

Advanced examples for opening TCP and UDP ports

To allow IP address 192.168.1.10 access to port 22 for all protocols
sudo ufw allow from 192.168.1.10 to any port 22
Open port 74.86.26.69:443 (SSL 443 nginx/apache/lighttpd server) for all, enter:
sudo ufw allow from any to 74.86.26.69 port 443 proto tcp
To allows subnet 192.168.1.0/24 to Sabma services, enter:
ufw allow from 192.168.1.0/24 to any app Samba
You can find service info as follows:
sudo ufw app list
Sample outputs:

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  Squid

To get information on Squid profile/app, run:
ufw app info Squid
Sample outputs:

Profile: Squid
Title: Squid proxy cache
Description: Internet object cache (WWW proxy cache)
 
Ports:
  2048,3128,3130,3401,4827/tcp

Denying/blocking port access

We can add deny rule as follows to block all access to port 25:
sudo ufw deny 25
sudo ufw deny 25/tcp comment 'Block access to smptd by default'

How to delete ufw rules

We can list firewall rules using the following syntax on Ubuntu Linux:
sudo ufw status
sudo ufw numbered

Once we found firewall rule number delete by that number:
sudo ufw delete {num}
sudo ufw delete 5

Another option is to type:
ufw delete deny 25/tcp comment 'Block access to smptd by default'

Conclusion

In this page, you learned how to open TCP and UDP ports using UFW which is a default firewall management tool on Ubuntu Linux. See ufw command man page for more info and all other my tutorials below:

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdig host ip nmap
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w

ADVERTISEMENTS
6 comments… add one
  • muhammed Aug 4, 2015 @ 11:50

    Dear Guru,
    i need step in installing
    Pydio on the Linux server.
    Thanks

  • Jouni "rautamiekka" Järvinen Aug 4, 2015 @ 14:52

    UFW isn’t good enough, so we’re not using it, instead learning “iptables”

    • Svenv Mar 3, 2016 @ 13:03

      UFW is just a frontend to iptables, fine for simple config, won’t prevent you from doing advanced iptables stuff.

  • Ravi Aug 5, 2015 @ 2:06

    This was an informative article. Thanks.

  • Carol Spears Aug 6, 2015 @ 2:51

    UFW was as complicated to learn as iptables.

  • Fábio Aug 10, 2015 @ 23:26

    Nice article. I sometimes configure UFW, but I think it´s better just to work directly with iptables. Thank you.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.