Command Palette

Search for a command to run...

Hectal
PHASE 6Advanced ~15 min· topic 7 of 8

Topic 6.7

VPNs & Tunnels: WireGuard, IPsec & SSH Tunnels

In one line

A tunnel wraps packets inside other packets so they can cross a network that wouldn't carry them directly, and a VPN is an encrypted tunnel. WireGuard is a small, modern VPN using public keys. IPsec is the long-standing standard for site-to-site links, including cloud VPN gateways. SSH tunnels forward single ports for quick, ad-hoc access. Tunnels add headers, so they lower the MTU (Topic 4.6).

0/8 · 0%

Think of it like this

Posting a letter inside a sealed courier pouch. The courier only sees the pouch's address (the tunnel endpoints), not the letter inside or who it's really for.

Words you'll meet

New words in this topic, in plain English. Come back here whenever one feels fuzzy.

Tunnel
Carrying packets inside other packets between two endpoints.
VPN
Virtual Private Network: an encrypted tunnel connecting networks or devices.
WireGuard
A modern, minimal VPN protocol using public-key peers over UDP.
IPsec
A suite of protocols for encrypted IP tunnels, common for site-to-site links.
AllowedIPs
In WireGuard, the address ranges routed to a peer and accepted from it.
SSH local forwarding
Using ssh -L to send a local port's traffic through an SSH server to another host.
Split tunnel
Sending only some traffic (private ranges) through the VPN and the rest directly.

Step by step

01A WireGuard link to the kitchen partner

Tiffin needs a private link from its Mumbai VPC to a partner kitchen's network (10.20.0.0/16). Each side generates a key pair and puts the other's public key in its config. Only traffic to the listed AllowedIPs goes through the tunnel.

/etc/wireguard/wg0.conf (Tiffin gateway)whole filetext
[Interface]
Address    = 10.99.0.1/30
ListenPort = 51820
PrivateKey = <contents of /etc/wireguard/private.key>   # never commit this
MTU        = 1420

[Peer]
# partner kitchen gateway
PublicKey  = 9mQ2...partner-public-key...=
Endpoint   = 198.51.100.24:51820
AllowedIPs = 10.99.0.2/32, 10.20.0.0/16   # tunnel address + partner network
PersistentKeepalive = 25                  # keeps NAT mappings open
terminal
$ wg genkey | sudo tee /etc/wireguard/private.key | wg pubkey
sudo wg-quick up wg0
sudo wg show wg0 | head -8
── expected output ──
p6xF...tiffin-public-key...=
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.99.0.1/30 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] ip -4 route add 10.20.0.0/16 dev wg0
interface: wg0
public key: p6xF...tiffin-public-key...=
listening port: 51820
 
peer: 9mQ2...partner-public-key...=
endpoint: 198.51.100.24:51820
allowed ips: 10.99.0.2/32, 10.20.0.0/16
latest handshake: 12 seconds ago
'latest handshake' proves the tunnel is up. No handshake usually means a blocked UDP port or a wrong key or endpoint.
A WireGuard link to the kitchen partnerdiagram
Rendering diagram…

02Site-to-site IPsec with a cloud VPN gateway

For the head office, Tiffin uses AWS Site-to-Site VPN: AWS runs two IPsec tunnels (for redundancy) to the office router, with BGP exchanging routes. The office router config comes from AWS's downloadable template, and both tunnels should show UP.

terminal
$ aws ec2 describe-vpn-connections --query 'VpnConnections[0].VgwTelemetry[].[OutsideIpAddress,Status,StatusMessage]' --output text
── expected output ──
13.126.10.11 UP 2 BGP ROUTES
35.154.22.80 UP 2 BGP ROUTES

03A quick SSH tunnel

Priya needs to run one query against the private database from her laptop. Instead of opening the database to the internet, she forwards a local port through the bastion host. The connection exists only while the SSH session runs.

terminal
$ ssh -N -L 5433:db.tiffin.internal:5432 priya@bastion.tiffin.in &
psql -h 127.0.0.1 -p 5433 -U readonly tiffin -c 'select count(*) from orders'
── expected output ──
count
-------
48213
(1 row)

Break it on purpose

Errors are the best teachers. Make each change, read the error, guess what went wrong, then reveal the answer.

Break #1

The tunnel that swallowed the internet

Arjun sets AllowedIPs = 0.0.0.0/0 on his laptop's WireGuard peer to 'make sure everything works'.

terminal
$ ip route get 1.1.1.1
curl -s -m 5 https://www.google.com -o /dev/null -w '%{http_code}\n'
── what you'll see ──
1.1.1.1 dev wg0 table 51820 src 10.99.0.6 uid 1000
000
All his traffic now goes into the tunnel, and the office gateway isn't set up to forward it to the internet.

Myth vs fact

Myth

A VPN makes everything behind it trusted.

Fact

Once connected, a compromised laptop can reach the whole network. Limit what VPN users can reach and authenticate services individually (zero-trust access).

Pro corner

Extra depth for experienced readers. New to this? Skip it for now and come back later.

  • ▸

    WireGuard is quiet by design: it doesn't answer unauthenticated packets, so port scans can't even tell it's there. For debugging, wg show (handshake times and transfer counters) and tcpdump -ni eth0 udp port 51820 tell you whether encrypted packets flow at all.

Remember this

  1. 1

    Tunnel = encapsulation: the original packet becomes the payload of an outer packet between two tunnel endpoints. VPN = a tunnel that also encrypts and authenticates.

  2. 2

    WireGuard: peers identified by public keys, a few lines of config, runs over UDP (default port 51820), built into the Linux kernel. AllowedIPs says which destination addresses go through each peer.

  3. 3

    IPsec: the standard for site-to-site VPNs and cloud VPN gateways (AWS Site-to-Site VPN). Uses IKE to negotiate keys and ESP to carry encrypted traffic, with UDP 500/4500 for setup and NAT traversal.

  4. 4

    Remote-access VPNs (WireGuard, OpenVPN, commercial ones) connect laptops to private networks. Many teams now use identity-aware proxies or 'zero trust' access instead, which check user and device per request rather than trusting the whole network.

  5. 5

    SSH tunnels: ssh -L 5433:db.internal:5432 bastion forwards a local port through a server you can SSH to. Handy for one-off database access, but not a replacement for a proper VPN or access proxy.

  6. 6

    Every tunnel adds overhead (around 60–80 bytes), so set the tunnel MTU (WireGuard defaults to 1420) or clamp MSS.

Explain it without notes

01

What does WireGuard's AllowedIPs do?

02

When is an SSH tunnel the right tool, and when isn't it?

Practice

01

Forward a remote web service on port 8080 to your laptop through a bastion.

02

Check whether a WireGuard tunnel is actually passing traffic.

Trade-offs

  • ↔

    WireGuard is simple and fast but has fewer enterprise features (no built-in user management). IPsec is interoperable with every vendor and cloud but complex to configure. VPNs grant broad network access, while zero-trust proxies grant per-application access at the cost of more setup.

Done when you can

  • I can explain tunnels and VPNs.

  • I can configure a WireGuard peer with sensible AllowedIPs.

  • I can use an SSH tunnel for one-off access.