How to Write nftables Rules for a VPS: A Beginner’s Guide

Learning to write nftables rules vps owners can trust takes one afternoon and removes a lot of firewall guesswork. nftables is the modern Linux firewall engine: one tool, one syntax, and tables that cover IPv4 and IPv6 together. This nftables beginner guide builds a working ruleset from nothing, explains each part in plain terms, and shows the safety habits that keep a bad rule from cutting off your own access.

Start with the model, since every rule makes sense once the model clicks. A ruleset holds tables. Tables hold chains. Chains hold rules. A chain attaches to a hook, which is a point in packet processing: input for packets arriving at the server, output for packets leaving it, and forward for packets passing through. Rules in a chain test packets and decide: accept, drop, or reject. Drop discards silently, while reject sends a refusal back, which tells scanners the port is closed rather than filtered. Most servers drop inbound and accept outbound.

Write nftables Rules VPS: Your First Ruleset

Here is a minimal ruleset for a web VPS: allow loopback traffic, allow established connections back in, allow SSH, HTTP, and HTTPS, and drop the rest. The nft table inet filter name means one table covers both IPv4 and IPv6, which is a real improvement over older tools that needed two parallel rule sets.

table inet filter {
  chain input {
    type filter hook input priority filter; policy drop;
    iif "lo" accept
    ct state established,related accept
    tcp dport 22 accept
    tcp dport 80 accept
    tcp dport 443 accept
  }
  chain output {
    type filter hook output priority filter; policy accept;
  }
}

Read it line by line. The input chain drops by default, so anything not listed fails closed. Loopback traffic passes, since local services talk to themselves over it. Established connections pass, so replies to your own requests come back. SSH, HTTP, and HTTPS pass on their ports. Everything else drops. The output chain accepts everything, so updates and API calls leave freely. That shape, default drop inbound plus named holes, is the pattern nearly every web VPS wants.

Applying a Ruleset Without Locking Yourself Out

Never apply an untested ruleset and walk away. Keep your SSH session open, apply the ruleset, and immediately test a fresh SSH login plus a web request from a second terminal. If either fails, revert to the previous ruleset from the open session. Better still, apply new rulesets with a timed rollback during testing: schedule a revert a few minutes out, test, and cancel the revert only when the tests pass. If you lose access, the scheduled revert restores the old rules on its own.

Save the working ruleset in your server docs and load it at boot through your system service, so a reboot does not silently drop the firewall. A firewall that exists only until the next reboot is a gap waiting for a restart. Test the boot behavior once on purpose: reboot during a maintenance window and confirm the rules return, rather than discovering a missing firewall after an unplanned restart.

Adding Rules the Safe Way

Add one rule at a time and test after each addition. Common additions for a web VPS include a rate limit on SSH attempts, an allow rule for a monitoring address, and a rule for a control panel port limited to your address. Address limits beat open ports whenever the visitor set is small: a panel port open only to your office address is far safer than the same port open to the world. For address lists that change often, use a named set, which stores many addresses in one rule instead of one rule per address.

Sets also speed up matching on busy servers, since a set lookup beats walking a long rule chain. Define the set once, reference it in a rule, and update the set without touching the rule. That separation keeps the ruleset readable as it grows, which is the main advantage nftables holds over older tools for larger setups.

Logging, Testing, and Debugging

Add logging to the drop rule while you test, so denied packets show up in the system log with a prefix you can search. Watch the log while you test each service: a denied packet to a port you meant to open points straight at the missing rule. Once testing ends, reduce logging to keep logs small, since a public server collects denied scans all day and logging every one fills disks.

When a service fails behind the firewall, check in order: is the service listening, does the ruleset allow the port, and does the chain policy drop before the rule? Rule order matters, since the first matching rule wins. A broad drop placed above a specific accept hides the accept entirely, which is the most common beginner error after a typo. List the active ruleset and read it top to bottom before guessing at service configs.

UFW or Raw nftables: Which First

Beginners should start with UFW and move to raw nftables only when UFW cannot express a rule they need. UFW covers the common web server case with far less risk, and our guide to set up a firewall with UFW on Ubuntu Server 24.04 is the faster path to a safe server. Raw nftables earns its place when rules get complex: large address sets, per-service chains, or traffic shaping beyond allow and deny. If you run a single web server, UFW plus good habits beats a hand-written ruleset you rarely review.

Whatever tool you choose, keep the surrounding baseline in order. Our first 10 things to do after buying a VPS checklist puts the firewall next to user setup and updates, our guide to common VPS setup mistakes beginners make lists the firewall errors that recur most, and our guide to set up automatic security updates on Ubuntu and Debian covers patching, since a firewall cannot cover unpatched services behind it.

A Ruleset You Can Grow

Start from the minimal ruleset above, add one rule at a time, test each addition from a second session, and keep the working copy saved and loaded at boot. Review the rules monthly and after every app install, since temporary ports have a habit of becoming permanent. Done that way, nftables stops feeling like a puzzle and becomes the quiet layer that lets your server ignore the internet’s background noise.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.