Setup & operation

How do I set up a firewall on a VPS?

Short answer

Use nftables on modern Linux, or ufw as a friendlier front end. Set the default inbound policy to drop, allow established and related connections, then allow only the ports you actually serve. Always allow your SSH port before enabling the policy, or the console will be your only way back in.

Remember IPv6. A ruleset that only covers IPv4 leaves every service reachable over v6, and every instance here has a routed /64. ufw handles both by default; hand-written nftables rules need an ip6 table or an inet table.

Test before you commit. A common pattern is to schedule a job that flushes the rules in ten minutes, apply the new policy, verify you are still connected, then cancel the job.

  • ufw default deny incoming; ufw default allow outgoing
  • ufw allow 22/tcp (or your chosen port) before ufw enable
  • ufw allow 80,443/tcp for web services
  • Confirm IPv6 is covered — check ufw status verbose or your nft ruleset

See also: IPv6