متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 42WireGuard · Network

Install and set up WireGuard VPN

WireGuard is a modern VPN: very fast, simple, with strong crypto and a tiny config. Admins use it to reach private servers safely, to connect two sites, or to give laptops access to the company network. This guide installs WireGuard on RHEL and Ubuntu, makes keys, builds a VPN server and a client (split or full tunnel), opens the firewall with NAT, adds peers without downtime, and covers the problems you will meet first.

  • 11 الدروس
  • 16 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

42.1

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.

ItemValue
PackagesRHEL: wireguard-tools (the module is in the kernel); Ubuntu: wireguard
Toolswg (status, keys), wg-quick (bring up an interface from a file)
Config/etc/wireguard/wg0.conf (mode 600)
Servicewg-quick@wg0.service
Port51820/udp (you choose)
VPN network in this guide10.8.0.0/24 (server 10.8.0.1)

الأوامر في هذا الدرس

42.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install wireguard-tools
modprobe wireguard && lsmod | grep wireguard

42.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y wireguard

42.4

Make 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.

42.5

First configuration: the server

/etc/wireguard/wg0.conf
[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/32

Turn on IP forwarding, so VPN clients can reach the networks behind the server:

echo 'net.ipv4.ip_forward = 1' > /etc/sysctl.d/90-wireguard.conf
sysctl --system

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 --reload

Ubuntu 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:

systemctl enable --now wg-quick@wg0
wg show
interface: wg0
  public key: gN65BkIKy1eCE9pP1wdc8ROUtkHLF2PfAqYdyYBz6EA=
  listening port: 51820

42.6

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:

/etc/wireguard/wg0.conf (client)
[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
SettingWhy
AllowedIPs = 10.8.0.0/24, 10.0.0.0/16Split tunnel: only company networks go through the VPN.
AllowedIPs = 0.0.0.0/0, ::/0Full tunnel: all traffic goes through the VPN (public Wi-Fi).
EndpointThe public address of the server. Clients need it; the server learns clients by themselves.
PersistentKeepalive = 25Keeps the tunnel open through home routers (NAT).
DNSUse the company DNS while connected.
wg-quick up wg0
ping -c 2 10.8.0.1
wg show
  latest handshake: 12 seconds ago
  transfer: 18.4 KiB received, 9.1 KiB sent

الأوامر في هذا الدرس

42.7

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…=' remove

wg 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).

الأوامر في هذا الدرس

42.8

Make it safe

Security checklist:

  • Private keys 600, root only; one key pair per device; remove peers of lost devices at once.
  • AllowedIPs on the server as small as possible (/32 per 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 show latest handshake per peer.

الأوامر في هذا الدرس

42.9

Update and remove

dnf -y upgrade wireguard-tools
apt-get install --only-upgrade -y wireguard
systemctl disable --now wg-quick@wg0
dnf -y remove wireguard-tools

42.10

Fix common problems

ProblemCauseFix
No latest handshake at allUDP 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 serverNo IP forwarding or no NAT.sysctl net.ipv4.ip_forward=1; masquerade rule.
Traffic to one network does not go through the VPNNot in the client's AllowedIPs.Add the network to AllowedIPs.
Two clients fight / connection jumpsThe same key or the same VPN IP on two devices.One key pair and one IP per device.
Websites hang but ping worksMTU too big on the path.Set MTU = 1380 (or lower) in [Interface].
Names do not resolve over the VPNNo DNS or the DNS server is not reachable.Set DNS = and allow it in AllowedIPs.
wg-quick: 'wg0' already existsThe interface is up already.wg-quick down wg0, then up again.

الأوامر في هذا الدرس

42.11

Next steps

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.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل