Skip to content
ShellOps Daily

Linux server hardening, systemd, and Bash automation tips

Networking & Proxy

Set Up WireGuard VPN Between VPS and Home Server

Victor Novak7 min read

Connecting a home server to a VPS securely is essential for remote access, data synchronization, or network extension. Setting up a setup wireguard vpn between vps and home server offers a fast, secure, and easy-to-manage solution. WireGuard’s modern encryption and minimalistic design make it ideal for system administrators and DevOps users seeking reliability without complexity. This article guides you through the entire process, from installation to configuration and troubleshooting.

You’ll start by installing WireGuard on both systems and generating the necessary keys for secure communication. Next, you’ll configure persistent keepalive to ensure stable and uninterrupted connections. The article also explains how to simplify management using the `wg-quick` tool and verify the setup with the `wg` command.

Security is a top priority, so you’ll learn how to properly configure firewall rules to protect your WireGuard tunnel. Finally, common setup issues are addressed, along with debugging techniques to help you resolve problems quickly. By the end, you’ll have a fully functional and secure setup wireguard vpn between vps and home server that meets your remote access needs.

Install and Configure WireGuard on Both Endpoints

Setting up a WireGuard VPN between a VPS and a home server begins with installing and configuring WireGuard on both endpoints. This ensures that both systems can communicate securely over a private tunnel. The process involves installing the WireGuard package, generating cryptographic keys, and setting up the configuration files to define the connection parameters.

Install WireGuard on Linux Systems

Install WireGuard on Linux systems using the package manager. On Debian or Ubuntu, run 'sudo apt install wireguard' to install the software. For RHEL-based systems like CentOS, use 'sudo yum install wireguard-tools' or 'sudo dnf install wireguard-tools'. Ensure the kernel supports WireGuard by checking the version; it is available in Linux kernels 5.6 and later. If your kernel is outdated, consider updating it or using a newer distribution.

Generate and Set Up WireGuard Keys

Generate cryptographic keys for the VPS and home server using the 'wg genkey' command. This creates a private key, which you should securely store, and a public key, which is shared with the other endpoint. On the VPS, generate the private key with 'wg genkey' and save it as 'privatekey.vps'. Repeat the process on the home server to create 'privatekey.home'. Use the 'wg pubkey' command to extract the public key from the private key file. These keys are essential for establishing a secure and authenticated connection between the two devices.

Set Up Persistent Keepalive for Stable Connections

Persistent keepalive is a crucial setting for maintaining stable WireGuard connections, especially when dealing with unstable or intermittent internet connections. By configuring this option, you ensure that the tunnel remains active even during periods of inactivity, preventing unexpected disconnections.

Understanding Persistent Keepalive in WireGuard

WireGuard uses persistent keepalive to send periodic packets between endpoints, ensuring the connection remains open. Without this feature, the tunnel may drop due to inactivity, particularly when the connection is behind a NAT or firewall. The persistentkeepalive option instructs WireGuard to send keepalive packets at regular intervals, preventing the connection from timing out.

How to Configure Persistent Keepalive in the Config File

To configure persistentkeepalive, add the line `PersistentKeepalive = 25` to the WireGuard configuration file on both the VPS and home server. This value represents the interval in seconds between keepalive packets. A value of 25 seconds is generally recommended for most use cases, as it balances reliability with minimal network overhead. After making the change, restart the WireGuard service to apply the new settings. To verify the configuration, use the `wg` command to check the connection status and ensure that keepalive packets are being sent as expected.

Configure WireGuard with Wg-quick for Simplicity

Using wg-quick simplifies the process of setting up and managing WireGuard interfaces on both the VPS and home server. This tool automates the creation of the virtual network interface and applies the configuration from a specified .conf file, reducing the potential for manual errors and streamlining the setup.

  1. Install wg-quick on both systems using the package manager (e.g., apt install wireguard-tools on Debian/Ubuntu).
  2. Create a configuration file (e.g., /etc/wireguard/wg0.conf) with the peer and interface settings.
  3. Use the command sudo wg-quick up wg0 to start the WireGuard interface and apply the configuration.
  4. Ensure the systemd service for WireGuard is enabled and active with systemctl enable –now wg-quick@wg0.

Using Wg-quick for Easy WireGuard Management

The wg-quick command is ideal for managing WireGuard interfaces because it abstracts the complexity of manually configuring the interface. It handles tasks like interface creation, IP address assignment, and firewall rule integration, making it a reliable tool for system administrators. By using wg-quick, you can quickly bring up or tear down the tunnel without needing to edit multiple configuration files manually.

Verify and Troubleshoot WireGuard with Wg Command

To verify the status of the WireGuard interface, use the wg command, which provides detailed information about the connection, including peer status, traffic statistics, and connection health. If the interface fails to establish, check the configuration file for syntax errors or missing parameters. The wg command also allows you to troubleshoot by displaying real-time data, helping you identify and resolve issues such as failed handshake attempts or dropped packets.

Secure the WireGuard Tunnel with Firewall Rules

Securing the WireGuard tunnel with firewall rules is essential to prevent unauthorized access and ensure stable connectivity. Properly configuring the firewall on both the VPS and home server ensures that only legitimate traffic is allowed through the WireGuard port, while blocking potential threats.

Allow WireGuard Traffic Through the Firewall

To allow WireGuard traffic, you must explicitly permit the WireGuard port (typically 51820) through the firewall on both the VPS and home server. For UFW, use the command `sudo ufw allow 51820/udp` to open the port. If using iptables, add a rule with `sudo iptables -A INPUT -p udp –dport 51820 -j ACCEPT`. Ensure that the firewall is enabled and that these rules are applied correctly to avoid connection issues.

Best Practices for WireGuard Firewall Configuration

Best practices for firewall configuration include restricting access to the WireGuard interface and limiting traffic to only the necessary ports. Avoid exposing the WireGuard port to the public internet unless required. Instead, use private IP addresses for the WireGuard interface and ensure that the firewall blocks all other incoming traffic. Regularly review and update firewall rules to adapt to changing security requirements and prevent potential vulnerabilities.

Troubleshoot Common WireGuard Setup Issues

Troubleshooting WireGuard setup issues requires a systematic approach to identify and resolve common configuration and operational problems. By verifying key components and using built-in diagnostic tools, you can ensure a stable and secure tunnel between your VPS and home server.

  1. Check that the WireGuard keys are correctly generated and matched between devices.
  2. Verify that the IP addresses assigned to the WireGuard interface are valid and not overlapping.
  3. Ensure that the Wire,Guard service is running and that the configuration files are correctly formatted.
  4. Use the wg command to monitor the tunnel status and debug any issues.

Common WireGuard Configuration Errors

Common WireGuard configuration errors often stem from mismatched keys, incorrect IP assignments, or syntax issues in the configuration files. Ensure that the private and public keys are correctly generated and shared between the VPS and home server. A mismatch in keys will prevent the tunnel from establishing. Additionally, verify that the IP addresses assigned to the WireGuard interfaces are unique and do not overlap with other networks to avoid routing conflicts.

How to Use the Wg Command for Debugging

The wg command is a powerful tool for monitoring and debugging WireGuard connections. Run 'wg show' to view the current status of the tunnel, including peer information, IP addresses, and connection statistics. If the tunnel is not active, check for errors in the configuration file or service status. Use 'wg show peers' to see detailed information about each peer, including their public key, endpoint, and recent handshake status. This command can help identify connectivity issues and provide insights into the tunnel's performance.

Frequently Asked Questions

How do I ensure the WireGuard tunnel remains active after a long period of inactivity?

To keep the WireGuard tunnel active, enable the persistentkeepalive option in your configuration file. Set it to a value like 25 or 15 to maintain a connection even during inactivity.

Can I use the same WireGuard configuration on multiple devices?

Yes, you can use the same WireGuard configuration on multiple devices, but ensure each device has its own private key. The public key can be shared for peer connections.

What should I do if the WireGuard connection drops frequently?

If the connection drops, check for network instability and enable persistentkeepalive. Also, verify your firewall settings and ensure the WireGuard service is running continuously.

Is it possible to set up WireGuard without using wg-quick?

Yes, you can set up WireGuard manually by editing the /etc/wireguard/wg0.conf file. However, using wg-quick simplifies management and ensures the service starts on boot.

Final Thoughts

WireGuard provides a secure and efficient way to connect your VPS and home server. By using persistentkeepalive and wg-quick, you can ensure a stable and easy-to-manage connection.

Once configured, your setup allows remote access and secure data transfer. Consider exploring advanced firewall rules and monitoring tools for enhanced security.