
WireGuard is an open-source modern VPN (Virtual Private Network) solution that utilizes cryptography protocols to create secure network connections between devices. It's efficient and offers improved reliability than traditional VPN protocols like IPSec. This guide explains how to install WireGuard VPN on a FreeBSD 14.0 and securely configure network tunnels on the server.
Before you begin:
Deploy a FreeBSD 14.0 server instance on Vultr.
Access the server using SSH.
Create a non-root user with sudo privileges and switch to the account.
WireGuard is available in the default FreeBSD pkg repositories with the latest package information. In the following steps, install the latest WireGuard VPN package on your server.
Update the server's package repository information.
Upgrade all installed packages to the latest versions.
Install WireGuard.
The above command installs the wireguard-tools package which includes the wireguard package and other necessary packages such as wg, and wg-quick that simplify the management of WireGuard interfaces.
WireGuard uses the server-client architecture to create network tunnels. The Wireguard server and client use private and public keys to authenticate and encrypt data. A client encrypts data using the server's public key which can only be decrypted using the associated server private key to ensure secure tunnel communications. Follow the steps below to configure the WireGuard VPN with a new tunnel interface and generate new key pairs to enable secure connections on the server.
Change the WireGuard /usr/local/etc/wireguard directory permissions to grant the owner read and write privileges.
Switch to the directory.
Generate a new private key to use with the WireGuard server.
Copy the generated private key value similar to the one below.
Generate a new public key based on the generated private key.
Copy the generated public key similar to the one below.
Create a new WireGuard server configuration using a text editor such as vi.
Add the following configurations to the file. Replace CHVDcFReVNu3vLwcCqpM3Wlu9mFEeJkQbVa4j4FBd2U= with the actual server private key you generated earlier.
Save and close the file.
The above configuration creates a new WireGuard interface that uses the private IP subnet 192.168.100.0/24 with the server address 192.168.100.1. Within the configuration:
Address: Sets the private IP subnet to use on the WireGuard server interface.SaveConfig: Allows the WireGuard interface to save its runtime configuration when the interface shuts down.ListenPort: Sets the server port to listen for incoming connections to the WireGuard interface.PrivateKey: Sets the WireGuard server's private key.WireGuard client configurations enable tunnel connections to the WireGuard server interface using the server's public key and public IP information. Each WireGuard client runs with a unique public key allowed to connect on the server interface to create a tunnel. Follow the steps below to create a shareable client configuration and enable it on your WireGuard server interface.
Generate a new private key for the WireGuard client.
Copy the generated private key similar to the one below.
Generate a new public key based on the private key value.
Copy the generated public key similar to the one below.
Create a new WireGuard client configuration file.
Add the following configurations to the file. Replace uIsqtxRWeDusnNYTWHdznJWfBrYe24996pfQ9jqNYmg= with the generated client private key, uSYKhXf8rbJfZNtUNuRwzAOYmjBnre3O2T+Isv4f+k0= with the server public key, and 139.180.144.36 with your WireGuard server's public IP address.
Save and close the file.
The above configuration creates a new WireGuard client that tunnels all network traffic and subnets 0.0.0.0/0 through the WireGuard VPN tunnel. In addition, client uses the 192.168.100.2 private IP address when connected to the WireGuard server. Within the configuration:
PrivateKey: Sets the WireGuard client private key.Address: Sets the client's private IP address and subnet to use when connected to a WireGuard tunnel.PublicKey: Sets the WireGuard server public key.AllowedIPs: Specifies the IP addresses and subnets allowed to connect through the WireGuard VPN connection. The value 0.0.0.0/0 all IP addresses and allows network interfaces to access the VPN connection.Endpoint: Sets the WireGuard server's public IP address and port the client should connect to.PersistentKvipalive: Sends data packets to the WireGuard server every 30 seconds to keep the connection alive.Oepn the WireGuard server configuration file.
Add the following configurations at the end of the file. Replace DPodowSBbXs+dvT4gKabLx7ecOZUhNlISPJEErOal28= with the client public key you generated earlier.
The above configuration enables your WireGuard client to connect to the WireGuard server interface. Within the configuration:
PublicKey: Sets the WireGuard client's public key.AllowedIPs: Enables the client's private IP address allowed to connect using the specified public key. Any other addresses such as 192.168.100.3 cannot connect using the public key unless you a create new client configuration with a different key.Enable the WireGuard server interface wg0 using the sysrc utility.
Download a copy of your WireGuard client configuration to your device using a secure transfer protocol such as scp. For example, run the following command on a Windows computer to download the file.
Enable the WireGuard to automatically start at boot time.
Start the WireGuard server.
View the WireGuard server status and verify the active interface information.
Your output should be similar to the one below.
Run the following command when stopping the WireGuard service.
The pf (Packet Filter) firewall is active and enabled on Vultr FreeBSD servers by default. Follow the steps below to secure the server and enable network connections through the WireGuard server interface.
Modify the sysctl.conf file to enable IP forwarding on the server.
Apply the sysctl.conf changes on your server.
Enable the Packet Filter firewall.
Enable Packet Filter logs.
View the server network interfaces and verify that the wg0 WireGuard interface is available.
Your output should be similar to the one below.
Open the Packet Filter configuration file.
Add the following configurations to the file. Replace vtnet0 with your actual server public interface name.
Save and close the file.
The above configuration enables new firewall rules to apply on the server. Within the configuration:
ext_if: Sets the external server network interface vtnet0.wg_if: Sets the WireGuard's interface to wg0.set skip on lo: Skips filtering on the loopback interface 127.0.0.1 to avoid internal traffic filtering.scrub in on $ext_if all fragment reassemble: Reassembles fragmented packets on the external network interface.nat on $ext_if from $wg_if:network to any -> ($ext_if): Enables NAT to forward packets from the WireGuard interface to the external interface.pass in on $ext_if proto udp from any to ($ext_if) port 51820: Allows incoming connections on the WireGuard UDP port 51820.pass quick on $wg_if: Enables all types of traffic on the WireGuard interface.pass out kvip state: Enables all outgoing traffic and kvips to track connection states.Start the Packet Filter firewall.
Reload the Packet Filter rules to apply changes.
The WireGuard server accepts connections from compatible applications such as the WireGuard client package. Follow the steps below to install the WireGuard client application and connect to the server.
Download and Install the latest WireGuard VPN client application on your device. For example, WireGuard VPN for Windows.
Open the WireGuard VPN client application.
Click Add Tunnel or Import tunnel(s) from file to load your client configuration file.
Find and open the client configuration file you downloaded earlier.
Click Activate to connect to your WireGuard VPN server.
Verify a network connection to your WireGuard server is established and data transfer is active on the interface.
Run the following command on the WireGuard server to verify the number of clients connected to the interface.
You should output should be similar to the one below.
You have installed and configured WireGuard VPN server on FreeBSD 14.0 and set up a client to create a VPN tunnel on the server. WireGuard setup enables encrypted data transfers, secure access, and effective network traffic management necessary when working with multiple client devices. For more information about WireGuard VPN, visit the WireGuard VPN Documentation.
0 Comments
Be the first to comment and share your perspective with the community.