WireGuard on Debian: private SSH access and a post-reboot check

We configure a separate tunnel between a laptop and a training VPS, restrict access with nftables, and check SSH, sudo, and automatic startup.

An encrypted tunnel connects a laptop to a server through a firewall.

SSH key login, password-protected sudo, and a firewall were already working on the training VPS. The next step was to add a separate encrypted route from the laptop to the server. We configured WireGuard, checked actual administrative access, and rebooted the server to see whether the tunnel would come up automatically.

The goal is simple: the laptop should connect to the VPS's private address, while the firewall should allow only the new SSH access we need through this interface. We kept public SSH as a separate recovery path.

Below are the fields of the applied configuration and educational equivalents of the commands. Values in <...> and variables such as $WG_IF must be replaced for your own environment: these are not our real addresses, keys, or interface names. The code examples are not a complete script to run. Actual short responses and verified results are identified separately.

First, we defined the tunnel's scope

We chose a connection between two nodes: the server and the laptop. Each side has one peer and one private IPv4 address with a /32 mask. The server knows the laptop's address, and the laptop knows the server's address.

The laptop already has another working tunnel. Before adding the new one, we therefore checked that its addresses and routes would not conflict with the existing network. The new connection received its own keys and configuration. The other routes, policy rules, and DNS remained unchanged.

We chose wg-quick for the new interface. systemd-networkd continues to manage the server's physical interface. On the laptop, NetworkManager sees the new interface as external: we did not create a separate saved profile with automatic connection.

The configurations contain no default route, DNS changes, NAT, forwarding, SaveConfig, network hooks, or PersistentKeepalive. These are the boundaries of our specific verified setup. If incoming connections are needed after a client behind NAT has been idle for a long time, the need for keepalive should be assessed separately. WireGuard explanation.

The package and independent keys

The WireGuard module was already available in the VPS kernel. We installed only wireguard-tools version 1.0.20210914-3, without upgrading or removing other packages.

Installation did not succeed on the first attempt: downloading the package through the CLI stopped at a safety check before dpkg ran. We read the result, checked the initial package state and the verified archive in the cache, separately reconciled the operation's unfinished state, and performed one installation from the verified cache. The final check passed. The exact cause of the initial rejection still needs to be reproduced separately; it would be premature to present it as a fixed automation bug.

Each node generated its own key pair. The private key stayed on the node where it was generated, in files with 0600 permissions. Only public keys were transferred between the nodes.

An educational equivalent of generation for new files in a private directory:

umask 077
set -C
wg genkey > wg-private.key
wg pubkey < wg-private.key > wg-public.key

These commands do not print the private key in the terminal. In our run, generation and writing were handled by a separately verified operator script. The generation principle is described in the official Quick Start.

Server and laptop configuration

Server configuration outline:

[Interface]
PrivateKey = <SERVER_PRIVATE_KEY>
Address = <SERVER_VPN_IP>/32
ListenPort = <WG_PORT>

[Peer]
PublicKey = <CLIENT_PUBLIC_KEY>
AllowedIPs = <CLIENT_VPN_IP>/32

Client configuration outline:

[Interface]
PrivateKey = <CLIENT_PRIVATE_KEY>
Address = <CLIENT_VPN_IP>/32

[Peer]
PublicKey = <SERVER_PUBLIC_KEY>
AllowedIPs = <SERVER_VPN_IP>/32
Endpoint = <SERVER_PUBLIC_IPV4>:<WG_PORT>

The external connection here requires the server's public IPv4 address: the WireGuard ingress rule is deliberately IPv4-only. Endpoint therefore uses <SERVER_PUBLIC_IPV4>, rather than a domain name or an IPv6 address.

AllowedIPs defines the permitted source addresses of packets from a peer and the peer selection for outgoing traffic. In our case, it is a single address of the opposite node. This is not a list of permitted TCP ports: we restrict services separately in the firewall. wg manual.

wg-quick creates routes based on AllowedIPs. The reciprocal /32 entries provide a narrow route to the other end of the tunnel; we did not set a DNS field. wg-quick manual.

Two firewall permissions

The firewall received two new rules: accepting IPv4 WireGuard UDP traffic through the physical interface, and new SSH connections through the tunnel from the only peer's address to the server's private address.

Below are anonymized rule fragments in the existing input chain, not a complete ruleset or a command to replace it:

meta nfproto ipv4 iifname "<UPLINK>" udp dport <WG_PORT> counter name wg_udp accept
iifname "<WG_IF>" ip saddr <CLIENT_VPN_IP> ip daddr <SERVER_VPN_IP> tcp dport 22 ct state new counter name wg_ssh_new accept

We created the named counters separately and placed the rules before the final rejection. The existing rule for established/related connections handles subsequent packets of an allowed session. We applied the changes in one additional transaction without flush ruleset. Public SSH and the forwarding prohibition were preserved.

We also updated the saved policy and its loader. On the server, WireGuard depends on the firewall service, starts after it, and checks its state before bringing up the interface. We therefore checked both the current kernel rules and the preparations for loading them after reboot.

The server drop-in under /etc/systemd/system/wg-quick@${WG_IF}.service.d/ had the following structure; the service name and helper path have been replaced with placeholders:

[Unit]
Requires=<FIREWALL_BOOT_SERVICE>
After=<FIREWALL_BOOT_SERVICE>

[Service]
ExecStartPre=/usr/bin/python3 -I <FIREWALL_BOOT_HELPER> check

Requires adds the firewall loader dependency, while After defines startup ordering. With this combination, a loader startup failure blocks startup of the dependent WireGuard service. ExecStartPre additionally checks the current rules against the expected policy before bringing up the tunnel. systemd dependencies.

<FIREWALL_BOOT_SERVICE> and <FIREWALL_BOOT_HELPER> stand for our already installed service and its verified Python nftables loader from the preceding step in the series. They are not part of wireguard-tools, and this fragment does not install them. Another server needs its own corresponding service and check; replace the placeholders with actual values before use.

First start: active without automatic startup yet

On each node, configuration is stored at /etc/wireguard/${WG_IF}.conf, where $WG_IF is the new interface name on that particular node. The wg-quick@$WG_IF.service instance reads the corresponding file: the instance name and configuration basename must match. Standard wg-quick path.

The file contains PrivateKey, so in our run it is root-owned with 0600 permissions: only root can read or write it. This applies to both server and client configuration; we do not print the private key in the terminal or screenshots.

We started the new instances on the two nodes separately. An educational form of the invocation:

systemctl start "wg-quick@$WG_IF.service"
systemctl show "wg-quick@$WG_IF.service" \
  -p ActiveState -p SubState -p UnitFileState

The actual short stdout from the server check at this stage:

ActiveState=active
SubState=exited
UnitFileState=disabled

wg-quick has a oneshot service with RemainAfterExit=yes: the command that brings up the interface finishes, while the interface remains in the kernel. active/exited is therefore the expected state here. disabled showed that automatic startup of the instance had not yet been enabled. WireGuard service template.

The first start revealed a useful detail: the kernel added a local IPv6 multicast route, ff00::/8. A check expecting all IPv6 routes to remain unchanged stopped, although the interface was already working. We did not restart the service blindly. A separate check allowed only this line created by the kernel for the new interface, confirmed that the interface had no IPv6 addresses, and confirmed that the rest of the network was unchanged.

Runtime check before enabling automatic startup. The field indicating no SSH evidence in this frame refers specifically to this check; SSH was checked separately.

Checking SSH and sudo through the tunnel

A handshake and packet transfer confirmed WireGuard operation. We then opened a new SSH connection through the private addresses, using the SSH key selected after rotation and the already verified server key.

We separately checked the actual connection addresses, server identity, and current boot. Passwordless noninteractive sudo was rejected. After entering the password, sudo returned UID 0.

The new session did not use an SSH password or an existing multiplexed connection. We also checked the effective sshd policy for the VPN source: public-key login was allowed, password and keyboard-interactive authentication were disabled, and new root login was prohibited.

A separate new connection confirmed administrative access through the tunnel.

Checking a closed port

To check the firewall restriction, we temporarily started another TCP listener. We first contacted it through loopback, and it responded. A connection to the same port through WireGuard was not established. A repeat loopback control check passed again.

Observing packet headers on the new interface captured two matching incoming SYN packets. The rejected-packet counter increased, but we do not attribute its total value exclusively to our test. The listener exited normally, the sockets closed, and the temporary port was gone after cleanup. This checks one specific closed port, rather than all possible traffic directions or unauthorized peers.

The frame shows the negative test; the final two SYN packets and completed cleanup were confirmed by a separate result after this frame.

Enabling automatic startup and rebooting the server

After checking runtime, we enabled automatic startup only for the new server instance. We kept manual startup on the laptop.

systemctl --no-reload enable "wg-quick@$WG_IF.service"
systemctl daemon-reload

Between these actions, we checked the created automatic-startup link and the persistence of its directory to disk. We did not run start or restart during this process. Enabling automatic startup and starting a service are separate systemd actions. systemctl manual.

No reboot had taken place at this step.

Before rebooting, we once again opened separate public and VPN SSH connections and checked password-protected sudo. An independent provider console was available. We then requested one normal reboot:

systemctl --no-block reboot

The operator log recorded dispatch of the request before it was executed, so an uncertain transport response would not cause a repeat reboot. In the console, we observed EFI loading, the kernel, and a new login prompt. Once SSH returned, we checked that this was the same server, but a different boot.

Result after the new boot

Verified properties of the server instance after reboot:

ActiveState=active
SubState=exited
UnitFileState=enabled

These are verified property values, not a separately recorded raw stdout containing these three lines. Automatic startup is also confirmed by the new boot and a fresh SSH connection through the tunnel.

Comparison showed unchanged keys, configuration, peer, and narrow routes. The complete kernel ruleset matched the saved policy, and the firewall loader had a successful record for this specific new boot. SSH, systemd-networkd, and the firewall were working; there were no systemd services in the failed state.

The post-reboot check only read the state and compared it with the plan.

We then performed two more independent logins: through the public address and through WireGuard. Both succeeded with the selected key and verified server identity. In both, passwordless sudo was rejected, while password-protected sudo obtained UID 0.

Both administrative access paths work after reboot.

All six frames show real terminals with fresh summaries of operator checks. These are not raw command logs from wg, systemctl, or ssh.

We have a separate narrow route for administering the training VPS, a verified rejection of one additional port, and server automatic startup that has passed a real reboot. Public SSH remains available. The next engineering step is to move this verified process into the shared Go CLI; fully automatic preparation of a clean server has not yet been implemented.

  1. Operations & Security nftables default-deny without losing server control
  2. Operations & Security A separate administrator: from key login to the right privileges
  3. Operations & Security Your first SSH login: recognising your server
  4. Operations & Security Recovering VPS access when SSH stops working
  5. Operations & Security Planned SSH key rotation: verifying new access and revoking the old key