August 06, 2026 · by CLIQHOST
A Linux server exposed to the internet without a properly configured firewall is an open invitation for attackers. Whether you're running a WordPress site, a Node.js application, or a mail server, setting up a firewall should be one of the very first things you do after provisioning a NVMe VPS or an SSD VPS.
In this guide you will learn:
- What UFW and iptables are and when to use each
- How to install and enable UFW on Ubuntu/Debian
- Which rules are essential for a web server
- How to use iptables directly for advanced use cases
- Common mistakes and how to avoid them
A firewall is your server's first line of defence. It decides which network traffic is allowed to enter or leave the server, based on rules you define.
On Linux, the underlying engine is netfilter — built into the kernel. Two main tools sit on top of it:
If you have a managed dedicated server, the administration team can handle these configurations for you. If you manage your server yourself, keep reading.
On Ubuntu 20.04/22.04 and Debian 11/12, UFW comes pre-installed. Check the status:
sudo ufw status verbose
If it's not installed:
sudo apt update && sudo apt install ufw -y
Before enabling UFW, define the baseline behaviour: deny all incoming, allow all outgoing.
sudo ufw default deny incoming
sudo ufw default allow outgoing
This is the "zero trust" principle applied at the network level — nothing gets in unless explicitly allowed.
⚠️ Warning: If you enable UFW without allowing SSH first, you will lock yourself out of your own server!
sudo ufw allow 22/tcp
If you changed the SSH port (recommended — e.g., port 2222):
sudo ufw allow 2222/tcp
sudo ufw enable
You'll receive a warning that existing connections may be disrupted. Confirm with y.
If you host a website on a WordPress VPS hosting plan or run Nginx/Apache, open the HTTP and HTTPS ports:
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
Or use the application profile shortcut:
sudo ufw allow 'Nginx Full'
# or
sudo ufw allow 'Apache Full'
| Service | UFW Command |
|---|---|
| MySQL (local only) | sudo ufw deny 3306 |
| MySQL (specific IP) | sudo ufw allow from 192.168.1.100 to any port 3306 |
| Passive FTP | sudo ufw allow 21/tcp |
| Email SMTP | sudo ufw allow 25/tcp |
| Email IMAPS | sudo ufw allow 993/tcp |
If you want to allow SSH only from a specific IP address (e.g., your office):
sudo ufw allow from 89.34.56.78 to any port 22
sudo ufw delete allow 22/tcp
This approach is extremely effective against brute-force attacks.
sudo ufw status numbered
Sample output:
Status: active
To Action From
-- ------ ----
[ 1] 22/tcp ALLOW IN Anywhere
[ 2] 80/tcp ALLOW IN Anywhere
[ 3] 443/tcp ALLOW IN Anywhere
sudo ufw delete 3
sudo ufw reset
Warning: this command disables UFW and deletes all rules!
UFW is sufficient for most servers, but iptables gives you granular control. This is especially useful when managing an unmanaged dedicated server with complex networking requirements.
iptables works with tables, chains, and rules:
- Main tables: filter, nat, mangle
- Chains: INPUT, OUTPUT, FORWARD
View current rules:
sudo iptables -L -v -n
Block a suspicious IP:
sudo iptables -A INPUT -s 45.67.89.123 -j DROP
Allow port 443:
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
Limit ICMP flood (ping protection):
sudo iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
iptables rules are not persistent across reboots by default! To save them:
sudo apt install iptables-persistent -y
sudo netfilter-persistent save
| Criterion | UFW | iptables |
|---|---|---|
| Ease of use | ✅ Very simple | ⚠️ Complex syntax |
| Granular control | ⚠️ Limited | ✅ Maximum |
| Best for | Simple VPS, websites | Complex servers, routers |
| Automatic persistence | ✅ Yes | ❌ Requires extra setup |
For a single-vCore VPS running a blog or an online store, UFW is more than enough.
ufw allow from any broadly — negates the entire purpose of a firewall/etc/default/ufw contains IPV6=yesAfter configuration, test from an external machine using nmap:
nmap -sV -p 22,80,443,3306 YOUR_SERVER_IP
Unwanted ports should appear as filtered or closed — never open.
You can also use the online tool Shields Up by GRC for a quick browser-based check.
Configuring a firewall correctly is a responsibility you can't afford to delay. UFW offers a fast and reliable solution for the most common scenarios, while iptables provides maximum flexibility for advanced configurations.
If you'd rather focus on your project and leave server administration to experienced professionals, explore server management services from CLIQHOST — security hardening, monitoring, and performance optimization all included.
Have questions about configuring your server? Contact our team — we're here to help.
Real reviews from customers who trust CLIQHOST for performance, reliability and expert technical support.
"We moved our online shop from a foreign host and the difference is night and day — pages load instantly and support replies in minutes, in Romanian."
"Migrated 12 client sites to CLIQHOST. Free migration, zero downtime, and the cPanel setup is exactly what my team needed. Highly recommend."
"Our NVMe VPS handles traffic spikes without a sweat. Full root, local datacenter, and billing in MDL — everything we wanted from a provider."