WireGuard VPN Quickstart
Before you begin
- Applies to:One WireGuard client connecting to a Linux server’s tunnel address.
- Where to start:Start on the Linux server. Use a separate client to check tunnel connectivity.
- What you need:Have administrator access on both endpoints, the server endpoint, an approved UDP port, and a private tunnel subnet that overlaps neither network.
Guide tools
This quickstart connects one remote client to a Linux server with WireGuard. It routes traffic only to the server’s tunnel address; it does not route the client’s traffic to the server’s LAN or through the server as an internet exit node. Choose a private tunnel subnet that does not overlap with either endpoint’s existing networks.
The examples use 10.77.0.0/24, server address 10.77.0.1, client address 10.77.0.2, and UDP port 51820. Replace them if they conflict with your network plan.
Install WireGuard Tools and Generate Server Keys
Server SetupInstall the distribution’s WireGuard package, then create a private key directory and generate the server key pair as root. These commands refuse to overwrite existing keys; if a key file already exists, inspect and back it up rather than regenerating it. Keep the private key and never send it to a client.
sudo apt updatesudo apt install -y wireguardsudo dnf install -y wireguard-toolssudo install -d -m 0700 /etc/wireguardsudo sh -eu -c 'for path in /etc/wireguard/server.key /etc/wireguard/server.pub; do if [ -e "$path" ] || [ -L "$path" ]; then printf "Refusing to overwrite existing key file: %s\n" "$path" >&2 exit 1 fidoneumask 077set -Cwg genkey > /etc/wireguard/server.keywg pubkey < /etc/wireguard/server.key > /etc/wireguard/server.pub' && sudo cat /etc/wireguard/server.pub❯ Illustrative Output (Results Vary)
u3k...server-public-key...X0=The public key can be shared with the client. /etc/wireguard/server.key stays readable only by root.
Keep the Client Private Key on the Client Device
Client SetupGenerate the client key pair on the device that will connect. These commands refuse to overwrite existing files in this directory; inspect any existing keys before continuing. Send only the public key to the server administrator. The Windows and mobile WireGuard apps can generate a key pair when you add a tunnel; use the Linux commands below for a Linux client.
mkdir -p "$HOME/wireguard-client"cd "$HOME/wireguard-client" || exitif [ -e client.key ] || [ -L client.key ] || [ -e client.pub ] || [ -L client.pub ]; then printf 'Existing client.key or client.pub found; inspect the files before continuing.\n' >&2 falseelse (umask 077; set -C; wg genkey > client.key && wg pubkey < client.key > client.pub) && chmod 0600 client.key client.pub && cat client.pubfi1. Create a new, empty tunnel in the WireGuard app.2. Let the app generate its private and public key pair.3. Copy or export only the public key for the server configuration.4. Keep the private key inside the app; do not send it to the server.❯ Illustrative Output (Results Vary)
Y7s...client-public-key...rE=Keep the Linux client.key file private. The umask 077 command gives the generated files owner-only permissions.
Add the Peer Keys and Tunnel Addresses
ConfigurationOn the server, set CLIENT_PUBLIC_KEY to the 44-character public key received from the client. The command validates its format, refuses to overwrite an existing interface config, and reads the server private key directly into the root-owned config without printing it. On a Linux client, create the file using its private key, the server public key, and the server’s DNS name, IPv4 address, or bracketed IPv6 address. The client config command also refuses to overwrite an existing file. In the Windows or mobile app, keep the generated private key in the app and add the same address and peer settings to that tunnel.
CLIENT_PUBLIC_KEY="paste-client-public-key-here"if [[ ! "$CLIENT_PUBLIC_KEY" =~ ^[A-Za-z0-9+/]{43}=$ ]]; then printf 'Client public key must be a 44-character WireGuard base64 key.\n' >&2 falseelsesudo env CLIENT_PUBLIC_KEY="$CLIENT_PUBLIC_KEY" sh -eu -c ' if [ -e /etc/wireguard/wg0.conf ] || [ -L /etc/wireguard/wg0.conf ]; then printf "Refusing to overwrite /etc/wireguard/wg0.conf; inspect the existing interface first.\n" >&2 exit 1 fi server_key=$(cat /etc/wireguard/server.key) umask 077 set -C cat > /etc/wireguard/wg0.conf <<EOF[Interface]Address = 10.77.0.1/24ListenPort = 51820PrivateKey = $server_key
[Peer]PublicKey = $CLIENT_PUBLIC_KEYAllowedIPs = 10.77.0.2/32EOF'fiOn a Linux client, install the WireGuard tools using the same package instructions as Step 1, then create client.conf in the directory from Step 2. Replace the example values first. In the Windows/mobile app, set the interface address to 10.77.0.2/32, and set the peer to the server public key, your server endpoint on UDP 51820, allowed IP 10.77.0.1/32, and persistent keepalive 25 seconds:
SERVER_PUBLIC_KEY="paste-server-public-key-here"SERVER_ENDPOINT="vpn.example.net"if [[ ! "$SERVER_PUBLIC_KEY" =~ ^[A-Za-z0-9+/]{43}=$ ]] || { [[ ! "$SERVER_ENDPOINT" =~ ^[A-Za-z0-9.-]+$ ]] && [[ ! "$SERVER_ENDPOINT" =~ ^\[[0-9A-Fa-f]*:[0-9A-Fa-f:]*\]$ ]]; }; then printf 'Enter a valid server public key and a DNS name, IPv4 address, or bracketed IPv6 endpoint.\n' >&2 falseelif [ -e client.conf ] || [ -L client.conf ]; then printf 'client.conf already exists; inspect it before continuing.\n' >&2 falseelif [ ! -s client.key ]; then printf 'client.key is missing or empty; generate the client key pair first.\n' >&2 falseelseCLIENT_PRIVATE_KEY="$(cat client.key)"if [[ ! "$CLIENT_PRIVATE_KEY" =~ ^[A-Za-z0-9+/]{43}=$ ]]; then printf 'client.key does not contain a valid WireGuard private key; inspect it before continuing.\n' >&2 unset CLIENT_PRIVATE_KEY falseelif (umask 077; set -C; cat > client.conf <<EOF[Interface]Address = 10.77.0.2/32PrivateKey = $CLIENT_PRIVATE_KEY
[Peer]PublicKey = $SERVER_PUBLIC_KEYEndpoint = $SERVER_ENDPOINT:51820AllowedIPs = 10.77.0.1/32PersistentKeepalive = 25EOF) && chmod 0600 client.conf; then unset CLIENT_PRIVATE_KEYelse unset CLIENT_PRIVATE_KEY falsefifi❯ Illustrative Output (Results Vary)
The server peer allows only 10.77.0.2/32, and the client routes only 10.77.0.1/32 through the tunnel. Keep the private keys out of shared notes, tickets, and source control.
Permit UDP 51820 and Enable the WireGuard Interface
Firewall and ServiceIf the server is behind a router, reserve its LAN address and forward UDP port 51820 to it. Allow the same port in the server firewall, then start the wg-quick@wg0 service. These firewall and service commands change persistent host configuration. The UFW or firewalld rule opens only WireGuard’s listener; it does not expose the tunnel subnet as a routed LAN.
sudo ufw allow 51820/udpsudo ufw statussudo firewall-cmd --permanent --add-port=51820/udpsudo firewall-cmd --reloadsudo firewall-cmd --list-portssudo systemctl enable --now wg-quick@wg0sudo systemctl status wg-quick@wg0 --no-pagersudo wg show wg0❯ Illustrative Output (Results Vary)
interface: wg0 public key: u3k...X0= listening port: 51820Forwarding is not needed for this server-only tunnel. Add separate routing and firewall rules only if you intentionally want clients to reach the server’s LAN.
Bring Up the Client and Verify the Tunnel
Connection TestImport the completed configuration into the Windows/mobile app and activate the tunnel. On a Linux client, install the config under /etc/wireguard and start it with wg-quick. A recent handshake confirms the peers exchanged authenticated packets; test a service on 10.77.0.1 to verify the traffic your deployment needs.
# Linux client only; run from the directory containing client.conf.if sudo test -e /etc/wireguard/wg-client.conf || sudo test -L /etc/wireguard/wg-client.conf; then printf 'Existing /etc/wireguard/wg-client.conf found; inspect it before continuing.\n' >&2 falseelse sudo install -m 0600 client.conf /etc/wireguard/wg-client.conf && sudo systemctl enable --now wg-quick@wg-client && sudo wg show wg-clientfi❯ Illustrative Output (Results Vary)
peer: u3k...server-public-key...X0= endpoint: 203.0.113.20:51820 allowed ips: 10.77.0.1/32 latest handshake: 8 seconds ago transfer: 1.24 KiB received, 1.08 KiB sentReferences
Your feedback keeps these production procedures accurate and battle-tested.