WireGuard commands cheat sheet
Installation
-
sudo apt install wireguardInstalls the WireGuard tools, including wg and wg-quick, on Ubuntu and Debian. The core component is already built into the kernel of current releases.
-
sudo dnf install wireguard-toolsInstalls the WireGuard tools on Fedora and on RHEL-family distributions, such as AlmaLinux and Rocky Linux. Older releases of RHEL-family distributions don't include the kernel module and need extra repositories, as described in the installation instructions on the WireGuard website.
-
sudo modprobe wireguard && lsmod | grep wireguardLoads the WireGuard module and checks that it's present in the kernel. It's usually also loaded automatically when you bring up the interface.
Keys
-
sudo -i cd /etc/wireguard umask 077Switches to the root user and the configuration folder, and sets umask 077 so that the files you create are readable only by root. Run the rest of the commands in this section in the same window.
-
wg genkey | tee server.key | wg pubkey > server.pubCreates a private key for the server and saves the public key derived from it next to it. The private key stays on the server only, and you copy the public key into the client files.
-
wg genkey | tee client1.key | wg pubkey > client1.pubCreates a key pair for a client. Create a separate key pair for each device, and don't share keys between devices.
-
wg genpsk > client1.pskCreates a preshared key, an additional shared key that is set on both the server and the client. The key adds a layer of symmetric encryption, designed to protect against future quantum computers as well. Using it is optional.
-
wg pubkey < server.keyShows the public key that matches an existing private key, for example when the .pub file is lost.
-
cat server.pubShows the server's public key, so you can copy it into the PublicKey line in the client file.
Server configuration file
-
[Interface] Address = 10.8.0.1/24 ListenPort = 51820 PrivateKey = SERVER_PRIVATE_KEY [Peer] PublicKey = CLIENT1_PUBLIC_KEY PresharedKey = CLIENT1_PRESHARED_KEY AllowedIPs = 10.8.0.2/32A basic server file, saved as /etc/wireguard/wg0.conf. The file name sets the interface name, wg0. Replace SERVER_PRIVATE_KEY with the contents of server.key, and the client keys with the contents of client1.pub and client1.psk. Add a separate [Peer] section for each client.
-
Address = 10.8.0.1/24The server's address inside the tunnel and the VPN network it belongs to. Choose a private range that isn't used on the networks your clients connect from.
-
ListenPort = 51820The UDP port the server listens on. 51820 is only a convention, and you can choose a different port. Without this line, a random port is chosen every time the interface starts, so always set it on the server.
-
AllowedIPs = 10.8.0.2/32In the [Peer] section on the server: the address this client is allowed to use inside the tunnel. Each client gets a different address, usually with /32.
-
PresharedKey = CLIENT1_PRESHARED_KEYThe client's preshared key. The same value is also set in the [Peer] section of the client file.
-
SaveConfig = trueSaves the current state of the interface to the file every time it stops. With this setting on, changes written to the file while the interface is running are overwritten the next time it stops, so it's better not to use it and to edit the file manually.
-
sudo chmod 600 /etc/wireguard/wg0.confMakes the file readable by root only, because it contains the server's private key.
Routing and NAT on the server
-
echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/70-wireguard-routing.confEnables packet forwarding between interfaces (IP forwarding) permanently. Without this setting, clients reach the server, but they can't reach the internet through it.
-
echo "net.ipv6.conf.all.forwarding = 1" | sudo tee -a /etc/sysctl.d/70-wireguard-routing.confAdds the same setting for IPv6, when the tunnel also carries IPv6 traffic. On a server that gets its IPv6 address automatically from Router Advertisements, this setting stops it from receiving the address. In that case, also add net.ipv6.conf.eth0.accept_ra = 2 to the file, with the server's interface name.
-
sudo sysctl --systemLoads the sysctl configuration files and applies the change immediately, without a reboot.
-
ip route show defaultShows the network interface through which traffic leaves for the internet, for example eth0 or ens18. Use this name in the NAT rules below.
-
PostUp = iptables -I FORWARD -i %i -j ACCEPT; iptables -I FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADELines for the [Interface] section of the server file: when the interface comes up, they allow forwarding of tunnel traffic and perform NAT (MASQUERADE) for internet access, and when it stops, they delete the rules. %i is replaced with the interface name. On a server whose firewall is managed with firewalld, use the firewalld commands instead of these lines, and don't mix the two methods.
-
PostUp = nft add table inet wg-nat; nft add chain inet wg-nat postrouting '{ type nat hook postrouting priority srcnat; }'; nft add rule inet wg-nat postrouting ip saddr 10.8.0.0/24 oifname eth0 masquerade PostDown = nft delete table inet wg-natThe same NAT with nftables, in a separate table that is deleted entirely when the interface stops. When the forward chain on the server blocks traffic by default, you also need to allow traffic to and from wg0 in it.
Firewall
-
sudo ufw allow 51820/udpOpens the WireGuard UDP port in UFW. WireGuard works over UDP only, so opening the port for TCP won't help.
-
sudo ufw route allow in on wg0 out on eth0Allows UFW to forward traffic from the tunnel to the outgoing interface, when clients browse the internet through the server. The NAT itself is set in the PostUp and PostDown lines.
-
sudo firewall-cmd --permanent --zone=public --add-port=51820/udpOpens the WireGuard UDP port in firewalld, in the public zone.
-
sudo firewall-cmd --permanent --zone=internal --add-interface=wg0Adds the wg0 interface to the internal zone, so that the rules for VPN clients are separate from the rules of the public zone.
-
sudo firewall-cmd --permanent --zone=public --add-masqueradeEnables NAT (masquerading) for traffic leaving through the public zone, so that clients go out to the internet with the server's address.
-
sudo firewall-cmd --reloadLoads the permanent firewalld configuration and applies the changes.
Starting and stopping
-
sudo wg-quick up wg0Brings up the interface according to /etc/wireguard/wg0.conf: sets the addresses, keys, and routes, and runs the PostUp lines.
-
sudo wg-quick down wg0Brings down the interface, deletes the routes that were added, and runs the PostDown lines.
-
sudo systemctl enable --now wg-quick@wg0Runs the tunnel as a systemd service, both now and every time the server boots. If you already brought up the interface manually with wg-quick up, bring it down first.
-
sudo systemctl status wg-quick@wg0Shows whether the service is running, along with its latest messages, including errors in the configuration file.
-
sudo systemctl reload wg-quick@wg0Reloads the configuration file without dropping existing connections. After changing the Address or PostUp lines, run restart instead of reload.
-
sudo journalctl -u wg-quick@wg0 -n 50 --no-pagerShows the last 50 messages of the service, for example when it fails to start.
Tunnel status
-
sudo wgShows all WireGuard interfaces and their clients: the last address (endpoint), the allowed IPs, the time of the latest handshake, and the amount of traffic.
-
sudo wg show wg0 latest-handshakesShows the time of the latest handshake for each client, as a Unix timestamp. A value of 0 means there has been no handshake with the client since the interface came up, and a working handshake renews every few minutes while there's traffic.
-
sudo wg show wg0 transferShows how many bytes were received and sent for each client.
-
sudo wg show wg0 endpointsShows the address and port each client last connected from.
-
sudo wg showconf wg0Shows the active configuration of the interface in configuration file format, including clients added with wg set.
-
sudo wg show all dumpShows tab-separated output, with one line per interface and per client, handy for scripts and monitoring.
-
watch -n 2 sudo wgRefreshes the display every two seconds, so you can see in real time whether the handshake updates when the client connects.
Adding and removing clients
-
sudo wg set wg0 peer CLIENT2_PUBLIC_KEY allowed-ips 10.8.0.3/32Adds a client to the running interface without disconnecting other clients. The change is temporary: to keep it, also add the client as a [Peer] section in wg0.conf.
-
sudo wg set wg0 peer CLIENT2_PUBLIC_KEY preshared-key /etc/wireguard/client2.pskSets a preshared key for the client from a file. wg set accepts the key only as a file path, not as text on the command line.
-
sudo wg set wg0 peer CLIENT2_PUBLIC_KEY allowed-ips 10.8.0.3/32,192.168.50.0/24Replaces the client's list of allowed IPs, for example to allow access to the network behind it. The new list replaces the previous one entirely.
-
sudo wg set wg0 peer CLIENT2_PUBLIC_KEY removeRemoves a client from the running interface, and its key stops working immediately. To make the removal permanent, also delete its section from the file.
-
sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'Applies the changes written to the file, such as a new or deleted client, to the running interface without dropping existing connections. wg-quick strip removes the lines that wg doesn't recognize, such as Address and PostUp.
-
sudo wg-quick save wg0Rewrites the file from the current state of the interface, including clients added with wg set. Back up the file before you use it.
Client configuration file
-
[Interface] PrivateKey = CLIENT1_PRIVATE_KEY Address = 10.8.0.2/32 DNS = 1.1.1.1 [Peer] PublicKey = SERVER_PUBLIC_KEY PresharedKey = CLIENT1_PRESHARED_KEY Endpoint = 203.0.113.10:51820 AllowedIPs = 0.0.0.0/0, ::/0 PersistentKeepalive = 25A client file that sends all traffic through the server (full tunnel). Replace 203.0.113.10 with the server's address or a domain name that points to it. Save the file with a name like client1.conf.
-
AllowedIPs = 10.8.0.0/24Split tunnel: only traffic to the VPN network goes through the tunnel, and regular browsing goes out directly from the device. Add any other networks that should go through the server here, separated by commas.
-
AllowedIPs = 0.0.0.0/0, ::/0Full tunnel: all of the device's traffic, over IPv4 and IPv6, goes through the server. Even when the server has no IPv6, including ::/0 keeps IPv6 traffic from leaking outside the tunnel.
-
Endpoint = vpn.example.com:51820The server's address and port. You can use a domain name instead of an IP address. The server itself doesn't need an Endpoint for its clients, because it learns their address from the connection.
-
PersistentKeepalive = 25Sends a small packet every 25 seconds so that the router or NAT on the client side doesn't forget the connection. It's needed mainly when the client is behind NAT and must receive traffic even when it isn't sending any. Without such a need, leave this setting off.
-
DNS = 1.1.1.1The DNS server the device uses while the tunnel is active. On Linux, wg-quick sets it with the resolvconf command, so that command must be installed. In the WireGuard apps for Windows, macOS, and phones, the setting works directly.
-
PostUp = resolvectl dns %i 1.1.1.1; resolvectl domain %i ~.An alternative to the DNS line on Linux with systemd-resolved, when the resolvconf command isn't installed. The ~. part sends all DNS queries through the tunnel.
-
MTU = 1280Sets the maximum packet size in the tunnel manually. wg-quick calculates a value automatically, so lower it only when there are signs of an MTU problem, such as websites that load partially or connections that hang.
-
sudo cp client1.conf /etc/wireguard/ sudo wg-quick up client1Connects a Linux computer to the server: copies the client file to the configuration folder and brings up the client1 interface. To disconnect, run sudo wg-quick down client1.
QR code for phones
-
sudo apt install qrencodeInstalls qrencode, which turns the client file into a QR code.
-
qrencode -t ansiutf8 < client1.confShows the client file as a QR code directly in the terminal window. In the WireGuard app for Android and iPhone, choose to add a tunnel from a QR code and scan the screen.
-
qrencode -o client1.png < client1.confSaves the QR code as a PNG file. The code contains the client's private key, so delete the file after scanning it.
Testing and troubleshooting
-
sudo tcpdump -ni eth0 udp port 51820Shows on the server whether packets from the client reach the port. If no packets arrive, the port is blocked along the way, in the server's firewall or on the client's network, or the Endpoint in the client file is wrong.
-
ping -c 3 10.8.0.1Checks from the client that the tunnel works, with a ping to the server's address inside the tunnel.
-
ip addr show wg0Shows the interface, its address, and its MTU.
-
ip route get 1.1.1.1Shows which interface traffic to a given address will leave through. With a full tunnel, the answer on the client should be the WireGuard interface.
-
cat /proc/sys/net/ipv4/ip_forwardShows 1 when packet forwarding is enabled on the server. A value of 0 explains a tunnel that comes up but doesn't give access to the internet.
-
sudo iptables -t nat -L POSTROUTING -n -vShows the NAT rules and their packet counters. A counter that stays at 0 while the client browses points to a rule with a wrong interface name or address range.
-
echo "module wireguard +p" | sudo tee /sys/kernel/debug/dynamic_debug/control sudo dmesg -wTTurns on WireGuard debug messages in the kernel log and shows them in real time, for example messages about a failed handshake. To turn them off, run the same command with -p instead of +p. On servers with Secure Boot, this option may be blocked.
No matching commands and settings.
What WireGuard is and how it works
WireGuard is a simple, modern VPN whose core component runs inside the Linux kernel, and it is also available for Windows, macOS, and phones. It carries encrypted traffic over UDP only, and each side is identified by a key pair, with no usernames or passwords. Each client is set up as a peer, with a public key and a list of allowed addresses (AllowedIPs).
This list is the basis of what the WireGuard documentation calls Cryptokey Routing, routing by key: when sending, it determines which peer receives a packet for a given address, and when receiving, it determines which addresses a peer is allowed to send from. A packet from an address that isn't on the list of the key that signed it is dropped. Port 51820 appears in the documentation and examples, but it's only a convention, and you can choose any other UDP port. Information about ports and the services that use them is collected on the Ports page.
When the connection doesn't work
- No handshake at all: the output of sudo wg shows no latest handshake line for the client. Check that the UDP port is open in the firewall, that the Endpoint in the client file is correct, and that the keys weren't swapped: each side's file contains its own private key and the other side's public key.
- A handshake, but no browsing: check that packet forwarding is enabled, that the NAT rule uses the correct interface name, and that the client's address appears in the AllowedIPs of its section on the server.
- Websites load partially: this is a common sign of an MTU problem. Lower the MTU in the client file, for example to 1280, and test again.
- DNS leaks: with a full tunnel, set a DNS line in the client file so that queries go through the tunnel and not to the DNS server of the local network.
Keeping the tunnel secure
- Private keys: the files with the private keys and wg0.conf are readable by root only. Replace an exposed key with a new key pair.
- A key pair for each device: this way, you can block a lost device with wg set and remove, without touching the others.
- Narrow AllowedIPs on the server: for each client on the server, set only its own address, with /32, and not wide ranges.
- An up-to-date client list: remove clients that are no longer in use, so the tunnel keeps only the access that is needed.
The addresses 203.0.113.10 and vpn.example.com on this page are reserved for documentation, so replace them with your server's address. The 10.8.0.0/24 range is an example private range: the private ranges are listed on the Reserved IP ranges page, and notation such as /24 and /32 is explained in the CIDR table. More firewall commands are on the UFW commands, firewalld commands, and iptables and nftables commands pages.
On a Linux VPS, you connect over SSH with full root access and set up a WireGuard server with every command on this page: keys, the wg0.conf file, routing and NAT, the firewall, and client files for computers and phones.
WireGuard FAQ
What is WireGuard, and why is it considered simple and fast?
WireGuard is a modern VPN whose core component runs inside the Linux kernel. Its configuration is short: one file for the server and one file for each client, each with keys, addresses, and a port. Each side is identified by a key pair, and traffic travels over UDP with modern encryption. That's why a WireGuard server is up and running in minutes, and connecting from a phone or a computer takes a single tap or click.
How do I connect a phone or a Windows computer to a WireGuard server?
Create a client file with its own keys on the server, and add the client's public key to the server file. For a phone, show the file as a QR code with qrencode -t ansiutf8, and scan it in the WireGuard app. On Windows and macOS, import the file into the official app and activate the tunnel.
What's the difference between a full tunnel and a split tunnel?
With a full tunnel, all of the device's traffic goes through the server, which you set with AllowedIPs = 0.0.0.0/0, ::/0 in the client file. Browsing then goes out with the server's IP address, even from a public Wi-Fi network. With a split tunnel, AllowedIPs lists only the networks that should go through the tunnel, such as the VPN network, and regular browsing goes out directly from the device.
How do I add a new client without disconnecting the clients already connected?
Add the client's [Peer] section to wg0.conf, and run sudo systemctl reload wg-quick@wg0 or sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'. The change is applied to the running interface, and the connected clients keep working without interruption.
Which port does WireGuard use, and can I change it?
The common port is UDP 51820, but it's only a convention. Change the ListenPort line in the server file and the port in the Endpoint line of the client files, and open the new port for UDP in the server's firewall.