WireGuard
WireGuard Between Two AlmaLinux Servers
Theory, configuration, testing, troubleshooting, and operational hardening
Purpose: Build a persistent WireGuard tunnel between two AlmaLinux servers.
This guide uses a simple point-to-point VPN. Server A and Server B can communicate over private tunnel addresses, even when their normal network addresses are on different networks.
Example topology
| Setting | Serevr A | Server B |
|---|---|---|
| Role | Initiator / peer A | Peer B |
| Public or reachable endpoint | 198.51.100.10:51820 | 203.0.113.20:51820 |
| WireGuard interface | wg0 | wg0 |
| Tunnel address | 10.20.0.1/24 | 10.20.0.2/24 |
| WireGuard UDP port | 51820 | 51820 |
| Example LAN behind the server (optional) | 192.168.10.0/24 | 192.168.20.0/24 |
The public addresses above are documentation-only TEST-NET addresses. Replace them with the real reachable addresses or DNS names of your servers. Do not copy them as real endpoints.
This guide assumes each server can reach the other over UDP port 51820, and that both systems use a supported AlmaLinux release with systemd, firewalld, and NetworkManager available. If one server is behind NAT, see the NAT section.
1. What WireGuard does
WireGuard is a modern Layer 3 VPN. It creates an encrypted virtual network interface, commonly named wg0, and routes IP packets through it to configured peers.
After configuration, applications can communicate using the tunnel IP addresses:
- Server A reaches Server B at
10.20.0.2. - Server B reaches Server A at
10.20.0.1. - SSH, monitoring, databases, or other services can listen on or be reached through these tunnel addresses, subject to routing and firewall policy.
WireGuard does not automatically make every network behind either server reachable. It only sends traffic matching the configured routes (AllowedIPs) to the relevant peer. Routing, IP forwarding, and firewall rules are separate concerns.
1.1 Main components
- Interface (
wg0): the virtual network interface on each server. - Private key: secret key kept on one server; never share it.
- Public key: derived from the private key and shared with the other peer.
- Peer: a remote WireGuard participant identified by its public key.
- Endpoint: the remote peer’s reachable IP address or DNS name and UDP port.
- AllowedIPs: both a routing selector for outgoing traffic and a source-address allow-list for packets received from that peer.
- ListenPort: local UDP port on which a peer listens. It is often configured on the server expected to accept incoming connections.
- PersistentKeepalive: optional periodic keepalive packet, useful when a peer is behind NAT and needs its NAT mapping to stay open.
1.2 WireGuard is not a traditional client/server VPN
WireGuard peers are fundamentally peers. One peer can be configured with a fixed endpoint and the other can learn the first peer’s endpoint from authenticated traffic. In this guide, Server B is the stable endpoint and Server A initiates traffic toward it.
The words “server” and “client” below describe the example’s operational roles, not a protocol restriction.
1.3 Public keys authenticate peers; they do not encrypt by themselves
Each peer keeps its private key secret and shares its public key. WireGuard uses these keys to authenticate peers and establish encrypted sessions. Do not exchange private keys by chat, email, Git, or a shared ticket.
1.4 What WireGuard does not provide automatically
WireGuard does not automatically:
- open the firewall on the physical interface;
- configure your router’s port forwarding;
- enable Linux IP forwarding;
- route a remote LAN through the tunnel;
- configure DNS for applications;
- guarantee a service is reachable if that service’s own firewall or bind address blocks it.
2. Plan the address space before configuring anything
Choose a tunnel subnet that does not overlap with:
- either server’s local LAN;
- your home or office LAN;
- Docker, Podman, Kubernetes, or other VPN subnets;
- any remote network you might route later.
This guide uses 10.20.0.0/24 for the tunnel. The two servers use 10.20.0.1 and 10.20.0.2.
For a simple two-host tunnel, /24 is easy to understand. A /30 or /31 can also be used for point-to-point designs, but be sure that the chosen addressing and routing match your intended topology. Keep the example simple while learning.
Addressing checklist
Before starting, record:
- Server A’s real reachable IP or DNS name.
- Server B’s real reachable IP or DNS name.
- Which server can accept incoming UDP connections.
- Whether either server is behind NAT.
- The tunnel subnet and the IP assigned to each peer.
- Whether the goal is only server-to-server access or access to whole LAN’s behind the servers.
For this guide, the initial goal is server-to-server access only. LAN-to-LAN routing is an optional advanced section later.
3. Prerequisites and connectivity
On both servers, become root or use sudo for administrative commands.
Check the operating system:
cat /etc/almalinux-release
uname -r
Check whether firewalld is running:
sudo systemctl status firewalld
sudo firewall-cmd --state
Check current IP addresses and routes:
ip -br address
ip route
Check time synchronization. Accurate time is good operational practice, although WireGuard does not require both hosts to have perfectly synchronized clocks to encrypt packets:
timedatectl status
Confirm you have a way to access each server if you make a firewall mistake. If these are remote systems, keep an existing SSH session open while applying firewall changes and test a second session before closing it.
UDP reachability
Server B must be reachable on UDP port 51820. If it is behind a router or cloud firewall, allow/forward that UDP port to Server B. Server A normally does not need an inbound port-forward for this example because it initiates the connection.
Do not confuse UDP with TCP: allowing TCP port 51820 will not allow WireGuard traffic.
4. Install WireGuard tools
Package names and kernel support can vary by AlmaLinux major release, enabled repositories, and kernel build. On current supported AlmaLinux releases, first try the distribution-provided package:
sudo dnf install -y wireguard-tools
If DNF cannot find the package, do not blindly install random kernel modules or third-party scripts. First check the release, enabled repositories, and official AlmaLinux documentation for that specific release:
cat /etc/almalinux-release
dnf repolist
dnf search wireguard
The wireguard-tools package provides commands such as wg and wg-quick. WireGuard kernel support is included in modern Linux kernels, but the exact package and support path depend on the system’s release and kernel. Do not assume that a package named kmod-wireguard is always required.
Check that the tools are available:
wg --version
wg-quick --help
wg displays and manages WireGuard protocol configuration and runtime state. wg-quick is a helper that configures the interface, addresses, and routes from a configuration file.
5. Generate keys securely
Do this separately on each server. Each server must have its own key pair.
Create a configuration directory with restrictive permissions:
sudo install -d -m 700 /etc/wireguard
On Server A:
sudo sh -c 'umask 077; wg genkey | tee /etc/wireguard/server-a.key | wg pubkey > /etc/wireguard/server-a.pub'
sudo chmod 600 /etc/wireguard/server-a.key
sudo chmod 644 /etc/wireguard/server-a.pub
On Server B:
sudo sh -c 'umask 077; wg genkey | tee /etc/wireguard/server-b.key | wg pubkey > /etc/wireguard/server-b.pub'
sudo chmod 600 /etc/wireguard/server-b.key
sudo chmod 644 /etc/wireguard/server-b.pub
The umask 077 prevents the private key from being created with permissions accessible to other users. The private key should be readable only by root.
Display the public key on each server so you can copy it to the other server:
sudo cat /etc/wireguard/server-a.pub
or on Server B:
sudo cat /etc/wireguard/server-b.pub
Exchange public keys only. In the examples below, replace SERVER_A_PUBLIC_KEY and SERVER_B_PUBLIC_KEY with the actual one-line contents of the corresponding .pub files. These placeholders are not valid keys.
Key handling rules
- Never paste private keys into documentation, screenshots, tickets, or public repositories.
- Do not place private keys in a Git repository, even a private one.
- Keep backups encrypted and access-controlled.
- If a private key is exposed, generate a new key pair and update the peer configuration on the other side.
6. Configure Server B (the stable endpoint)
Server B will listen on UDP port 51820 and has tunnel address 10.20.0.2/24.
Create /etc/wireguard/wg0.conf:
[Interface]
Address = 10.20.0.2/24
ListenPort = 51820
PrivateKey = SERVER_B_PRIVATE_KEY
[Peer]
# Server A
PublicKey = SERVER_A_PUBLIC_KEY
AllowedIPs = 10.20.0.1/32
Replace SERVER_B_PRIVATE_KEY with the contents of /etc/wireguard/server-b.key, and replace SERVER_A_PUBLIC_KEY with the public key copied from Server A.
Edit securely:
sudo install -m 600 /dev/null /etc/wireguard/wg0.conf
sudoedit /etc/wireguard/wg0.conf
sudo chmod 600 /etc/wireguard/wg0.conf
Important:
Addressis the IP assigned to Server B’s WireGuard interface.ListenPorttells WireGuard which local UDP port to listen on.PrivateKeyis Server B’s secret private key.- The peer’s
AllowedIPs = 10.20.0.1/32says that traffic for Server A’s tunnel IP is routed to that peer, and packets received from that peer must use anallowed source address. /32identifies exactly one IPv4 address. It does not mean the whole10.20.0.0/24subnet.
Do not put quotes around the key strings. Each key is a single base64-encoded line.
7. Configure Server A (the initiating peer)
Server A has tunnel address 10.20.0.1/24 and sends traffic to Server B’s reachable endpoint.
Create /etc/wireguard/wg0.conf:
[Interface]
Address = 10.20.0.1/24
PrivateKey = SERVER_A_PRIVATE_KEY
[Peer]
# Server B
PublicKey = SERVER_B_PUBLIC_KEY
Endpoint = 203.0.113.20:51820
AllowedIPs = 10.20.0.2/32
PersistentKeepalive = 25
Replace: - SERVER_A_PRIVATE_KEY with the contents of /etc/wireguard/server-a.key; - SERVER_B_PUBLIC_KEY with Server B’s public key; - 203.0.113.20 with Server B’s real reachable IP or DNS name.
PersistentKeepalive = 25 sends a small authenticated packet periodically when needed. It is useful if Server A is behind NAT or a stateful firewall and needs to keep its outbound mapping open. It is not mandatory on every peer; omit it if it is unnecessary for your network.
Protect the configuration:
sudo install -m 600 /dev/null /etc/wireguard/wg0.conf
sudoedit /etc/wireguard/wg0.conf
sudo chmod 600 /etc/wireguard/wg0.conf
Why Server A has an Endpoint and Server B does not
Server A knows where to find Server B, so it has a fixed Endpoint.
Server B can learn Server A’s current endpoint from correctly authenticated packets. This is useful when Server A is behind NAT or its public address changes.
If both endpoints have stable public addresses, you can configure endpoints on both peers if you prefer. It is not required for this example.
8. Understand AllowedIPs
AllowedIPs is one of the most important WireGuard settings because it has two roles:
- Outbound routing selection: which destination addresses should be sent to a peer.
- Inbound source validation: which source addresses are accepted as belonging to that peer.
For this two-host example:
AllowedIPs = 10.20.0.2/32
on Server A means that traffic destined for 10.20.0.2 goes through Server B’s peer configuration. On Server B, the corresponding setting is 10.20.0.1/32.
Common patterns:
-----------------------------------------------------------------------
AllowedIPs Meaning
----------------------------------- -----------------------------------
10.20.0.2/32 One specific IPv4 host
10.20.0.0/24 All addresses in the tunnel subnet
192.168.20.0/24 A remote LAN, if routing to that LAN is intentionally configured
0.0.0.0/0 All IPv4 destinations
::/0 All IPv6 destinations
-----------------------------------------------------------------------
Do not set 0.0.0.0/0 casually. It commonly creates a full-tunnel design where all IPv4 traffic is routed through that peer, and it requires careful routing, DNS, and firewall planning. For a simple host-to-host tunnel, use the narrow /32 entries shown above.
9. Configure the firewall on Server B
WireGuard uses UDP. Open UDP port 51820 in the active firewalld zone on Server B.
Inspect the active zones and interfaces:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all
If the public-facing interface is in the public zone, allow the port permanently:
sudo firewall-cmd --permanent --zone=public --add-port=51820/udp
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --list-ports
Use the actual zone assigned to Server B’s public interface instead of assuming it is public. Do not blindly move interfaces between zones on a remote server.
If Server B is hosted by a cloud provider, also allow inbound UDP 51820 in the provider’s network firewall/security group. If a router is in front of Server B, forward UDP 51820 to Server B’s LAN address.
Server A typically does not need an inbound UDP rule for this configuration, because it initiates traffic outbound and stateful firewalls usually allow the response. Local firewall policies can differ, so inspect them if the handshake fails.
Open only what is needed
Opening UDP 51820 allows WireGuard packets to reach the host. It does not mean SSH or other services should be opened to the whole internet. Permit application traffic through wg0 according to the services you actually want to expose.
10. Start WireGuard and enable it at boot
After checking that both configuration files are correct, start Server B first:
sudo systemctl enable --now wg-quick@wg0
Then start Server A:
sudo systemctl enable --now wg-quick@wg0
The wg-quick@wg0 service loads /etc/wireguard/wg0.conf, creates/configures wg0, and sets up addresses and routes described by the configuration.
enable configures it to start at boot;
--now starts it immediately.
Check the service:
sudo systemctl status wg-quick@wg0
Check the interface and peer state:
ip -br address show wg0
sudo wg show
ip route
If the service fails, read the logs before repeatedly restarting it:
sudo journalctl -u wg-quick@wg0 --no-pager -n 100
The wg show output can display public keys, endpoints, latest handshake times, transfer counters, and allowed IPs. It should not reveal the private key.
11. Test the tunnel
11.1 Ping the remote tunnel address
From Server A:
ping -c 4 10.20.0.2
From Server B:
ping -c 4 10.20.0.1
A ping failure does not by itself prove that WireGuard is broken. ICMP might be blocked by firewalld or another firewall even when the tunnel handshake is successful.
11.2 Check the handshake
On either server:
sudo wg show
Look for:
latest handshake: a recent timestamp after traffic has been exchanged;transfer: sent and received byte counters that increase when you generate traffic;- the expected peer public key and
AllowedIPs.
A WireGuard interface can be up even if the peer has not completed a handshake. The handshake is a better sign of peer reachability than the mere existence of wg0.
11.3 Test a real service
If SSH is enabled on Server B, test it from Server A:
Use a real username. Ensure SSH is running and that Server B’s firewall allows SSH traffic arriving on wg0.
To inspect listening services:
sudo ss -lntup
A service bound only to 127.0.0.1 will not be reachable through the tunnel. A service may listen on 0.0.0.0 or on the tunnel IP, subject to its security requirements.
11.4 Inspect packets if necessary
On Server B, while initiating traffic from Server A:
sudo tcpdump -ni any udp port 51820
If tcpdump is not installed, install it from the distribution’s trusted repositories. A capture can show whether UDP packets arrive, but encrypted WireGuard payloads will not reveal the application contents.
12. Allow services through the tunnel with firewalld
The outer UDP port and the inner application traffic are different things:
- UDP
51820reaches WireGuard on Server B’s physical/network-facing interface. - Once decrypted, traffic is associated with
wg0and its tunnel addresses. - Firewalld policy must permit the desired inner traffic as well.
Inspect the active zones:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --get-zone-of-interface=wg0
sudo firewall-cmd --zone=public --list-all
Depending on the distribution’s firewalld version and current configuration, wg0 may not be assigned to a zone automatically. Do not assume which zone it belongs to; inspect it first.
A straightforward learning-lab option is to assign wg0 to a dedicated zone and allow only the services required over the VPN. For example, if the trusted zone is not appropriate for your security model, do not use it simply because the interface is encrypted. Encryption authenticates peers; it does not make every peer or every forwarded
packet trustworthy.
Example of creating a dedicated zone:
sudo firewall-cmd --permanent --new-zone=wireguard
sudo firewall-cmd --permanent --zone=wireguard --add-interface=wg0
sudo firewall-cmd --permanent --zone=wireguard --add-service=ssh
sudo firewall-cmd --reload
This is an example, not a universal prescription. If the zone already exists, --new-zone will fail. Check your configuration first, and ensure that the interface is not already assigned to a different zone. You can also create richer source-specific rules if only one peer should be allowed to reach a service.
After changes, inspect the resulting state:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=wireguard --list-all
For a remote host, preserve an existing management session and verify that your SSH access remains available before disconnecting.
13. Troubleshooting checklist
Problem: wg0 does not exist
Check the service and configuration syntax:
sudo systemctl status wg-quick@wg0
sudo journalctl -u wg-quick@wg0 --no-pager -n 100
sudo ls -l /etc/wireguard/wg0.conf
Frequent causes:
- typo in the configuration;
- wrong private key or malformed key line;
- invalid interface address;
- file permissions or configuration path errors;
- a conflicting interface or route.
Problem: wg0 exists but there is no handshake
Check:
- Server A has the correct Server B public key.
- Server B has the correct Server A public key.
- Server A’s endpoint points to the real reachable IP/DNS and UDP port.
- UDP port 51820 is permitted by the host firewall, cloud firewall, and router.
- Server B’s WireGuard service is running and listening.
- The peers’ public keys and configurations have not been swapped.
Useful commands:
sudo wg show
sudo ss -lunp | grep 51820
sudo firewall-cmd --get-active-zones
sudo tcpdump -ni any udp port 51820
ss may not display a conventional userspace listening socket for every kernel implementation, so treat it as one clue rather than definitive proof.
wg show and packet captures are often more informative.
Problem: handshake exists but ping fails
Possible causes:
- ICMP is blocked in firewalld or another firewall;
AllowedIPsdo not include the expected tunnel destination/source;- the route is missing or overridden;
- the tunnel address is wrong;
- packets are received but the return path is incorrect.
Check:
ip -br address show wg0
ip route
sudo wg show
sudo firewall-cmd --get-active-zones
Use a real TCP service test as well as ping.
Problem: ping works but SSH fails
Check that SSH is installed and active on the target:
sudo systemctl status sshd
sudo ss -lntp | grep ':22'
Check firewalld rules and whether sshd is listening on an address reachable via wg0. AlmaLinux uses the sshd systemd service name.
Problem: it worked until reboot
Confirm the service is enabled:
sudo systemctl is-enabled wg-quick@wg0
sudo systemctl status wg-quick@wg0
If needed:
sudo systemctl enable wg-quick@wg0
Problem: one side is behind NAT
Keep PersistentKeepalive = 25 on the peer behind NAT when necessary. Ensure the peer with the stable public endpoint permits UDP 51820. If both peers are behind NAT without a reachable public endpoint, you may need a third reachable relay/server or a different network design.
Problem: overlapping networks
If a local LAN and a tunnel/remote LAN use the same subnet, routing becomes ambiguous. Choose a non-overlapping tunnel subnet before adding more routes. Changing AllowedIPs cannot fix a fundamentally overlapping address plan.
14. Optional: route a LAN behind each server
The initial configuration is only for communication between 10.20.0.1 and 10.20.0.2. To connect entire LANs behind the servers, more work is required.
Example:
- Server A’s LAN:
192.168.10.0/24 - Server B’s LAN:
192.168.20.0/24
At a high level, you would:
- Add
192.168.20.0/24to Server A’s peerAllowedIPsfor Server B. - Add
192.168.10.0/24to Server B’s peerAllowedIPsfor Server A. - Enable IPv4 forwarding on both servers where forwarding is needed.
- Configure firewalld forwarding/routing rules to permit traffic between
wg0and the relevant LAN interface. - Ensure each LAN knows how to return traffic to the remote LAN—through routes on its router, or by using carefully planned NAT where appropriate.
- Apply equivalent IPv6 forwarding and firewall policy if IPv6 is included.
The peer configuration would conceptually include:
Server A’s peer for Server B:
[Peer]
PublicKey = SERVER_B_PUBLIC_KEY
Endpoint = 203.0.113.20:51820
AllowedIPs = 10.20.0.2/32, 192.168.20.0/24
PersistentKeepalive = 25
Server B’s peer for Server A:
[Peer]
PublicKey = SERVER_A_PUBLIC_KEY
AllowedIPs = 10.20.0.1/32, 192.168.10.0/24
These snippets only illustrate the route/peer portion. They are not a complete LAN-to-LAN configuration. Do not enable forwarding or add broad firewall rules until you have planned the exact interfaces, source/destination ranges, return routes, and access policy. Forwarding a subnet is a different task from securing access to the two servers themselves.
Enable IPv4 forwarding (only if required)
A router must forward packets between interfaces. If this server is intended to route between the VPN and another network, enable forwarding deliberately, for example with a dedicated sysctl configuration:
sudo tee /etc/sysctl.d/90-wireguard-forwarding.conf >/dev/null <<'EOF'
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
sysctl net.ipv4.ip_forward
Only enable this on systems intended to route packets. Forwarding by itself does not create firewall permissions or return routes. For IPv6 routing, the corresponding IPv6 forwarding and firewall settings must be considered separately.
15. Security and maintenance recommendations
- Keep private keys root-readable only.
- Use narrow
AllowedIPsentries; avoid0.0.0.0/0unless intentionally building a full-tunnel VPN. - Open UDP 51820 only on the peer that needs to receive it.
- Permit only required application services on the tunnel interface.
- Keep AlmaLinux packages and the kernel updated through normal, supported maintenance.
- Back up
/etc/wireguardsecurely, not in a public Git repository. - Record the mapping between each peer’s public key, tunnel IP, and owner.
- Remove peer entries when a system is retired or a key is compromised.
- Monitor
wg showand systemd logs when diagnosing connectivity. - Do not assume that a VPN is a substitute for host firewalls, application authentication, or patching.
Quick operational commands
# Interface addresses
ip -br address
# Routes
ip route
# WireGuard status
sudo wg show
# Start now
sudo systemctl start wg-quick@wg0
# Stop now
sudo systemctl stop wg-quick@wg0
# Enable at boot
sudo systemctl enable wg-quick@wg0
# Restart after editing configuration
sudo systemctl restart wg-quick@wg0
# Service logs
sudo journalctl -u wg-quick@wg0 --no-pager -n 100
# Firewalld active zones
sudo firewall-cmd --get-active-zones
Be careful when restarting a remote host’s networking. Keep an existing SSH session open and confirm a second connection works before ending the first.
16. A useful mental model
When debugging, check each layer in order:
- Physical/IP reachability: Can Server A reach Server B’s public endpoint?
- UDP firewall: Can UDP 51820 reach Server B?
- WireGuard identity: Are the peer public keys correct?
- Handshake: Does
wg showreport a recent handshake? - Tunnel addressing and routes: Are the tunnel addresses and
AllowedIPscorrect? - Host firewall: Is the desired traffic permitted on
wg0? - Application: Is the target service listening on the expected address and port?
- Return path: Can replies get back to the originating peer?
This layered approach is more effective than changing random firewall rules. A successful handshake proves that the WireGuard peers can authenticate and exchange protocol traffic; it does not prove that every application port or routed subnet is working.
17. Final verification checklist
- Both servers have their own unique private/public key pair.
- Private keys are stored securely and never exchanged.
- Server B’s real endpoint accepts UDP 51820.
- Each peer has the other peer’s correct public key.
- Tunnel IPs do not overlap with existing networks.
-
wg-quick@wg0starts successfully on both hosts. -
wg showreports a recent handshake after traffic is generated. - Tunnel IP connectivity works, or any ping failure has been checked against firewall policy.
- The intended application works through the tunnel.
- Only necessary services are allowed through firewalld.
-
wg-quick@wg0is enabled at boot. - The configuration and key backups are stored securely.
Appendix A: Minimal configuration templates
Server A
[Interface]
Address = 10.20.0.1/24
PrivateKey = <SERVER_A_PRIVATE_KEY>
[Peer]
PublicKey = <SERVER_B_PUBLIC_KEY>
Endpoint = <SERVER_B_REAL_IP_OR_DNS>:51820
AllowedIPs = 10.20.0.2/32
PersistentKeepalive = 25
Server B
[Interface]
Address = 10.20.0.2/24
ListenPort = 51820
PrivateKey = <SERVER_B_PRIVATE_KEY>
[Peer]
PublicKey = <SERVER_A_PUBLIC_KEY>
AllowedIPs = 10.20.0.1/32
The angle-bracketed values are placeholders, not literal configuration. Replace them before starting WireGuard.
Appendix B: Official references
Use documentation for the exact AlmaLinux release you run, as package availability and firewall tooling can differ between major versions.
- WireGuard quick start: https://www.wireguard.com/quickstart/
- WireGuard concepts and protocol: https://www.wireguard.com/
wg(8)manual: https://man7.org/linux/man-pages/man8/wg.8.htmlwg-quick(8)manual: https://man7.org/linux/man-pages/man8/wg-quick.8.html- AlmaLinux documentation: https://docs.almalinux.org/
- firewalld documentation: https://firewalld.org/documentation/