Debian 13 Tutorial / Configure nftables and Verify New SSH Access

Before applying nftables on Debian 13, allow new SSH connections and the network traffic the server needs. Check the candidate rules, schedule rollback and test a separate SSH connection before saving a boot policy. After reboot, verify the actual kernel rules and connectivity as well as the service state.

Confirm the Isolated Lab’s Initial State

nftables is a system managing the Linux kernel's packet filtering functions, and nft is a command to view and apply rules. Here, we practice on a dedicated VirtualBox NAT VM with Debian 13 minimal server installed. The initial kernel rules are empty, and nftables.service is in a disabled and inactive state. SSH via loopback NAT forwarding is connected from host port 22213 to guest port 22.

sudo apt update
sudo apt install nftables
sudo nft --version
sudo nft list ruleset
systemctl is-enabled nftables.service
systemctl is-active nftables.service
systemctl cat nftables.service
sudo ss -lntp
ip -br address
ip route

In this practice, nftables 1.1.3 and TCP 22 reception on the guest were confirmed. The disabled state of is-enabled and the inactive state of is-active indicate inactivity, so the command's exit code may not be 0. If a table already exists in nft list ruleset or another firewall manager is running, the full replacement example below should not be applied. In particular, do not delete rules managed by containers, VPNs, routers, UFW, or firewalld.

The flush ruleset directive below deletes the entire nftables ruleset. It is used only to replace this dedicated VM’s complete configuration, starting from empty rules. Do not use the example as a way to add a small allowance to an existing production firewall. The Debian 13 nft manual describes rulesets, address families and validity checking.

First, verify on the VirtualBox console whether a normal user login and sudo authentication are possible. Prepare two SSH windows and test a new connection in another window without disconnecting the current session. Just having the existing SSH connection alive does not prove that new access is allowed. On servers where the SSH port has been changed, the rules must be adjusted according to the guest's actual listening port.

Understand the Policy and Required Traffic

This example blocks incoming input to the server itself by default and allows outgoing output from the server. forward relaying packets between other devices is blocked. Therefore, this configuration is not suitable for using this VM as a router or container network gateway.

Rules Reasons and scope
inet table Handles both IPv4 and IPv6 to reduce the mistake of opening only one side
lo allowed Maintain server internal loopback communication
established,related Allow communication captured by connection tracking and corresponding responses
ICMP·ICMPv6 Maintain error delivery, path MTU, and IPv6 neighbor discovery operations; here, allow the entire relevant protocol
UDP 67→68, 547→546 Allow server responses to DHCPv4 and DHCPv6 clients
TCP destination 22 new Allow new SSH connections; distinguish from the host's forwarded port 22213

ct refers to connection tracking: the kernel records connection state so rules can identify established traffic and related packets. For this VM’s outbound DNS and web requests, the output policy permits requests and the state rule permits associated replies. This VM is not a DNS server, so no new inbound DNS-query allowance is needed. Design additional destination-port rules when adding a server role.

Back Up the Original State and Write a Candidate

If the path below already exists, check the previous practice records and do not overwrite them. Proceed only if you have confirmed that the existing kernel rules are empty and the service is disabled/inactive.

sudo test ! -e /var/backups/lf-debian13-16 &&
sudo install -d -m 700 /var/backups/lf-debian13-16 &&
sudo cp -a /etc/nftables.conf /var/backups/lf-debian13-16/nftables.conf &&
sudo sh -c 'nft list ruleset > /var/backups/lf-debian13-16/rules-before.nft'

Next, create a restore script. It is intended to restore the empty rules of this dedicated VM, and running it may delete rules newly created by other programs, so do not add other network management tools during the practice.

cat <<'EOF' | sudo tee /var/backups/lf-debian13-16/restore.sh > /dev/null
#!/bin/sh
set -eu
{ printf 'flush ruleset\n'; cat /var/backups/lf-debian13-16/rules-before.nft; } | /usr/sbin/nft -f -
EOF
sudo chmod 700 /var/backups/lf-debian13-16/restore.sh

Create a candidate file. /etc/nftables.conf is not modified yet. The configuration syntax is specific to nft, so the following block is the file content, not a command to run directly in the shell.

Open the file as sudo nano /var/backups/lf-debian13-16/candidate.nft and save the entire content. If nano is not installed, choose the editor you prefer to use.

flush ruleset
table inet lf_server {
    chain input {
        type filter hook input priority filter; policy drop;
        iifname "lo" accept
        ct state invalid drop
        ct state established,related accept
        meta l4proto { icmp, ipv6-icmp } accept
        udp sport 67 udp dport 68 accept
        udp sport 547 udp dport 546 accept
        tcp dport 22 ct state new accept
        counter drop
    }
    chain forward {
        type filter hook forward priority filter; policy drop;
    }
    chain output {
        type filter hook output priority filter; policy accept;
    }
}
sudo nft --check --file /var/backups/lf-debian13-16/candidate.nft
sudo nft list ruleset

Upon successful verification, it usually exits with code 0 with no separate output, and the kernel rules should still be empty. The --check option validates commands without applying changes. However, a syntactically correct SSH block rule may still pass the check, so it cannot replace connection verification. If an error occurs during the check, modify the corresponding file/line and recheck before proceeding.

Schedule Rollback Before Applying Rules

Even if the remote connection is disconnected, first schedule the recovery to run independently of the SSH session. The 3 minutes is an example for this exercise, so adjust it according to the verification time. If a previous timer with the same name exists, check its status first.

sudo systemd-run --unit=lf-nft16-rollback --on-active=3min --timer-property=AccuracySec=1s /var/backups/lf-debian13-16/restore.sh
systemctl is-active lf-nft16-rollback.timer
systemctl list-timers lf-nft16-rollback.timer --no-pager
sudo nft --file /var/backups/lf-debian13-16/candidate.nft
sudo nft list ruleset

After confirming that the timer is active and the scheduled execution time is displayed, apply it. This temporary timer created with systemd-run will not remain as a recovery device after a reboot. Do not reboot at this stage.

Open a new SSH connection from another host terminal and verify that commands can be executed. From here, connect from the host PowerShell as follows.

ssh -p 22213 lfstudent@127.0.0.1

In the newly logged-in session, separate from the existing connection, check the address, path, name lookup, and outbound communication as well.

ip -br address
ip route
getent ahostsv4 deb.debian.org
curl --noproxy '*' --connect-timeout 3 --max-time 10 -fsS https://deb.debian.org/debian/README
sudo nft list chain inet lf_server input

In this exercise, we verified a new SSH connection and outbound HTTPS after applying the correct rules. There is no need to apply incorrect rules and disconnect the connection in practice. To verify the recovery path, wait for the scheduled time, then check from the console or a new SSH session whether sudo nft list ruleset is empty again and whether there are execution results in sudo journalctl -u lf-nft16-rollback.service --no-pager. In the actual verification, we confirmed the recovery of the originally empty rules and that the saved files were unchanged with a 35-second scheduled time.

After the scheduled recovery test succeeds, to make a reservation again under the same name, stop the remaining timer using sudo systemctl stop lf-nft16-rollback.timer and then rerun the reservation and apply block. Distinguish this from canceling a recovery that has not yet been executed. If SSH disconnects, do not continue applying other rules; reconnect after the scheduled recovery. If the recovery also fails, run sudo /var/backups/lf-debian13-16/restore.sh from the console and check the listening port and NAT forwarding.

Compare a Running Service with Input Filtering

Use the dedicated Python server and host-loopback forwarding from the DNS, ports and HTTP diagnostics lab to compare firewall effects on guest IPv4 port 8080. Let the previous rollback test finish, then schedule rollback again before reapplying the candidate. If its timer is still pending, check the remaining time rather than creating a duplicate.

  1. For an empty original rule, first check whether the host can receive files normally.
  2. After applying candidate rules, compare whether new SSH succeeds but host HTTP requests fail.
  3. Check whether the loopback HTTP requests inside the guest and the ss listening rows are maintained.
  4. Check whether the last drop counter in the input chain increases.
sudo ss -lntp 'sport = :8080'
curl --noproxy '*' --connect-timeout 3 --max-time 6 -fsS http://127.0.0.1:8080/probe.txt
sudo nft list chain inet lf_server input

In the actual comparison, the loopback response of the running server succeeded, the host request failed, and the last block counter increased. The counter represents the number of packets that matched that drop rule, so it may include other traffic as well. As in this exercise, you need to look at the request location and time together with the server's listening state.

To check the difference, you can insert the rule allowing 8080 at the front only in the dedicated practice table.

sudo nft insert rule inet lf_server input tcp dport 8080 accept

If the same host request succeeds again, it means the difference between service listening and the input filter has been checked. This command is applied only to the current kernel and does not modify the saved file. After the comparison, if you reapply the original candidate file, the temporary 8080 allowance will disappear. Reapply the entire candidate only on a dedicated VM without rules added by other programs. Also, terminate and delete the HTTP server and its NAT forwarding rules as part of the cleanup procedure from the previous exercise.

Save Boot Configuration and Verify Persistence

If you have tested the scheduled recovery, schedule the recovery timer again, apply the candidate, and then confirm that the new SSH succeeds. Do not cancel the recovery timer before confirming success. After deciding to maintain the rule, execute the following.

sudo systemctl stop lf-nft16-rollback.timer
sudo cp /var/backups/lf-debian13-16/candidate.nft /etc/nftables.conf
sudo nft --check --file /etc/nftables.conf
sudo systemctl enable --now nftables.service
systemctl is-enabled nftables.service
systemctl is-active nftables.service
sudo nft list ruleset

enable is set to start at boot, and --now is requested to start immediately. The services in this environment follow the oneshot with RemainAfterExit method, so they may be shown as active (exited) even after the rules are read and the process has finished. Just because the resident daemon is not visible does not mean the firewall is turned off.

After confirming the new SSH connection again, reboot while keeping the console secured.

sudo systemctl reboot

After booting is complete, reconnect and check not only that the service is enabled and active, but also the actual contents of sudo nft list ruleset, the addresses, default route, DNS lookup and outbound requests again. Do not conclude there is a rule error just because one connection failed during the startup; also check the console's boot status. In this VM, after the boot ID changed, the lab verified new SSH access, DHCP addressing, DNS, outbound HTTPS and persistent rules.

Restore the Original File, Ruleset and Service State

The following restores only this VM’s original disabled/inactive service and empty ruleset. The installed unit shown by systemctl cat nftables.service ran nft flush ruleset when stopped, so stopping the service also cleared all kernel rules. Do not reuse this cleanup on a server with rules managed by other network tools.

sudo systemctl disable --now nftables.service
sudo cp -a /var/backups/lf-debian13-16/nftables.conf /etc/nftables.conf
sudo /var/backups/lf-debian13-16/restore.sh
sudo cmp /etc/nftables.conf /var/backups/lf-debian13-16/nftables.conf
sudo nft list ruleset
systemctl is-enabled nftables.service
systemctl is-active nftables.service

Confirm that cmp succeeded without output, the current rules are empty, and the service has returned to disabled and inactive. Also reopen a new SSH and check that connection works normally. After reverting, the guest firewall protection in this practice will disappear, so in actual operations, you must distinguish whether returning to the original state or applying the desired security policy is necessary.