UFW a firewall configuration tool available on Ubuntu. UFW provides a user friendly way to create an IPv4 and IPv6 ip/port based firewall rules.
By default UFW is disabled, it can be enabled by running the following. Please ensure you have firewall rules in place to allow SSH before you enable it!
sudo ufw enable
Allow Port/Protocol
sudo ufw allow <port>/<optional: protocol>
Example: To allow incoming tcp and udp packets 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 deny incoming tcp and udp packets on port 53
sudo ufw deny 53
Example: To deny incoming tcp packets on port 53
sudo ufw deny 53/tcp
Show existing rules
To list the rules currently in place, run 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