Setup Wireguard VPN on Mikrotik router
In this document, we will set up a Wireguard VPN on a MikroTik router and configure the tunnel for use with a specific IP only.
Please note: Enabling fasttrack along with Wireguard may cause slow requests to Wireguard. The current solution is to remove fasttrack rule.
Setup Wireguard VPN
Prepare Wireguard client file
The WireGuard client configuration file should be formatted as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
- PrivateKey: The private key of the client.
- Address: The IP address (with subnet) assigned to the client.
- DNS: The DNS servers to use.
- PublicKey: The public key of the server.
- PresharedKey: The preshared key for added security.
- AllowedIPs: Defines which IP addresses can be routed through the tunnel.
- Endpoint: The IP and port of the WireGuard server.
Create WireGuard Interface on MikroTik
1. Create the WireGuard Interface
In the Private Key
field, input the PrivateKey
from your client configuration file.
2. Add the Peer
Fill in the peer information using the details from the client configuration:
- Public Key: The server's public key.
- Endpoint: The IP address and port of the WireGuard server.
- Allowed IPs: Enter the address ranges allowed to use this tunnel (e.g., 0.0.0.0/0 for all traffic).
- Preshared Key: The preshared key (if used).
- Interface: The WireGuard interface created earlier.
Note: Leave the Private Key
empty
3. Create Address list
- Go to IP > Addresses.
- Create a new address list, the
Address
value can be obtained in the Wireguard client file,Interface
value is the Wireguard interface.
Routing for Wireguard traffic
1. Create a Routing Table
Go to Routing
-> Tables
2. Create Route
Go to IP
-> Routes
Add a new route with the following settings:
- Dst Address:
0.0.0.0/0
- Gateway: The Wireguard interface
- Routing Table: Add a new route with the following settings:
3. Create NAT rule
Go to IP
-> Firewall
-> select tab NAT
Create a new NAT rule with the following settings:
- Chain: srcnat
- Out. Interface: The WireGuard interface
- Action: masquarage
Note: Ensure this NAT rule is set as the second priority, below any existing PPPoE NAT rules.
4. Create Mangle rule
Create a mangle rule with the following concept: all packets destined for the target address will be routed to the WireGuard client. You can find the target IP address by using host google.com
or nslookup google.com
.
Mikrotik script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|