Managing firewall rules in Ubuntu Server
Ubuntu comes with a built-in firewall management tool called UFW (uncomplicated firewall). Below are several commands that will help manage UFW. Ensure SSH access is allowed before enabling UFW, otherwise, access to the server may be blocked.
By default, UFW will block all incoming connections and allow all outgoing connections. If this has been changed, the defaults can be restored with these commands:
sudo ufw default deny incoming
sudo ufw default allow outgoingAt a minimum, you will want to add a rule allowing inbound SSH traffic:
sudo ufw allow sshInstead of “ssh”, you can provide the port for the service (in this case, port 22):
sudo ufw allow 22The above rule can be modified if you would only like to allow SSH traffic from a specific IP:
sudo ufw allow from x.x.x.x to any port 22We recommend allowing Mac Mini Vault’s NOC IP range so that we can more quickly assist in the event of a lockout:
sudo ufw allow from 192.159.66.96/27 to any port 22To deny traffic from a specific IP, use this command:
sudo ufw deny from x.x.x.xTo delete a rule, list the rules in a numbered list, and then delete the rule number:
sudo ufw status numbered
sudo ufw delete 5The above commands will list all UFW rules, and then delete rule #5 in the list.
To enable UFW:
sudo ufw enableTo disable UFW:
sudo ufw disableTo check UFW status:
sudo ufw statusor
sudo ufw status verbose