This episode thoroughly covers WireGuard key management and configuration contents: wg genkey, wg genpsk, and every field in the [Interface] and [Peer] sections such as Address, ListenPort, AllowedIPs, Endpoint, and PersistentKeepalive.

In episode 3 you successfully brought up your first wg0 interface. Now it is time to understand every part you have been writing: the keys that underpin WireGuard and the meaning of each field in [Interface] and [Peer]. This is the foundation for correct and secure configuration.
Episode 4 dissects three areas: key management (including pre-shared keys), the [Interface] section that controls the interface's identity and behavior, and the [Peer] section that governs each inbound and outbound connection. By the end of the episode, your two VMs will connect to each other for the first time.
The private key is the source of an interface's identity, and the public key is derived from it. Always generate both at the same time and store the private key file with strict permissions:
umask 077
wg genkey | tee privatekey | wg pubkey > publickeyumask 077 ensures the key file is not readable by other users. The private key lives in the privatekey file, and the public key to share lives in publickey. Distribute your public key to all peers you will talk to.
WireGuard can add an extra layer of security called a pre-shared key per peer. Its function is to strengthen the session key against scenarios where the private key leaks, at the cost of both ends having to share the same secret:
wg genpskPaste the result into PresharedKey in the [Peer] section on both sides. Note: this pre-shared key is per peer pair, not per interface.
Rotation is done by changing the interface's private key and updating the public key on the peer side. The following command swaps the private key live without taking the interface down:
sudo wg set wg0 private-key /etc/wireguard/privatekeyFull rotation along with a graceful rotation strategy will be covered thoroughly in episode 12.
Each WireGuard interface is defined in the [Interface] section. The most common fields:
wg-quick up runs.wg-quick defaults to 51820.wg-quick uses; Table = off disables automatic route addition.[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = pS9S6eGJY8tVb3DkG5XwS9zHpN9lQfBcE5S6o+3Qn5k=
Table = auto
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADEThe PostUp above is used to enable NAT masquerading as soon as the interface comes up, a pattern we will reuse many times in episodes 10 and 14.
Each connection is defined in a [Peer] section:
Time to connect the server and client. On the server, add the client peer; on the client, add the server peer:
sudo wg set wg0 peer <PUBLIK_CLIENT> allowed-ips 10.0.0.2/32On the client, the configuration is built from the same fields:
[Interface]
Address = 10.0.0.2/24
PrivateKey = <kunci privat client>
[Peer]
PublicKey = <kunci publik server>
AllowedIPs = 10.0.0.0/24
Endpoint = 203.0.113.5:51820AllowedIPs = 10.0.0.0/24 on the client side means all addresses in the tunnel subnet belong to the server. Once both sides have each other as peers, test the connection:
ping -c 3 10.0.0.1
sudo wg showIf wg show shows the handshake column populated, your two VMs are officially connected over WireGuard.
Episode 4 completed key management and interface and peer configuration. You can now generate keys, add a pre-shared key, understand every field in [Interface] and [Peer], and successfully connect two VMs.
Key takeaways:
wg genkey generates a private key; wg pubkey derives the public key.wg genpsk creates an optional pre-shared key per peer pair.[Interface] controls the identity, port, and up/down hooks of the interface.[Peer] controls the destination public key, AllowedIPs, endpoint, and keepalive.In episode 5 we dissect handshake and lifecycle — how Noise IKpsk2 progresses from handshake initiation to flowing encrypted data, the anti-DoS cookie mechanism, automatic rekeying, and the session lifecycle from empty to data-received.