
WireGuard is a modern, open-source and high-performance VPN (Virtual Private Network) solution known for its simplicity and efficiency. It aims offers secure and fast connections between different using tunnels to enable remote access, site-to-site connections, or protect outgoing network traffic. WireGuard offers improved speed, reliability, and ease of use as compared to traditional VPN protocols such as OpenVPN and IPSec.
This article explains how to install WireGuard VPN on Rocky Linux 9 to enable secure network tunnel connections on the server.
Before you begin:
WireGuard is not available in the default repositories on Rocky Linux 9. Follow the steps below to enable the EPEL (Extra Packages for Enterprise Linux) repository and install WireGuard on your server.
Install the EPEL repository.
Install the WireGuard tools package.
View the installed WireGuard version on your server.
Your output should be similar to the one below.
WireGuard uses the cryptokey routing mechanism to create VPN tunnels on your server by checking public keys linked to specific IP addresses. WireGuard requires a private key that matches a valid public key to enable VPN tunnel connections on a network interface. Follow these steps to set up a new WireGuard interface and create key pairs to use on your server.
Generate a new WireGuard server private and public key pair in the /etc/wireguard directory.
Modify the file permissions to grant only privileged users read access to the generated keys.
View your available server network interfaces and verify your main interface details.
Your output should be similar to the one below.
Based on the above output. enp1s0 is the main public network interface name and uses the public IP address 192.0.2.161 required by WireGuard to translate and forward network requests to the Internet.
Create a new WireGuard server interface configuration file. For example, wg0.conf.
Add the following configurations to the file. Replace <Server_Private_Key> with the actual private key you generated earlier.
Save and close the file.
The above WireGuard configuration creates a new interface with the private IP address 172.16.0.1. Within the configuration:
Address = 172.16.0.1/24: Assigns the private IP address 172.16.0.1 to the WireGuard interface, with a subnet mask of 255.255.255.0.SaveConfig = true: Enables WireGuard to automatically save the configuration when the server shuts down.PrivateKey = <Server_Private_Key>: Sets the WireGuard server's private key.ListenPort = 51820: Sets the WireGuard server port 51820 to listen for incoming VPN connection requests.A WireGuard client configuration consists of a private and public key pair allowed to create connections through the WireGuard server tunnel interface. In the following steps, create a new WireGuard client configuration that includes new private and public keys, permitted addresses, and traffic to allow on the VPN tunnel connections.
Generate new private and public keys in the WireGuard directory /etc/wireguard/. Replace client with your desired client naming scheme.
View the generated WireGuard client private and public key values.
Copy the generated private and public key values to your clipboard.
Create a new WireGuard client configuration file.
Add the following configurations to the file. Replace <Client_Private_Key> with your generated private key, <Server_Public_Key>, 192.0.2.161 with your actual WireGuard server's public key and IP address respectively.
Save and close the file.
The above configuration assigns creates a new WireGuard client with the private VPN tunnel IP address 172.16.0.2. Within the configuration:
PrivateKey: Sets the WireGuard client's private key used for encryption and authentication in the VPN tunnel.Address: Sets the WireGuard client's private IP address 172.16.0.2 with a subnet mask of 255.255.255.0.DNS: Sets Google public DNS server 8.8.8.8 as the DNS resolver to resolve domain names when connected to the VPN tunnel.PublicKey: Sets the public key of the destination WireGuard server.AllowedIPs: Defines the network client network addresses allowed to connect through the VPN tunnel.Endpoint: Sets the WireGuard server public IP address 192.0.2.161 and port 51820 to use when connecting to the VPN tunnel.PersistentKeepalive: Keeps the VPN connection active by sending keep-alive packets every 15 seconds.Open the WireGuard server interface configuration to enable the new client configuration.
Add the following configurations at the end of the file. Replace <Client_Public_Key> with your actual WireGuard client's public key you generated earlier.
Save and close the file.
The WireGuard wg-quick utility starts and manages VPN processes on your server. Follow the steps below to enable your WireGuard interface to start at boot time and allow VPN tunnels on your server using the wg-quick utility.
Start the WireGuard server wg0 interface.
Enable the WireGuard wg0 interface to automatically start at boot time.
View the WireGuard interface status and verify that it's active on your server.
Output.
View the WireGuard interface logs to verify the tunnel processes.
FirewallD is available and active on Vultr Rocky Linux 9 servers by default. In the following steps, configure the firewall to allow network connections on the default WireGuard server port 51820. In addition, enable port forwarding to forward network requests from the WireGuard interface to the internet using Network Address Translation (NAT) on your server.
View the firewalld status and verify that it's active.
If the firewalldstatus is inactive, run the following command to allow the SSH port 22 and enable the firewall.
Allow the default WireGuard interface UDP port 51820 through the firewall.
Reload firewalldto apply your configuration changes.
View all available firewalldport rules on your server and verify that the WireGuard port is available.
Output.
Enable IPV4 forwarding to forward packets from the WireGuard network to the server's public interface.
Reload your sysctl configuration to apply the configuration changes.
Enable address translation of network requests from the WireGuard interface 172.16.0.0/24 subnet to allow client connections to the Internet.
Reload firewalldto apply the new configuration changes.
Each WireGuard client requires a valid configuration file with a public key allowed to connect to the WireGuard VPN server interface. In the following steps, connect VPN clients to your WireGuard VPN server using the configuration you created earlier and test the VPN tunnel connections using the Ping utility.
Access your Rocky Linux 9 client server using SSH.
Install the EPEL repository and the WireGuard tools package on your server.
Download your WireGuard client configuration file from the VPN server using a file transfer protocol such as scp.
Enable the systemd-resolved service to enable DNS resolutions on the WireGuard network.
Enable the WireGuard client interface wg0 to connect to the VPN tunnel.
Output:
View the WireGuard tunnel status to verify that a VPN connection is established.
Your output should be similar to the one below.
Test connectivity to the WireGuard VPN server private IP address 172.16.0.1 using the Ping utility.
Verify that the connection is successful similar to the following output:
You have installed WireGuard on your Rocky Linux 9 server and connected clients to a VPN tunnel. WireGuard supports multiple client connections using valid key pairs allowed in your configurations. In addition, you can set up different interfaces and enable multiple client connections on the server. For more information, visit the WireGuard documentation.
0 Comments
Be the first to comment and share your perspective with the community.