SkillPilotAI

wg and wg-quick

WireGuard is a fast, modern VPN built on public keys between peers. wg shows and changes the WireGuard settings of an interface; wg-quick brings a whole tunnel (address, routes, MTU, DNS, hooks) up or down from one config file.

119.1 wg and wg-quick: what and where

ToolHandlesWhere
wgKeys, peers, AllowedIPs, endpointsLinux (wireguard-tools), macOS, Windows wg.exe
wg-quickInterface + IP + MTU + routes + DNS + hooks from /etc/wireguard/wg0.confLinux, macOS (bash wrapper)
wireguard.exeTunnel services, GUI, logsWindows
  • The kernel module is built in since Linux 5.6; macOS via brew or the App Store app.

119.2 How it works: handshake and cryptokey routing

WireGuard handshake and data
  1. 1.Laptop and server each have a private key; they exchanged only public keys

  2. 2.Laptop 192.168.139.20 sends handshake initiation to server UDP 51820

  3. 3.Server verifies the laptop's public key

  4. 4.Ping: outer 192.168.139.20 -> .10 UDP 51820, inner 10.0.0.2 -> 10.0.0.1, encrypted

AllowedIPs = routing table + filter
  1. 1.Server config: laptop peer AllowedIPs 10.0.0.2/32

  2. 2.Reply to 10.0.0.2: wg picks the laptop's key and endpoint (routing)

  3. 3.Laptop sends with inner source 10.0.0.9

  4. 4.10.0.0.9 not in the laptop's AllowedIPs: server drops it (filter)

One IP can belong to only one peer

119.3 Keys: genkey, pubkey, genpsk (8)

Terminal
wg --helpumask 077wg genkey | tee private.key | wg pubkey > public.keyls -l; cat public.keywg genpsk > psk.key
Output
Usage: wg <cmd> [<args>]  show, showconf, set  setconf: Applies a configuration file  addconf: Appends a configuration file  syncconf: Synchronizes a configuration file  genkey, genpsk, pubkey-rw------- 1 root root 45 private.key-rw------- 1 root root 45 public.keyQ4F6xDftvKt5E1f3ELPFpJvxPbkl0mI2o4FBuQH7k14=$ wc -c psk.key45 psk.key
  • umask 077 so key files are readable only by root.
  • Only the public key is given to the peer; genpsk makes an optional preshared key that both peers share.

119.4 Config file keys (9)

Terminal
grep -v Key /etc/wireguard/wg0-wg.conf
Output
[Interface] [Peer]Endpoint = 192.168.139.10:51820AllowedIPs = 10.0.0.0/24PersistentKeepalive = 25
Output
[Interface]PrivateKey = base64      # required, secretListenPort = 51820       # unset = randomFwMark = 0x1234          # mark outgoing packets[Peer]PublicKey = base64       # requiredPresharedKey = base64    # optional (genpsk)AllowedIPs = 10.0.0.2/32, 10.0.1.0/24   # 0.0.0.0/0, ::/0 = all trafficEndpoint = host:51820    # auto-updates from authenticated packets (roaming)PersistentKeepalive = 25 # 0/off = default

119.5 Bringing the tunnel up

wg-quick up wg0 would create a kernel WireGuard device (loading a module into the Docker VM), so the lab ran the same steps by hand with userspace wireguard-go.

Terminal
wireguard-go wg0wg setconf wg0 /etc/wireguard/wg0-wg.confip address add 10.0.0.2/24 dev wg0ip link set wg0 mtu 1420 upping -c 3 10.0.0.1
Output
$ ls /var/run/wireguard/wg0.sock$ ip route show dev wg010.0.0.0/24 proto kernel scope link src 10.0.0.2$ ping -c 3 10.0.0.164 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=32.9 ms64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=1.27 ms64 bytes from 10.0.0.1: icmp_seq=3 ttl=64 time=1.41 ms3 packets transmitted, 3 received, 0% packet loss
What wg-quick up does
  1. 1.ip link add wg0 type wireguard

  2. 2.wg setconf wg0 (stripped config)

  3. 3.ip address add (Address =)

  4. 4.Set MTU and bring the link up

  5. 5.Add routes for AllowedIPs, set DNS

  6. 6.Run PreUp / PostUp hooks

119.6 wg-quick commands (6) and extra keys (13)

Terminal
wg-quick up wg0                 # /etc/wireguard/wg0.confwg-quick up /path/wgnet0.conf   # from a filewg-quick down wg0               # remove interfacewg-quick save wg0               # live config to filewg-quick strip wg0              # only keys wg understandswg syncconf wg0 <(wg-quick strip wg0)   # reload, no restart
Output
Address = 10.0.0.2/24          # can repeatDNS = 10.0.0.1, corp.example.com   # via resolvconf (server + search domain)MTU = 1420                     # instead of autoTable = 1234 | off | auto      # routing table for routesPreUp = echo starting %iPostUp = iptables -A FORWARD -i %i -j ACCEPTPreDown = iptables -D FORWARD -i %i -j ACCEPTPostDown = logger "%i down"    # %i = interface nameSaveConfig = true              # overwrites the file on down
  • These [Interface] keys are understood only by wg-quick, not by wg; strip removes them.
  • SaveConfig = true can overwrite your hand edits when the tunnel goes down.

119.7 wg show (19)

Terminal
wg show wg0
Output
interface: wg0  public key: Q4F6xDftvKt5E1f3ELPFpJvxPbkl0mI2o4FBuQH7k14=  private key: (hidden)  listening port: 51820 peer: x+dwkEpSFiy8VC9uxTW/jIjglvMmMoqZmZcKPEZD7XM=  preshared key: (hidden)  endpoint: 192.168.139.20:49135  allowed ips: 10.0.0.2/32  latest handshake: 8 seconds ago  transfer: 564 B received, 476 B sent
  • latest handshake + growing transfer = tunnel alive; the private key is shown as (hidden).
Terminal
wg show interfaceswg show wg0 endpointswg show wg0 latest-handshakeswg show wg0 transferwg show wg0 dump | cut -c1-60
Output
wg0x+dwkEpSFiy8VC9uxTW/jIjglvMmMoqZmZcKPEZD7XM=	192.168.139.20:49135x+dwkEpSFiy8VC9uxTW/jIjglvMmMoqZmZcKPEZD7XM=	1794191708x+dwkEpSFiy8VC9uxTW/jIjglvMmMoqZmZcKPEZD7XM=	596	476<private key removed>	Q4F6xDftvKt5E1fx+dwkEpSFiy8VC9uxTW/jIjglvMmMoqZmZcKPEZD7XM=	<psk removed>
  • Field output is tab-separated for scripts; latest-handshakes is in Unix seconds, 0 = never.
  • Plain wg = wg show all; wg showconf wg0 prints the live config in file format.

119.8 wg set (16): live changes

Terminal
wg set wg0 peer $NEW allowed-ips 10.0.0.3/32wg show wg0 allowed-ipswg set wg0 peer $NEW removewg show wg0 peers | wc -lwg set wg0 fwmark 0x1234wg set wg0 fwmark off
Output
KXiuCK1EAspzh0VC9/CpgiGMWUskvKJq0w/iR5ZwvA0=	10.0.0.3/32x+dwkEpSFiy8VC9uxTW/jIjglvMmMoqZmZcKPEZD7XM=	10.0.0.2/321$ wg show wg0 fwmark0x1234$ wg show wg0 fwmarkoff
Output
listen-port 51820 | 0 (random)private-key /etc/wireguard/private.key | <(echo KEY) | /dev/null (remove)peer KEY ... | peer KEY removepreshared-key psk.key | /dev/nullendpoint 203.0.113.5:51820persistent-keepalive 25 | offallowed-ips 10.0.0.2/32,192.168.10.0/24 | "" (remove all)

119.9 setconf, addconf, syncconf

Terminal
wg addconf wg0 /etc/wireguard/extra-peer.confwg show wg0 allowed-ips | cut -f2wg syncconf wg0 /etc/wireguard/wg0-wg.confwg show wg0 allowed-ips | cut -f2
Output
10.0.0.2/3210.0.0.4/32$ wg syncconf wg0 /etc/wireguard/wg0-wg.conf$ wg show wg0 allowed-ips | cut -f210.0.0.2/32
CommandEffect
wg setconfReplace the whole config
wg addconfAppend peers from a file
wg syncconfApply only differences; running sessions not disturbed

119.10 Environment variables and debugging (8)

Terminal
WG_HIDE_KEYS=never wg show wg0 | head -3 | cut -c1-30wg show wg0 | head -3WG_COLOR_MODE=always wg show wg0 | head -1 | cat -vWG_COLOR_MODE=never wg show wg0 | head -1 | cat -v
Output
interface: wg0  public key: Q4F6xDftvKt5E1f3  private key: <private key removed>interface: wg0  public key: Q4F6xDftvKt5E1f3ELPFpJvxPbkl0mI2o4FBuQH7k14=  private key: (hidden)^[[0m^[[32m^[[1minterface^[[0m: ^[[32mwg0^[[0minterface: wg0
  • WG_HIDE_KEYS=never prints the private key (default: hidden).
  • WG_COLOR_MODE=always|never|auto; WG_ENDPOINT_RESOLUTION_RETRIES (default 15, or infinity).
  • Debug: dmesg with dynamic debug (kernel module), LOG_LEVEL=verbose for wireguard-go.

119.11 Windows: wireguard.exe and wg.exe (9)

Terminal
where wireguard wgsc query WireGuardTunnel$wg0
Output
INFO: Could not find files for the given pattern(s).[SC] EnumQueryServicesStatus:OpenService FAILED 1060:The specified service does not exist as an installed service.
Output
wireguard /installtunnelservice C:\wg\wg0.conf   service + tunnel upwireguard /uninstalltunnelservice wg0wireguard /installmanagerservice     GUI / traywireguard /uninstallmanagerservicewireguard /dumplog > C:\wg\log.txt   (/tail to follow)wg show | showconf | set | setconf ...   same as Linux

Not installed on the office PC, so only a read-only check ran. After install, use an elevated prompt for the tunnel service commands.

119.12 Common mistakes

  • Peer listed but no latest handshake and transfer 0: check public keys on both sides, Endpoint IP/port and UDP 51820 in the firewall.
  • Thinking AllowedIPs is only a filter: it also routes; the same range cannot go to two peers.
  • Sharing the private key instead of the public key.
  • Putting wg-quick keys (Address, DNS) into a file used with wg setconf: use wg-quick strip.

119.13 Interview tips

  • wg = only WireGuard settings; wg-quick = interface + IP + MTU + routes + DNS + hooks (production uses wg-quick or its systemd service).
  • Cryptokey routing: AllowedIPs decides the peer for outgoing packets and checks the source of incoming ones.
  • Tunnel health: latest handshake and transfer in wg show.
  • Reload without restart: wg syncconf wg0 <(wg-quick strip wg0).