Setting up firewall with UFW on Linux
2 minutes read •
Check UFW status
It may return status active or inactive depending on the default config. If it is already active then disable it first.
Btw, the default ufw config is located in /etc/default/ufw.
Disable UFW
It will disable the firewall for now. Now if the firewall was active and it had some preconfigured rules, we need to reset everything to start with a clean environment.
Reset the firewall rules (PROCEED WITH CAUTION!)
Configure UFW
Default incoming and outgoing
Allow SSH connection
We can use both the name “ssh” or the port number “22”
Allow HTTP and HTTPS connections
Some Tests or Pratices with defferent scenarios (Optional)
We can Allow/Deny FTP
Allow/Deny traffic only from a specific IP address
Allow SSH connection only from a specific IP
We can also specify an entire subnet like:
Enable UFW
After configuring UFW we need to enable it to take efffect.
Managing UFW rules
Print the status or rules in numbered format
It shows an output similar to the following:
[
[
[
[
[
[) )
[) )
[) )
[) )
Delete a rule
First we need to print the status in numbered format. And we need to do that everytime we delete a rule. Because when we change the rules the index number changes. For example, if we wanto delete rule number 5. To do that we can,
Done!