Guide
Firewall on a VPS with ufw
Last updated: 15 November 2026
A firewall blocks every port you did not open on purpose. The one rule that matters: allow your SSH port before you switch the firewall on. If you are logged in as root, leave out sudo.
Fast VPS from $7.99 a month
- Sign up with an email address, a password and any nickname: no ID, no phone number.
- Pay with Monero, Bitcoin, Litecoin, USDT, USDC, SOL or TRX, or by card through MoonPay. Monero is the most private way to pay; USDT or USDC on Solana confirm in seconds, and we add no transfer fee on them.
- Orders are processed automatically: most are ready within minutes, occasionally it takes up to a few hours.
- New VPS orders have a 24-hour guarantee (see the terms for what is covered).
See Fast VPS plansHow ordering works
Debian and Ubuntu: ufw
- Install it if needed:
sudo apt install ufw.
- Set the defaults:
sudo ufw default deny incoming
sudo ufw default allow outgoing
- Allow SSH first (use your own port if it is not 22):
sudo ufw allow 22/tcp
- Allow what your services need, for example a web server:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
- Switch it on and check:
sudo ufw enable
sudo ufw status verbose
ufw warns that the command may disrupt SSH connections; answer y once your SSH port is allowed.
To remove a rule, use sudo ufw status numbered and sudo ufw delete NUMBER.
AlmaLinux and Rocky: firewalld
sudo systemctl enable --now firewalld
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
If SSH runs on another port, also run sudo firewall-cmd --permanent --add-port=PORT/tcp before the reload, and keep your current session open until a new login works.
Good to know
- Docker publishes container ports through its own rules and can bypass ufw. Publish a port only on the local address (
127.0.0.1:8080:80) if it must not be public. See Docker on a VPS.
sudo ufw limit 22/tcp replaces the allow rule and blocks an address that opens 6 or more connections in 30 seconds.
- If you lock yourself out, use the emergency console (VNC) on your service page, if it shows one, and run
sudo ufw disable or fix the rule.
Questions and answers
What is the most important firewall rule when I enable ufw?
Allow your SSH port before you run ufw enable, otherwise you lock yourself out.
Which ports should a web server leave open?
Your SSH port, 80 and 443, and nothing else unless a service needs it.
Does ufw protect ports published by Docker?
Not reliably: Docker publishes ports through its own rules. Bind private services to 127.0.0.1.
What do I do if I lock myself out?
Use the emergency VNC console on your service page, if it shows one, log in and disable or fix the rule.
Ready to try it? Fast VPS from $7.99 a month
- Pick a plan: Fast 1-2, $7.99 a month or all Fast VPS plans.
- Sign up with an email address, a password and any nickname: no ID, no phone number.
- Pay with Monero, Bitcoin, Litecoin, USDT, USDC, SOL or TRX, or by card through MoonPay. Monero is the most private way to pay; USDT or USDC on Solana confirm in seconds, and we add no transfer fee on them.
- Orders are processed automatically: most are ready within minutes, occasionally it takes up to a few hours.
- New VPS orders have a 24-hour guarantee (see the terms for what is covered).
- Before you pay you can ping our test IPs and read the live status page.
- Support is available 24/7 via email and our client ticket portal.
See Fast VPS plansHow ordering works