This episode builds a site-to-site L2TP/IPsec connection between two network gateways. You assemble the tunnel mode configuration on both sides, connect the two LNSes through xl2tpd, set up static and dynamic routing, and open the required firewall ports.

So far we built remote access: one user connects to one server. Episode 12 changes the scenario to site-to-site — two networks separated by the internet connected through two gateways. Every machine at the branch office can talk to machines at headquarters as if they were on the same network.
The site-to-site L2TP/IPsec topology uses two gateways that both act as LNS. Because traffic crosses two different networks, tunnel mode is the correct choice (see episode 5). We will assemble the configuration on both sides, connect them, and then set up routing.
Imagine headquarters with the 10.10.10.0/24 network and a branch with 192.168.1.0/24. Both gateways have their own public IP:
Kantor Pusat: 10.10.10.0/24 - gateway 203.0.113.10 (LNS)
Kantor Cabang: 192.168.1.0/24 - gateway 198.51.100.20 (LNS)Traffic from the branch toward headquarters goes through the L2TP/IPsec tunnel built between the two gateways. Because both sides are LNS, the tunnel is initiated from one side acting as a logical LAC — in this example the branch side starts the connection.
In Libreswan, the site-to-site connection uses tunnel mode with leftsubnet and rightsubnet:
conn branch-to-hq
type=tunnel
left=203.0.113.10
leftsubnet=10.10.10.0/24
right=198.51.100.20
rightsubnet=192.168.1.0/24
authby=secret
ikev2=insist
ike=aes256-sha2;modp2048
esp=aes256-sha2
auto=startNote auto=start — the headquarters gateway actively builds the connection at boot instead of waiting.
The branch side is the mirror image:
conn branch-to-hq
type=tunnel
left=198.51.100.20
leftsubnet=192.168.1.0/24
right=203.0.113.10
rightsubnet=10.10.10.0/24
authby=secret
ikev2=insist
ike=aes256-sha2;modp2048
esp=aes256-sha2
auto=startThe secrets on both sides must be consistent. The format in /etc/ipsec.secrets for a host-pair PSK:
203.0.113.10 198.51.100.20 : PSK "psk-site-to-site-yang-kuat"So that the gateways forward traffic between networks, enable kernel forwarding:
sudo sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-ipsec.confIn addition, set up routing: machines behind the gateway must know that the remote network is reachable through the gateway. Add static routes on every LAN router, or enable dynamic routing — for example OSPF over the ppp0 interface — for larger networks.
Open the required ports and allow the VPN subnets through the firewall:
sudo nft add rule inet filter input udp dport 500 accept
sudo nft add rule inet filter input udp dport 4500 accept
sudo nft add rule inet filter forward ip saddr 192.168.1.0/24 accept
sudo nft add rule inet filter forward ip saddr 10.10.10.0/24 acceptDo not forget to add a MASQUERADE rule or correct return routing, because traffic from the VPN subnets must be able to get back to its origin network.
After both sides are running, verify from one of the gateways:
sudo ipsec statusall
ping -c 3 198.51.100.20If the SA is up and the cross-gateway ping succeeds, continue from a machine behind the gateway:
ping -c 3 10.10.10.5This step tests full routing — not just IPsec — because the packets must enter the tunnel at the gateway and then be forwarded to the destination machine in the other subnet.
The two most common mistakes in site-to-site deployments. First, the PSK differs between the two sides — the IKE negotiation will fail and the log shows authentication failed. Second, leftsubnet and rightsubnet are swapped — the SA forms but traffic never arrives because the destination subnet does not match. Check both before touching the firewall.
Tip
If the ping between gateways succeeds but the ping between subnets fails, the problem is almost certainly routing or firewall, not IPsec. Use traceroute and check the ip route table at every hop.
Episode 12 built a full site-to-site L2TP/IPsec setup: a two-LNS topology with tunnel mode, mirror configurations on both gateways, a host-pair PSK in ipsec.secrets, IP forwarding and routing, and firewall rules.
Key takeaways:
leftsubnet and rightsubnet define the networks behind the gateways.net.ipv4.ip_forward on both gateways.In the next episode, episode 13, we will discuss security analysis and attack vectors — threats such as PSK brute-force and Aggressive Mode, downgrade attacks, and mitigation with IKEv2, certificates, and firewall hardening.