VPS security · Practical guide

Set up a VPS firewall without blocking your own access

List required ports, allow SSH first and test the guest and Control Panel firewall rules.

Archive placement: April 2023

Server racks and network connections in a data centre

A useful firewall allows the traffic your services need and blocks the rest. Start with a short port list, preserve an administration route, and test each change from outside the VPS instead of assuming an “enabled” status means the setup is correct.

1. List the services that actually need public access

Make a simple list before adding rules: SSH for administration, HTTP and HTTPS for a public website, and any other application ports you deliberately expose. A database used only by the website normally does not need a public port. Keep monitoring, VPNs and administrator connections in the list so they are not accidentally blocked.

On the VPS: identify TCP and UDP listeners
sudo ss -lntup

An address of 127.0.0.1 or ::1 is local to the server. 0.0.0.0 or [::] generally indicates a listener that can accept connections on network interfaces, subject to firewall rules. A listening port is not automatically safe to expose. Identify the process before allowing it.

2. Understand the two firewall layers

The Control Panel firewall filters traffic outside the guest operating system. UFW, nftables or another guest firewall filters traffic inside it. Both must allow a required connection. A guest allow rule cannot cancel an upstream block. Existing Docker, VPN or control-panel software can also manage networking; do not replace its rules with an unrelated tutorial.

For Docker-published ports, ordinary UFW incoming rules may not filter traffic the way you expect. Check Docker’s documented firewall behaviour and test externally before treating a database or admin interface as private. Use one understood guest firewall manager rather than mixing manual iptables commands with UFW.

3. Preserve SSH and console access first

Confirm the browser console works, then leave a current SSH session open. In the Control Panel, allow your actual incoming SSH TCP port before tightening rules. The UFW example uses port 22. Replace it if SSH uses a custom port; the OpenSSH UFW profile may still refer to 22.

On the VPS using UFW: allow the actual administration port
sudo ufw allow 22/tcp
sudo ufw status numbered

If your public administrator address is stable, a source-restricted rule is preferable. For example, allow from your real public IP to the SSH port rather than entering an example address verbatim. Keep a documented recovery route for when an office connection or VPN changes. Do not remove a broad working rule until the restricted replacement is tested.

4. Add the application rules and review the defaults

A normal public web server usually needs incoming TCP 80 and 443. Add these only if this VPS serves a website. Do not copy a mail server or database port list onto a server that does not provide those services.

On a public web VPS using UFW
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw status verbose

For a fresh general-purpose web VPS, denying unsolicited incoming connections and allowing outgoing connections is a common starting point. It is not appropriate for every routed/VPN host. If adopting it, set default deny incoming and default allow outgoing only after the required incoming rules are in place. Review what those defaults mean for your actual application.

5. Enable and verify one layer at a time

On the VPS: enable the reviewed UFW policy
sudo ufw enable
sudo ufw status verbose

Read the confirmation before accepting it. If UFW was already active, your rules generally took effect when added; there is no reason to disable it first. Make a new SSH connection, load the website from another network, and check any deliberately restricted admin interface. An existing SSH connection may remain open even when new logins are blocked.

Then make the corresponding Control Panel changes and repeat the tests. Keep IPv6 enabled or disabled according to your actual network design, and check that the guest and upstream rules cover the addresses your DNS publishes. A working IPv4 test does not test an AAAA record’s IPv6 route.

6. Troubleshoot by following the connection

  • Confirm the application is running and listening on the intended address and port. A loopback-only listener will not become public just because the firewall permits it.
  • If SSH times out, inspect both firewall layers, the destination address and any source-IP restriction. If the connection is refused, inspect the listener first.
  • If the website works locally but fails externally, check DNS A/AAAA records and upstream rules. If it fails locally too, check the web service logs and configuration.

Use the console to restore the specific last rule if access is lost. Disabling the guest firewall can be a brief console-only recovery measure, but re-enable the corrected rules promptly; do not leave the whole server exposed as the permanent fix.

7. Remove old rules carefully

Keep a short description of why each rule exists. UFW’s numbered view is useful when removing a specific old rule with ufw delete followed by its number, but the numbers change after a deletion, so refresh the list each time. Test your public services afterward. Provider-managed restrictions, including outbound SMTP restrictions where applied, are separate from customer firewall rules and cannot be removed with a guest allow rule.

Common questions

Should I allow my database port to everyone?

Usually no. Keep a website’s database on loopback or a private network. If remote access is required, restrict the source, use encryption and strong authentication, and verify the restriction externally.

Why can I still use my old SSH window after a bad rule?

A firewall can allow established connections while rejecting new ones. Always test a new connection before treating a rule change as successful.

Further reading

← Back to all guidesDiscuss your hosting setup ↗