هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
Know it before you install
Every machine (a peer) has a private key and a public key. A peer accepts packets only from public keys it knows, and AllowedIPs says which addresses belong to which peer (it is the routing table and the access list in one). WireGuard uses UDP (default port 51820) and is silent to strangers.
| Item | Value |
|---|---|
| Packages | RHEL: wireguard-tools (the module is in the kernel); Ubuntu: wireguard |
| Tools | wg (status, keys), wg-quick (bring up an interface from a file) |
| Config | /etc/wireguard/wg0.conf (mode 600) |
| Service | wg-quick@wg0.service |
| Port | 51820/udp (you choose) |
| VPN network in this guide | 10.8.0.0/24 (server 10.8.0.1) |
Install on RHEL, Rocky, Alma and Fedora
Install on Ubuntu and Debian
apt-get update
apt-get install -y wireguardMake the keys
On the server (and the same on every client). umask 077 keeps the private key readable by root only:
umask 077
cd /etc/wireguard
wg genkey | tee server.key | wg pubkey > server.pub
cat server.pub
gN65BkIKy1eCE9pP1wdc8ROUtkHLF2PfAqYdyYBz6EA=انتبه
Never send a private key over chat or email. Each side shares only its public key.
First configuration: the server
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <contents of server.key>
[Peer]
# alice-laptop
PublicKey = <alice's public key>
AllowedIPs = 10.8.0.2/32
[Peer]
# bob-laptop
PublicKey = <bob's public key>
AllowedIPs = 10.8.0.3/32Turn on IP forwarding, so VPN clients can reach the networks behind the server:
Open the firewall and NAT the VPN traffic to the private network. RHEL with firewalld:
firewall-cmd --permanent --add-port=51820/udp
firewall-cmd --permanent --zone=internal --add-interface=wg0
firewall-cmd --permanent --zone=public --add-masquerade
firewall-cmd --reloadUbuntu with UFW: ufw allow 51820/udp, ufw route allow in on wg0 out on eth0, and a NAT rule in /etc/ufw/before.rules (a *nat block with -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE).
Start it now and at boot:
The client
On Linux put this in /etc/wireguard/wg0.conf; on Windows, macOS, Android and iOS import the same text into the WireGuard app:
[Interface]
Address = 10.8.0.2/24
PrivateKey = <alice's private key>
DNS = 10.0.0.53
[Peer]
PublicKey = gN65BkIKy1eCE9pP1wdc8ROUtkHLF2PfAqYdyYBz6EA=
Endpoint = vpn.example.com:51820
AllowedIPs = 10.8.0.0/24, 10.0.0.0/16
PersistentKeepalive = 25| Setting | Why |
|---|---|
AllowedIPs = 10.8.0.0/24, 10.0.0.0/16 | Split tunnel: only company networks go through the VPN. |
AllowedIPs = 0.0.0.0/0, ::/0 | Full tunnel: all traffic goes through the VPN (public Wi-Fi). |
Endpoint | The public address of the server. Clients need it; the server learns clients by themselves. |
PersistentKeepalive = 25 | Keeps the tunnel open through home routers (NAT). |
DNS | Use the company DNS while connected. |
Daily use: add peers and the real uses
Add a new peer to the running server without cutting the others, then save it in the file:
wg set wg0 peer 'Xc0…carol-public-key…=' allowed-ips 10.8.0.4/32
wg show wg0 peers
vim /etc/wireguard/wg0.conf
wg syncconf wg0 <(wg-quick strip wg0)
wg set wg0 peer 'Xc0…carol-public-key…=' removewg syncconf loads changes from the file without restarting the interface. For many users, tools like wg-easy or a small script that makes keys + client files help.
Real uses at work:
- Admin access to private servers (SSH, databases) without public ports.
- Site-to-site: two offices or an office and a cloud VPC (each side a peer with the other's network in
AllowedIPs). - Roaming laptops on public Wi-Fi (full tunnel).
Make it safe
Security checklist:
- Private keys
600, root only; one key pair per device; remove peers of lost devices at once. AllowedIPson the server as small as possible (/32per client).- Firewall the VPN zone: VPN users reach only what they need (not the whole network).
- Optional: a preshared key per peer (
wg genpsk,PresharedKey =) for extra protection. - Keep the kernel updated (WireGuard lives in the kernel).
- Log and monitor:
wg showlatest handshake per peer.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
No latest handshake at all | UDP port blocked, wrong Endpoint, or wrong public key. | Open 51820/udp (firewall and cloud security group); compare keys. |
| Handshake OK, but no traffic to the network behind the server | No IP forwarding or no NAT. | sysctl net.ipv4.ip_forward=1; masquerade rule. |
| Traffic to one network does not go through the VPN | Not in the client's AllowedIPs. | Add the network to AllowedIPs. |
| Two clients fight / connection jumps | The same key or the same VPN IP on two devices. | One key pair and one IP per device. |
| Websites hang but ping works | MTU too big on the path. | Set MTU = 1380 (or lower) in [Interface]. |
| Names do not resolve over the VPN | No DNS or the DNS server is not reachable. | Set DNS = and allow it in AllowedIPs. |
wg-quick: 'wg0' already exists | The interface is up already. | wg-quick down wg0, then up again. |
Next steps
- Quick how-to: Connect two servers with a WireGuard VPN.
- Command page: wg, Command page: ip.
- At work: Fast modern VPN. The classic VPN: OpenVPN.
- The firewall around it: firewalld, UFW.
1What does AllowedIPs do on a WireGuard peer?
It says which addresses belong to that peer: traffic to them goes into the tunnel, and only packets from them are accepted.
2Handshake works, but clients can not reach servers behind the VPN. What is missing?
IP forwarding and NAT (masquerade) on the VPN server.