Online admissions open for the SEO, web development and networking & cybersecurity courses. Enquire for the next batch.

How a VPN Works: Tunnelling, IPsec, TLS VPNs and WireGuard

5 min read Updated By the SkillNewton team

What is a VPN?

A VPN (virtual private network) creates an encrypted tunnel across an untrusted network such as the internet, so two endpoints can exchange traffic as if they shared a private link. Organisations use VPNs to connect branch offices and to let remote staff reach internal systems. Common technologies are IPsec, SSL/TLS VPNs such as OpenVPN, and WireGuard.

Tunnelling and the key terms

Tunnelling means wrapping one packet inside another. The original packet, with its private source and destination addresses, becomes the encrypted payload of an outer packet addressed between the two VPN endpoints. Routers on the internet see only the outer header; the far endpoint removes it, decrypts the inner packet and forwards it on the private network.

  • Confidentiality: encryption stops anyone on the path reading the inner traffic.
  • Integrity: a message authentication code shows if a packet was altered.
  • Authentication: each endpoint proves its identity with certificates, keys or a pre-shared secret.
  • VPN gateway or concentrator: the device or server that terminates tunnels.

The encryption ideas behind this are explained in the cryptography guide.

IPsec: IKEv2, ESP and the two modes

IPsec is a suite of standards that protects IP packets at the network layer. It has two stages. First, IKEv2 (Internet Key Exchange) authenticates the peers and agrees keys, using UDP port 500. Second, ESP (Encapsulating Security Payload, IP protocol 50) encrypts and authenticates the data. When a NAT device sits between the peers, ESP is wrapped in UDP port 4500, known as NAT traversal.

  • Tunnel mode: the whole original packet is encrypted and given a new outer header. This is the usual mode for site-to-site VPNs.
  • Transport mode: only the payload is protected and the original header is kept, used for host-to-host protection.
  • AH (Authentication Header, IP protocol 51) provides integrity without encryption and does not work through NAT, so ESP is the normal choice.

IPsec is built into enterprise firewalls and routers from every major vendor and into Windows, macOS, iOS, Android and Linux (through strongSwan or Libreswan), which makes it the common choice for connecting sites run on different equipment.

SSL/TLS VPNs and WireGuard

An SSL/TLS VPN uses TLS to protect the tunnel, usually over TCP port 443 and sometimes over DTLS on UDP. Because port 443 is open on most networks, these VPNs work from hotels and public Wi-Fi where IPsec may be blocked. Some offer a browser portal for specific web applications; others install a client that carries all traffic. OpenVPN is a widely used open-source example with its own TLS-based protocol, commonly on UDP port 1194.

WireGuard is a newer protocol with a deliberately small design. It has been part of the Linux kernel since version 5.6 and has clients for all major platforms. It runs over UDP, uses a fixed set of modern algorithms (Curve25519, ChaCha20-Poly1305 and BLAKE2s) and identifies each peer by its public key. Its AllowedIPs setting does two jobs: it lists which addresses may arrive from a peer and which destinations are routed to it.

# Generate a key pair on each peer
umask 077
wg genkey | tee privatekey | wg pubkey > publickey

# /etc/wireguard/wg0.conf on the client
[Interface]
PrivateKey = <client-private-key>
Address = 10.8.0.2/24

[Peer]
PublicKey = <server-public-key>
Endpoint = vpn.example.com:51820
AllowedIPs = 10.8.0.0/24, 192.168.50.0/24
PersistentKeepalive = 25

# Bring the tunnel up and check the handshake
sudo wg-quick up wg0
sudo wg show

Site-to-site, remote access and split tunnelling

ModelEndpointsTypical technology
Site-to-siteTwo gateways joining whole networks, such as head office and a branchIPsec tunnel mode, WireGuard
Remote accessOne user device and a central gatewayTLS VPN client, IKEv2, WireGuard

In a site-to-site VPN, users do nothing: the gateways keep the tunnel up and route traffic between the subnets, which must not overlap. Plan addressing with the subnetting guide before connecting sites.

Full tunnelling sends all traffic from a remote device through the VPN, so company security filtering applies to everything. Split tunnelling sends only traffic for internal networks through the tunnel and lets the rest go straight to the internet. Split tunnelling reduces load on the gateway but means internet traffic from that device bypasses company controls. In WireGuard the choice is simply AllowedIPs = 0.0.0.0/0, ::/0 for full tunnel or a list of internal prefixes for split tunnel.

What a VPN does not protect against

  • Malware or an attacker already on the device: the VPN carries their traffic too.
  • Phishing: a fake login page works the same inside a tunnel, as the phishing guide explains.
  • Tracking by websites through accounts, cookies and browser fingerprints.
  • The VPN operator itself, which can see traffic leaving the tunnel; with a commercial VPN you are moving trust, not removing it.
  • Unencrypted traffic after the tunnel ends: HTTP beyond the gateway is still readable.
  • DNS leaks, where name lookups bypass the tunnel because of a misconfigured client.

A VPN also places a remote device on the internal network, so a compromised laptop gains wide access. Many organisations now pair or replace VPNs with zero trust network access, which checks the user and device for each application instead of granting network-level access. In India, CERT-In directions issued in 2022 also place record-keeping duties on VPN service providers.

Setting up and checking a VPN, step by step

  1. Decide the model: site-to-site, remote access, or both.
  2. Pick non-overlapping tunnel and LAN subnets.
  3. Choose authentication: certificates or key pairs rather than shared passwords where possible.
  4. Open only the required ports on the gateway firewall, for example UDP 500 and 4500 for IPsec or UDP 51820 for WireGuard.
  5. Decide between full and split tunnelling and document why.
  6. Bring the tunnel up and test with ping to an internal address and a trace to confirm the path.

Practical exercise: build a WireGuard lab tunnel

Use two Linux virtual machines you own on the same host or LAN.

  1. Install WireGuard tools on both and generate a key pair on each.
  2. Give the tunnel the subnet 10.8.0.0/24, with .1 on the server and .2 on the client.
  3. Write wg0.conf on each side, open UDP 51820 on the server firewall and bring both up.
  4. Ping 10.8.0.1 from the client and confirm wg show reports a recent handshake.
  5. Capture traffic on the physical interface with tcpdump -i eth0 udp port 51820 and confirm the payload is unreadable.

You are done when the tunnel survives a reboot of either machine and you can explain each line of the configuration. VPN design for multi-site networks is part of the Advanced Networking course, and the gateway policy builds on firewall rules.

Common mistakes

  • Connecting two sites that use the same private subnet, so routing between them cannot work.
  • Using split tunnelling without considering that internet traffic bypasses company security controls.
  • Copying a WireGuard private key to the other peer instead of the public key.
  • Assuming a VPN makes a user anonymous or protects against phishing and malware.
  • Opening every port to the VPN gateway instead of only the protocols the tunnel uses.

Frequently asked questions

Is WireGuard more secure than IPsec?

Both are secure when configured correctly. WireGuard has a much smaller code base and fewer options, which makes it easier to review and harder to misconfigure. IPsec offers wider vendor support and more authentication options for enterprise use.

Why does my VPN work at home but not on hotel Wi-Fi?

The hotel network may block the UDP ports your VPN uses. A TLS VPN on TCP port 443 usually gets through because that port is needed for web browsing.

Does a VPN slow down my connection?

Usually a little, because of the extra headers, encryption work and the longer path through the gateway. The effect depends on gateway capacity and distance.

Want to Learn This Properly?

The Advanced Networking & Cybersecurity Mastery (Online) turns guides like this into practised skills, with projects on real websites.

WhatsApp Call Us Now

Free counselling

Send an Enquiry

Tell us which course you are interested in. We reply by email or phone with the next batch and how to enrol.

Protected by reCAPTCHA. Google Privacy and Terms apply.

We reply by email or phone within working hours. No spam.