Server Firewall Basics: What It Does and How to Start
A server listens on many doors, called ports. Each service you run, such as a website, mail or remote login, uses one. A firewall is the guard at those doors. It decides which traffic may come in and which is turned away. A good firewall is one of the cheapest ways to make a server safer.
What a firewall does
Without a firewall, any program that listens on a port can be reached from the internet. A firewall lets you say: only these ports are open, and everything else is closed. If you forget about a service you installed, the firewall still protects it.
Common ports in plain words
- 22 is the usual port for SSH, the remote login on Linux.
- 80 is plain web traffic.
- 443 is secure web traffic, the https in your address.
- 3389 is Remote Desktop on Windows servers.
- 25, 465, 587, 993 and 995 are used by email services.
- 3306 is the MySQL database. Most sites do not need it open to the world.
The rule of thumb
Open only what you need. If your server only hosts a website, you need ports for web traffic and for your own remote login. Everything else can stay closed.
Start with a default deny
Set the firewall to block incoming traffic by default, then allow the few services you need. On Ubuntu and Debian a simple tool called ufw makes this easy. On other systems you may find firewalld or iptables. Whichever tool you use, the order is the same:
- Allow your remote login port first.
- Allow web ports if the server runs a website.
- Turn the firewall on.
- Test that you can still log in from a new window.
Allowing the login port before you turn the firewall on is the step that keeps you from locking yourself out.
Limit who can log in
If you work from a fixed address, you can allow the remote login port only from it. This blocks everyone else from even trying a password. If your address changes often, skip this and use SSH keys instead.
Windows servers
Windows has its own firewall. Keep it on. Be careful with Remote Desktop, because it is a common target. Read our Windows VPS Remote Desktop tips.
A firewall is not everything
A firewall does not fix weak passwords or old software. It also does not stop floods of traffic aimed at your server. For that, read what DDoS protection is. Combine the firewall with the steps in secure a new VPS.
Common mistakes
- Turning on the firewall before allowing the login port.
- Opening the database port to the whole internet.
- Opening many ports “just to test” and never closing them.
- Forgetting that a new service needs its own rule.
Check what is open
From outside, a port scan shows what the world sees. Only run scans on your own server. Compare the result with the list you wrote down and close anything extra.
FAQ
Do I need a firewall if I use cPanel hosting?
On a hosting account we manage the server side for you. On a VPS or dedicated server, the firewall is your job. See order a VPS or dedicated server.
Can the firewall slow my site?
A simple rule set has no noticeable effect.
What if I lock myself out?
Open a support ticket and describe what you changed.