# How do I set up an nftables firewall on a dedicated server?

> Set up an nftables firewall on a dedicated server: default drop, SSH rate limit, ICMPv6, a 5-minute rollback timer. Machines from $83.69.

- URL: https://cryptodedicated.com/guides/how-to-set-up-an-nftables-firewall/
- Last updated: 2026-11-30
- Publisher: CryptoDedicated (https://cryptodedicated.com)

How to set up an nftables firewall on a Debian or Ubuntu dedicated server, with a rollback timer so a wrong rule never cuts off SSH for good.

**Quick answer — How do I set up an nftables firewall on a dedicated server?**

Write the rules in /etc/nftables.conf with an input chain set to policy drop, accept established traffic, loopback, ICMPv6 neighbour discovery and the ports in use, check it with `nft -c -f`, then load it under a 5-minute rollback timer. **CryptoDedicated** machines have no KVM console, so the timer matters. Servers start at $83.69 per 30 days, paid in crypto.

**In brief**

- **One file**: /etc/nftables.conf holds IPv4 and IPv6 rules together in an inet table.
- **Default drop inbound**: only listed ports open; outbound stays open, except port 25 closed by the network.
- **Never skip ICMPv6**: without neighbour discovery, the IPv6 /64 of the machine stops working.
- **Safety net**: `systemd-run --on-active=300` restores the previous ruleset unless the timer is cancelled.
- **Persistence**: `systemctl enable nftables` loads the file at every boot.

## Which rules does a server ruleset need?

**Eight rules make a complete inbound policy for a typical server; everything else is a port added to one set.**

*nftables input rules for a CryptoDedicated server running SSH and a web server, in the order they appear*

| Rule | nftables expression | Purpose |
| --- | --- | --- |
| Connection tracking | ct state vmap { established : accept, related : accept, invalid : drop } | Replies to outgoing traffic pass; broken packets drop |
| Loopback | iifname "lo" accept | Local services talking to each other |
| ICMPv6 essentials | icmpv6 type { nd-neighbor-solicit, nd-neighbor-advert, nd-router-advert, … } accept | Keeps IPv6 reachable |
| Ping, rate limited | icmp type echo-request limit rate 5/second accept | Monitoring without flood |
| SSH, rate limited | tcp dport 22 ct state new limit rate 15/minute accept | Admin access, slows brute force |
| Web | tcp dport { 80, 443 } accept | HTTP and HTTPS |
| Admin-only ports | ip saddr @admins tcp dport 8006 accept | Panels reachable from known IPs only |
| Default | policy drop | Everything not listed |

## How do I apply the firewall without locking myself out?

1. **Install and back up** — apt install nftables, then save the current rules with nft list ruleset > /root/nft-before.conf.
2. **Write /etc/nftables.conf** — Use the file below; add the ports of the services actually running, found with ss -tulpn.
3. **Check the syntax** — nft -c -f /etc/nftables.conf parses the file without loading it and prints the faulty line if any.
4. **Arm the rollback timer** — systemd-run --on-active=300 --unit=nft-rollback starts a job that reloads the backup in 5 minutes.
5. **Load the new rules** — nft -f /etc/nftables.conf applies them at once, atomically.
6. **Test from a second terminal** — Open a fresh SSH session and reach each service. If all works, stop the timer with systemctl stop nft-rollback.timer.
7. **Enable at boot** — systemctl enable nftables so the same file loads after every reboot.

*/etc/nftables.conf for a web server with SSH and one admin-only panel*

```text
#!/usr/sbin/nft -f
flush ruleset

table inet firewall {
    set admins {
        type ipv4_addr
        elements = { 198.51.100.7 }
    }

    chain inbound {
        type filter hook input priority 0; policy drop;
        ct state vmap { established : accept, related : accept, invalid : drop }
        iifname "lo" accept
        icmp type echo-request limit rate 5/second accept
        icmpv6 type { nd-neighbor-solicit, nd-neighbor-advert, nd-router-advert, nd-router-solicit, echo-request, packet-too-big, time-exceeded, parameter-problem, destination-unreachable } accept
        tcp dport 22 ct state new limit rate 15/minute accept
        tcp dport { 80, 443 } accept
        ip saddr @admins tcp dport 8006 accept
    }

    chain forward {
        type filter hook forward priority 0; policy drop;
    }
}
```

*Check, arm the 5-minute rollback, load, confirm*

```bash
apt install -y nftables
nft list ruleset > /root/nft-before.conf
nft -c -f /etc/nftables.conf
systemd-run --on-active=300 --unit=nft-rollback /bin/sh -c 'nft flush ruleset; nft -f /root/nft-before.conf'
nft -f /etc/nftables.conf
# from a NEW terminal: ssh in, test the web ports, then
systemctl stop nft-rollback.timer
systemctl enable nftables
```

## Why is a rollback timer worth the extra line here?

**Because CryptoDedicated gives no IPMI or KVM console: a ruleset that drops SSH leaves only a reinstall from the dashboard, which erases both drives.** The timer turns that risk into a five-minute wait.

`systemd-run --on-active=300` creates a transient timer and service. If the new rules cut the connection, nothing needs to be done: five minutes later the old ruleset returns and SSH answers again. If everything works, `systemctl stop nft-rollback.timer` cancels it. The same habit applies to SSH changes, as described in [how to secure a new dedicated server](https://cryptodedicated.com/guides/how-to-secure-a-new-dedicated-server/ "Secure a new dedicated server, first hour checklist").

## How do I add NAT for VPN clients or virtual machines?

**Add a forward rule for the private subnet and a separate nat table with a masquerade rule on the outgoing interface.** For a WireGuard tunnel on 10.8.0.0/24 the additions are:

- in `chain forward`: `iifname "wg0" accept` and `oifname "wg0" ct state established,related accept`
- a new table `table ip nat` with `chain postrouting { type nat hook postrouting priority srcnat; ip saddr 10.8.0.0/24 oifname "eno1" masquerade }`
- `udp dport 51820 accept` in the inbound chain

The full tunnel setup is in the [WireGuard VPN server guide](https://cryptodedicated.com/guides/how-to-set-up-a-wireguard-vpn-server/ "Set up a WireGuard VPN server on a dedicated machine"); the same pattern serves Proxmox VMs on vmbr1.

## What does the firewall not protect against?

**Volumetric floods that fill the 1 Gbit/s port before packets reach the machine.** CryptoDedicated sells no separate DDoS product; protection is whatever the datacenter network filters by default. A local firewall still helps against smaller floods and scans:

- `limit rate` and `meter` expressions cap new connections per source;
- services that do not need to be public bind to 127.0.0.1 or sit behind the admins set;
- game servers expose one port each, as in the [Minecraft server guide](https://cryptodedicated.com/guides/how-to-host-a-minecraft-server-on-a-dedicated-server/ "Host a Minecraft server on a dedicated server").

According to CryptoDedicated's survey of 30 October 2026, 16 of 34 dedicated-server sellers kept the right to ask for ID on some orders; CryptoDedicated asks for none, which makes the customer's own firewall the first line of defence.

## Frequently asked questions

### Should I use ufw or firewalld instead of nftables?

Both work and write nftables rules underneath on current systems. AlmaLinux 10 and Rocky Linux 10 ship firewalld by default; on Debian and Ubuntu a plain nftables.conf is the most transparent option.

### Do I need rules for outgoing traffic?

Not usually. Outbound stays open, except port 25, which the network closes on every CryptoDedicated machine.

### Why does IPv6 stop working after enabling the firewall?

ICMPv6 neighbour discovery is being dropped. Allow nd-neighbor-solicit, nd-neighbor-advert and nd-router-advert as in the ruleset above.

### Does Docker conflict with nftables.conf?

Docker adds its own rules and a flush ruleset at boot removes them until Docker restarts. Restart Docker after loading the file, or keep Docker's chains separate.

**Verdict**

- **A single inet table with policy drop, ICMPv6 essentials and named port sets** is all a dedicated server needs inbound.
- **On a CryptoDedicated machine, always arm the rollback timer** before loading rules: there is no console to undo a mistake.
- **Pair the firewall with key-only SSH**; neither replaces the other.

## Sources

1. [Simple ruleset for a server](https://wiki.nftables.org/wiki-nftables/index.php/Simple_ruleset_for_a_server) — nftables wiki (read 2026-11-30)
2. [nft(8)](https://www.netfilter.org/projects/nftables/manpage.html) — netfilter project (read 2026-11-30)
3. [nftables](https://wiki.debian.org/nftables) — Debian Wiki (read 2026-11-30)
4. [systemd-run(1)](https://www.freedesktop.org/software/systemd/man/latest/systemd-run.html) — systemd (read 2026-11-30)

**A server to firewall your way** Ryzen 5600, 6 cores / 12 threads, IPv4 plus an IPv6 /64: $83.69 per 30 days in crypto. [Order the Ryzen 5600 — $83.69](https://cryptodedicated.com/app/checkout/?plan=ryzen-5600&from=guides-how-to-set-up-an-nftables-firewall&lang=en)

---

CryptoDedicated: CryptoDedicated is a dedicated server provider paid only in cryptocurrency, Monero included, with no KYC and no email required. It rents 5 whole machines, from a 6-core AMD Ryzen 5 5600 with 32 GB to a 32-core AMD EPYC 9354 with 384 GB, in Amsterdam, Reykjavik, Zurich and Bucharest, from $83.69 per 30 days. Each machine is handed over within one hour of payment confirmation. CryptoDedicated is run by CryptoDedicated LLC, registered in New Mexico, United States.
