- 4 Docker Setup and 5 Firewalld Configuration must be done (this part changes the configuration of both).
Assumptions
- The outside network interface is
eth0, it is managed by NetworkManager, and its IPv6 address is set statically. - The VPN networks are
10.8.0.0/24(IPv4) andfd42:8::/64(IPv6); the server’s VPN address is10.8.0.1/fd42:8::1. - WireGuard listens on port
51820/udp. - Commands are run on the server with root rights (
sudo).
We’ll use WireGuard for these things:
- Sending clients to the internet through the VPN
- Letting clients talk to each other (for example a laptop and a phone)
- Making some services on the server (for example the iRedAdmin panel) reachable only over the VPN instead of by everyone
Installation#
sudo dnf install wireguard-tools qrencodeThe WireGuard kernel module is already in the Rocky Linux 10 kernel, so we only install the tools. qrencode is for showing the phone configuration as a QR code.
Creating the Key Pair#
Let’s create the key pair the WireGuard service will use:
sudo sh -c 'cd /etc/wireguard && umask 077 && wg genkey | tee server.key | wg pubkey > server.pub'cd and umask are commands of the shell itself, so running them as sudo cd or sudo umask doesn’t affect the current shell. That’s why we run the commands in a single root shell with sudo sh -c. umask 077 makes sure only root can read the key files we create.
Permanent IP Forwarding#
We turn on IP forwarding so that packets can be passed from one interface to another (in practice, so that traffic from the VPN interface can go out to the internet).
Docker already turns forwarding on, but only when its own service starts. This file turns forwarding on at boot, independent of Docker.
Check IPv6 First#
On Linux, once IPv6 forwarding is on, the kernel no longer accepts Router Advertisements (RA). If the VPS gets its IPv6 address through RA (SLAAC), the IPv6 connection may drop the moment we turn forwarding on. So before turning forwarding on, let’s check the default IPv6 route:
ip -6 route show defaultThe output should look similar to this:
default via fe80::1 dev eth0 proto static metric 100 pref mediumOn this server IPv6 is static (proto static) and RA is not used, so turning forwarding on doesn’t affect the IPv6 connection. If the output shows proto ra, you also need to add the line net.ipv6.conf.eth0.accept_ra = 2 to the file below.
Turning On Forwarding#
Let’s create the file /etc/sysctl.d/90-wireguard.conf with the content below:
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1Let’s apply the settings:
sudo sysctl --systemWireGuard Configuration#
Let’s create /etc/wireguard/wg0.conf like this:
[Interface]
Address = 10.8.0.1/24, fd42:8::1/64
ListenPort = 51820
PrivateKey = <server.key><server.key>: The contents of the file /etc/wireguard/server.key (sudo cat /etc/wireguard/server.key).
Let’s set the file’s permissions so only root can read it:
sudo chmod 600 /etc/wireguard/wg0.confFirewalld Configuration#
Binding the Outside Interface to the Public Zone Explicitly#
sudo nmcli -t -f NAME,DEVICE con show --active
sudo nmcli con modify "<CONNECTION_NAME>" connection.zone public<CONNECTION_NAME>: The name of the connection that belongs to eth0 in the output of the first command.
Opening the WireGuard Port#
sudo firewall-cmd --permanent --zone=public --add-service=wireguardCreating a Zone for the VPN in Firewalld#
# Creates a new, empty zone named vpn
sudo firewall-cmd --permanent --new-zone=vpn
# Binds the wg0 interface to this zone. From now on, all traffic from the WireGuard tunnel is checked against the rules of the vpn zone, not the public zone.
sudo firewall-cmd --permanent --zone=vpn --add-interface=wg0
# Sets which services on the server the traffic from the VPN may reach:
sudo firewall-cmd --permanent --zone=vpn \
--add-service=ssh --add-service=http --add-service=https \
--add-service=smtp-submission --add-service=smtps \
--add-service=imap --add-service=imaps
# Allows forwarding inside the same zone, so clients can talk to each other.
sudo firewall-cmd --permanent --zone=vpn --add-forwardWhy we allow the services that are already allowed in the public zone again in the VPN zone: when a client is connected to the VPN, its traffic to the server (with a full tunnel, even the traffic to the server’s public IP) comes in through wg0. If we don’t allow these services in the VPN zone, the client can’t reach them while it is connected to the VPN.
From the VPN to the Internet#
A zone manages traffic that comes to the server itself. For example: “Can someone coming from the VPN connect to SSH on the server?”
A policy manages traffic that goes through the server, so packets forwarded from one zone to another.
In Firewalld, forwarding between zones is off by default. So for the VPN clients to reach the internet, we need to define a policy explicitly.
# Creates a new, empty policy named vpn-internet.
sudo firewall-cmd --permanent --new-policy=vpn-internet
# Sets the direction of the policy. It applies when traffic comes from the vpn zone (wg0) and goes to the public zone (the outside interface, so the internet).
sudo firewall-cmd --permanent --policy=vpn-internet --add-ingress-zone=vpn
sudo firewall-cmd --permanent --policy=vpn-internet --add-egress-zone=public
# Accepts traffic in this direction. A VPN client can go to any address on the internet; port 25 is blocked by the no-smtp-out policy from part 5.
sudo firewall-cmd --permanent --policy=vpn-internet --set-target=ACCEPT
# Turns on NAT.
sudo firewall-cmd --permanent --policy=vpn-internet --add-masquerade
# --add-masquerade only applies to IPv4, IPv6 needs a separate rich rule
sudo firewall-cmd --permanent --policy=vpn-internet \
--add-rich-rule='rule family="ipv6" source address="fd42:8::/64" masquerade'Let’s apply the rules we added to Firewalld:
sudo firewall-cmd --reloadLet’s check the zone and the policy:
# Shows which interface is in which zone
sudo firewall-cmd --get-active-zonesdocker
interfaces: docker0
public (default)
interfaces: eth0
vpn
interfaces: wg0# Shows the settings of the policy
sudo firewall-cmd --info-policy=vpn-internetvpn-internet (active)
disable: no
priority: -1
target: ACCEPT
ingress-zones: vpn
egress-zones: public
services:
ports:
protocols:
masquerade: yes
forward-ports:
source-ports:
icmp-blocks:
rich rules:
rule family="ipv6" source address="fd42:8::/64" masqueradeThe no-smtp-out policy we created in 5 Firewalld Configuration uses ANY as the ingress zone, so it applies to the VPN clients too. Its priority (-10) is smaller than that of vpn-internet (-1), so it is checked first. After adding a client (7.1), while it is connected with the full tunnel profile, the command nc -v -w 5 gmail-smtp-in.l.google.com 25 </dev/null on the client should give a connection error. On Windows, Test-NetConnection gmail-smtp-in.l.google.com -Port 25 should return TcpTestSucceeded : False.
Docker’s FORWARD DROP Policy#
Docker sets the default policy of the FORWARD chain in iptables to DROP. Even if Firewalld allows the VPN traffic, the packet also passes through Docker’s table, and the DROP policy drops it.
Let’s check whether this policy is in place:
sudo iptables -S FORWARD | head -1
sudo ip6tables -S FORWARD | head -1If the output shows -P FORWARD DROP, the policy is in place.
Let’s stop Docker from setting the DROP policy by adding the line below to /etc/docker/daemon.json:
"ip-forward-no-drop": trueThe file should look like this:
{
"ip": "127.0.0.1",
"default-network-opts": {
"bridge": { "com.docker.network.bridge.host_binding_ipv4": "127.0.0.1" }
},
"log-driver": "journald",
"live-restore": true,
"ip-forward-no-drop": true,
"default-address-pools": [
{ "base": "172.20.0.0/16", "size": 24 }
]
}Let’s check the Docker configuration:
sudo dockerd --validate --config-file /etc/docker/daemon.jsonIf we got the configuration OK output, let’s restart Docker and fix the FORWARD policy:
sudo systemctl restart docker
sudo iptables -P FORWARD ACCEPT && sudo ip6tables -P FORWARD ACCEPTRestarting Docker doesn’t change the current DROP policy. The ip-forward-no-drop setting stops Docker from setting the policy to DROP from now on. The policy that is DROP right now doesn’t fix itself, so we set it to ACCEPT by hand once.
This doesn’t mean forwarded traffic is no longer filtered. Firewalld keeps filtering forwarded traffic with the zone and policy rules in its own nftables table. The ACCEPT policy in iptables only removes the second block Docker put there.
Starting WireGuard#
Let’s enable and start the service with the commands below:
sudo systemctl enable --now wg-quick@wg0
sudo wg showThe output should show interface: wg0 and listening port: 51820.
AllowedIPs = 0.0.0.0/0, ::/0), so the device goes out to the internet with the server’s IP address. See also split tunnel.accept_ra is set to 2.