Ubuntu – Manage UFW firewall

Post taken from:
https://www.cyberciti.biz/faq/how-to-open-firewall-port-on-ubuntu-linux-12-04-14-04-lts/

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'