Hello everyone,
I wanted to share a guide on setting up a WireGuard VPN connection to NordVPN on a Mikrotik router running RouterOS v7.x. While NordVPN provides instructions for setting up an IKEv2/IPSec VPN connection—which works fine—you need to use mangle to route specific destinations, and you cannot implement a kill switch. Other protocols like L2TP/IPSec have been retired by NordVPN, and OpenVPN does not work with Mikrotik routers. Using WireGuard offers better flexibility, including the ability to implement a kill switch and route specific traffic without complex configurations.
Prerequisites
- A Mikrotik router running RouterOS v7.x
- A Linux system (e.g., Debian) to retrieve necessary keys
- An active NordVPN subscription
Step 1: Install NordVPN and WireGuard on Linux
First, install the NordVPN client on your Linux system. You can find detailed instructions on the NordVPN support site:
Installing NordVPN on Linux distributions
Next, install WireGuard:
sudo apt install wireguard
Step 2: Retrieve Your Private Key and Server Information
Establish a VPN connection using the NordVPN client:
nordvpn connect
Then, retrieve your private key:
sudo wg show nordlynx private-key
Note: Keep your private key secure and do not share it.
Next, obtain the public key, IP address, and port of the connected NordVPN WireGuard server:
sudo wg show nordlynx
Example output:
peer: aaaaaaabbbbbbbbxxxxxxyyyyyyyyy=
endpoint: x.x.x.x:51820
Alternatively, to find other servers and their information, use the NordVPN API:
curl 'https://nordvpn.com/wp-admin/admin-ajax.php?action=servers_recommendations&filters\[servers_technologies\]\[identifier\]=wireguard_udp&limit=1' | python3 -m json.tool
Note: Each server has its own public key, but your private key remains the same.
Step 3: Configure WireGuard on the Mikrotik Router
Create the WireGuard Interface
/interface wireguard add listen-port=51820 mtu=1420 name=wg-nordvpn private-key="your_private_key"
- Replace “your_private_key” with the private key you obtained earlier.
Add the Peer (NordVPN Server)
/interface wireguard peers add allowed-address=0.0.0.0/0 endpoint-address=your_server_address endpoint-port=51820 interface=wg-nordvpn persistent-keepalive=5s public-key="server_public_key"
- Replace your_server_address with the endpoint address (e.g., us100.nordvpn.com).
- Replace “server_public_key” with the public key of the NordVPN server.
Assign an IP Address to the Interface
/ip address add address=10.5.0.2 interface=wg-nordvpn network=10.5.0.0
Step 4: Configure Firewall Rules
Allow Incoming WireGuard Connections
/ip firewall filter add action=accept chain=input comment="Allow WireGuard" dst-port=51820 protocol=udp
Allow Specific IPs to Use the VPN
First, define the list of IP addresses that are allowed to use the VPN:
/ip firewall address-list add address=192.168.1.0/24 list=access_vpn
Create a Firewall Rule to Allow Forwarding:
/ip firewall filter add action=accept chain=forward comment="Allow VPN Traffic" out-interface=wg-nordvpn src-address-list=access_vpn
Configure NAT for Outgoing Traffic
/ip firewall nat add action=masquerade chain=srcnat out-interface=wg-nordvpn
Step 5: Configure Routing and Implement a Kill Switch
Create a New Routing Table
/routing table add fib name="private_route"
Add Routing Rules
Route traffic from specific hosts through the VPN:
/routing rule add action=lookup-only-in-table src-address=192.168.1.100/32 table=private_route
Or route traffic to specific destinations (e.g., NordVPN DNS server):
/routing rule add action=lookup-only-in-table dst-address=103.86.96.100/32 table=private_route
Note: 103.86.96.100 is one of the DNS servers provided by NordVPN.
Configure Routes for the New Table
Add a blackhole route as a kill switch:
/ip route add blackhole distance=5 routing-table=private_route
Add a route through the WireGuard interface:
/ip route add distance=2 gateway=wg-nordvpn@main routing-table=private_route
Step 6: Test the Configuration
- Ensure that devices specified in the address list are routing traffic through the VPN.
- Verify that the kill switch works by disabling the WireGuard interface and checking if traffic from the specified devices is blocked.
Conclusion
That’s it! You should now have a working WireGuard VPN connection to NordVPN on your Mikrotik router, complete with policy routing and a kill switch. This setup allows you to route specific traffic through the VPN and ensures that if the VPN connection drops, traffic will not leak through your regular internet connection.
Let me know how it works for you or if you have any questions!