Linux Security & Ops 6 min read · Last verified 2026-11-19

Modern Linux Server Hardening: Migrating Stateful iptables Rules to nftables and Sandboxing systemd Services

Shipping a production API or WireGuard bastion onto a fresh Debian 12, Ubuntu 24.04, or Rocky Linux 9 VPS with default firewall and service unit settings leaves two massive attack surfaces exposed: unrestricted dual-stack ports (often forgotten on IPv6!) and long-running daemons executing with unnecessary filesystem write access and root capabilities. Combining unified inet nftables rules with systemd sandboxing cuts your blast radius dramatically with zero runtime overhead.

Primary References: Linux Kernel nftables (inet family), systemd.exec(5) & systemd.resource-control(5) · Author: NetDevKit Engineering Team

1. Why Unified inet nftables Replaces Split iptables + ip6tables

Historically, Linux administrators maintained two completely separate rule setsβ€”iptables for IPv4 and ip6tables for IPv6β€”which led to classic dual-stack security gaps where Port 5432 (PostgreSQL) or Port 6379 (Redis) was locked down on IPv4 while remaining wide open to the internet on IPv6. Modern distributions ship nftables as the native kernel packet filter, supporting an atomic dual-stack 'inet' table that filters both IPv4 and IPv6 in one rule set.

An equally important operational advantage is atomic replacement: running 'nft -f /etc/nftables.conf' swaps the entire table inside a single kernel transaction without the brief unprotected window or partial-load lockouts that plagued legacy shell-scripted iptables setups.

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

table inet filter {
  chain input {
    type filter hook input priority 0; policy drop;

    # Always allow loopback and stateful return traffic first
    iifname "lo" accept
    ct state established,related accept
    ct state invalid drop

    # Essential ICMP/ICMPv6 (required for PMTUD & IPv6 Neighbor Discovery)
    ip protocol icmp icmp type { echo-request, destination-unreachable, time-exceeded } accept
    ip6 nexthdr icmpv6 icmpv6 type { echo-request, nd-neighbor-solicit, nd-neighbor-advert, nd-router-advert, packet-too-big } accept

    # Restrict SSH to admin CIDR and allow public HTTPS + WireGuard
    ip saddr 203.0.113.0/24 tcp dport 22 accept
    tcp dport { 80, 443 } accept
    udp dport 51820 accept
  }
}

2. Never Blindly Block All ICMPv6 When Hardening a Host

N

o

t

i

c

e

t

h

e

e

x

p

l

i

c

i

t

i

c

m

p

v

6

a

l

l

o

w

l

i

s

t

a

b

o

v

e

.

U

n

l

i

k

e

I

P

v

4

β€”

w

h

e

r

e

A

R

P

o

p

e

r

a

t

e

s

a

t

L

a

y

e

r

2

b

e

l

o

w

t

h

e

I

P

f

i

r

e

w

a

l

l

β€”

I

P

v

6

r

e

l

i

e

s

e

n

t

i

r

e

l

y

o

n

I

C

M

P

v

6

N

e

i

g

h

b

o

r

D

i

s

c

o

v

e

r

y

(

N

D

P

)

t

o

r

e

s

o

l

v

e

M

A

C

a

d

d

r

e

s

s

e

s

a

n

d

l

e

a

r

n

d

e

f

a

u

l

t

r

o

u

t

e

r

s

,

p

l

u

s

P

a

c

k

e

t

T

o

o

B

i

g

(

T

y

p

e

2

)

f

o

r

P

a

t

h

M

T

U

D

i

s

c

o

v

e

r

y

.

D

r

o

p

p

i

n

g

a

l

l

I

C

M

P

v

6

t

r

a

f

f

i

c

b

r

e

a

k

s

I

P

v

6

n

e

t

w

o

r

k

i

n

g

c

o

m

p

l

e

t

e

l

y

a

n

d

c

a

u

s

e

s

s

i

l

e

n

t

T

L

S

s

t

a

l

l

s

a

c

r

o

s

s

V

P

N

t

u

n

n

e

l

s

.

3. Dropping systemd Exposure Score from 9.6 (UNSAFE) to Under 2.0 (OK)

Even with a tight firewall, a compromised web application running as root (or an unrestricted service user) can overwrite /etc/cron.d, read SSH host keys, or exhaust host RAM until the kernel OOM killer terminates your database. systemd includes built-in Linux namespace, seccomp-bpf, and cgroup v2 sandboxing directives that require zero Docker overhead.

Start by auditing any service with 'systemd-analyze security <unit>.service'. Adding just eight battle-tested directivesβ€”DynamicUser=yes (or dedicated non-root User), NoNewPrivileges=yes, PrivateTmp=yes, ProtectSystem=strict, ProtectHome=yes, CapabilityBoundingSet=, RestrictNamespaces=yes, and cgroup MemoryMaxβ€”regularly slashes a service's exposure rating from 9.6 UNSAFE below 2.0 OK.

[Unit]
Description=Hardened Production API Daemon
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=appuser
Group=appuser
ExecStart=/opt/app/bin/server --listen 127.0.0.1:8080
Restart=on-failure
RestartSec=5s

# Filesystem & Privilege Sandboxing
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/app
CapabilityBoundingSet=
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
SystemCallFilter=@system-service

# cgroup v2 Resource Ceilings
MemoryMax=1G
CPUQuota=150%

[Install]
WantedBy=multi-user.target

4. Verifying Both Layers Before Closing Your SSH Session

B

e

f

o

r

e

a

p

p

l

y

i

n

g

f

i

r

e

w

a

l

l

c

h

a

n

g

e

s

r

e

m

o

t

e

l

y

,

v

a

l

i

d

a

t

e

s

y

n

t

a

x

o

f

f

l

i

n

e

w

i

t

h

'

n

f

t

-

c

-

f

/

e

t

c

/

n

f

t

a

b

l

e

s

.

c

o

n

f

'

a

n

d

s

c

h

e

d

u

l

e

a

s

a

f

e

t

y

r

o

l

l

b

a

c

k

t

i

m

e

r

(

'

s

y

s

t

e

m

d

-

r

u

n

-

-

o

n

-

a

c

t

i

v

e

=

1

2

0

n

f

t

f

l

u

s

h

r

u

l

e

s

e

t

'

)

u

n

t

i

l

y

o

u

c

o

n

f

i

r

m

a

s

e

c

o

n

d

f

r

e

s

h

S

S

H

l

o

g

i

n

s

u

c

c

e

e

d

s

.

A

f

t

e

r

e

n

a

b

l

i

n

g

a

s

a

n

d

b

o

x

e

d

u

n

i

t

,

r

u

n

'

s

y

s

t

e

m

d

-

a

n

a

l

y

z

e

v

e

r

i

f

y

'

a

n

d

'

s

y

s

t

e

m

d

-

a

n

a

l

y

z

e

s

e

c

u

r

i

t

y

'

t

o

i

n

s

p

e

c

t

e

v

e

r

y

r

e

m

a

i

n

i

n

g

c

a

p

a

b

i

l

i

t

y

a

n

d

c

o

n

f

i

r

m

y

o

u

r

a

p

p

o

n

l

y

w

r

i

t

e

s

i

n

s

i

d

e

i

t

s

e

x

p

l

i

c

i

t

R

e

a

d

W

r

i

t

e

P

a

t

h

s

d

i

r

e

c

t

o

r

y

.

Companion Offline NetDevKit Calculators

Apply the formulas from this guide immediately in your browser with zero server uploads:

Frequently Asked Questions

Does UFW or firewalld still use nftables under the hood?

Yes. On modern Ubuntu, Debian, and RHEL/Rocky releases, both UFW (via iptables-nft) and firewalld default to the kernel nftables backend, though writing native /etc/nftables.conf gives cleaner atomic dual-stack control.

What does ProtectSystem=strict do in a systemd unit?

It mounts the entire host filesystem read-only for that service process, except for /dev, /proc, /sys, and any explicit directories you whitelist using ReadWritePaths=.

Why should I always include ct state established,related accept in a drop-policy firewall?

Without an established/related rule, your server's outbound DNS lookups, apt/dnf updates, and API calls will be able to leave the host, but all incoming reply packets will be silently dropped.

More Engineering Guides

Copied to clipboard