Jan Carlo Viray React · Angular · Node · SQL · Cloud · Entrepreneurship

Portfolio ·  LinkedIn ·  Twitter ·  Stack Overflow ·  Github ·  Contact Me

Add Firewall to Linux Server with UFW

So, you have your own server? Setting up a firewall on your server is very important once it is up and running. Thanks to ufw, doing this is fairly easy! Don’t even think about making your server public without a firewall! Check out my previous post on securing your server to add more protection.

Install UFW - The Uncomplicated Firewall

sudo apt-get update

sudo apt-get install ufw

Setup Defaults

These are the default settings out of the box, but let’s just make extra sure that they really are.

sudo ufw default deny incoming

sudo ufw default allow outgoing

Let’s Enable Some Ports by Service

If you don’t need some of these services, you don’t have to run them.

sudo ufw allow OpenSSH
sudo ufw allow http
sudo ufw allow https
sudo ufw allow ssh

Wondering what other services your server is running? Run less /etc/services to check.

Let’s Enable Some Ports Manually

You typically don’t need to define ports if you have specific services running like how we did in the previous section, but here are some examples.

# using mail? allow these ports
sudo ufw allow 25   #smtp
sudo ufw allow 143  #imap
sudo ufw allow 993  #imaps
sudo ufw allow 110  #incoming pop3
sudo ufw allow 995  #incoming pop3s

Enable UFW

sudo ufw enable

That’s it!

Want to read more on Firewalls?

If you have any questions or comments, please post them below. If you liked this post, you can share it with your followers or follow me on Twitter!