Using Ubuntu UFW

UFW is the default firewall configuration tool for Ubuntu.  UFW provides a user friendly way to create an IPv4 or IPv6 host-based firewall. By default UFW is disabled.

Allow Port/Protocol 

sudo ufw allow <port>/<optional: protocol>

Example: To allow incoming tcp and udp packet on port 53

sudo ufw allow 53

Example: To allow incoming tcp packets on port 53

sudo ufw allow 53/tcp

Deny Port/Protocol 

sudo ufw deny <port>/<optional: protocol>

Example: To allow incoming tcp and udp packet on port 53

sudo ufw deny 53

Example: To allow incoming tcp packets on port 53

sudo ufw deny 53/tcp

Show existing rules

To list the rules currently in place type the following

sudo ufw status verbose

or if you prefer a numbered list

sudo ufw status numbered

Allow by specific port and IP Address

sudo ufw allow from <ip address> to <protocol> port <port number>

example: allow ip address 192.168.0.4 access to port 22 for all protocols

sudo ufw allow from 192.168.0.4 to any port 22

Deleting rules by line number

First you will need to list the rule by line number:

sudo ufw status numbered

Then you can delete the relevant rules

sudo ufw delete <rule number>

Example: to delete rule 5

sudo ufw delete 5

 

Reference

https://help.ubuntu.com/community/UFW