How To Set Up A MikroTik Router With NAT, A Firewall And WireGuard VPN (RouterOS 7)


How To Setup A MikroTik Router With NAT And VPN Access

This guide shows how to set up a MikroTik router running RouterOS 7 with internet access for your local network, 1:1 NAT for a server, a basic firewall and secure remote access with WireGuard VPN, which is built into RouterOS 7.

Each step gives the command to paste into a terminal (in Winbox, click New Terminal, or connect by SSH) and the Winbox menu where you can do the same thing.

The examples use these addresses. Replace them with your own:

WhatExample
Internet (WAN) portether1
Local network (LAN) bridgebridge
Public IP address of the router203.0.113.2/29
Gateway from your provider203.0.113.1
Extra public IP for a server203.0.113.3
Local network192.168.88.0/24, router 192.168.88.1
VPN network10.10.10.0/24, router 10.10.10.1

Before you start

Connect to the router on a LAN port with Winbox, using the default admin user. Then update RouterOS, so you have the latest security fixes:

/system package update check-for-updates
/system package update install

The router restarts. After it comes back, update the RouterBOARD firmware to match and restart again:

/system routerboard upgrade
/system reboot

In Winbox: System > Packages > Check For Updates, then System > RouterBOARD > Upgrade.

1. Secure the admin account

Create your own user with a strong password:

/user add name=yourname group=full password="a-long-unique-password"

Log out, log back in as the new user, and only then disable the default admin account:

/user disable admin

Give the router a name, so you can tell it apart from others:

/system identity set name=office-router

In Winbox: System > Users and System > Identity.

2. Set up the internet connection

Add the public IP address to the WAN port, the default route to your provider’s gateway, and DNS servers:

/ip address add address=203.0.113.2/29 interface=ether1 comment=WAN
/ip route add dst-address=0.0.0.0/0 gateway=203.0.113.1
/ip dns set servers=1.1.1.1,9.9.9.9 allow-remote-requests=yes

allow-remote-requests=yes lets computers on your network use the router for DNS. The firewall in step 5 stops the internet from using it.

If your provider gives you an address automatically, use /ip dhcp-client add interface=ether1 instead of the first two lines.

In Winbox: IP > Addresses, IP > Routes and IP > DNS.

3. Set up the local network

Most MikroTik routers already have a bridge with the LAN ports, the address 192.168.88.1/24 and a DHCP server. If yours does not, or you have reset it without the default configuration:

/interface bridge add name=bridge
/interface bridge port add bridge=bridge interface=ether2
/ip address add address=192.168.88.1/24 interface=bridge comment=LAN
/ip pool add name=lan-pool ranges=192.168.88.100-192.168.88.200
/ip dhcp-server add name=lan-dhcp interface=bridge address-pool=lan-pool
/ip dhcp-server network add address=192.168.88.0/24 gateway=192.168.88.1 dns-server=192.168.88.1

Add a bridge port line for each LAN port you use.

In Winbox: Bridge, IP > Addresses and IP > DHCP Server > DHCP Setup.

4. NAT

Internet access for the local network

So that computers on the LAN can reach the internet through the router’s public address:

/ip firewall nat add chain=srcnat out-interface=ether1 action=masquerade comment="LAN to internet"

1:1 NAT for a server

To give a server on the LAN (here 192.168.88.10) its own public address (203.0.113.3), add the address to the WAN port and map it both ways:

/ip address add address=203.0.113.3/29 interface=ether1 comment="Server public IP"
/ip firewall nat add chain=dstnat dst-address=203.0.113.3 action=dst-nat to-addresses=192.168.88.10 comment="1:1 NAT in"
/ip firewall nat add chain=srcnat src-address=192.168.88.10 out-interface=ether1 action=src-nat to-addresses=203.0.113.3 comment="1:1 NAT out"

Put the two 1:1 NAT rules above the masquerade rule (drag them up in IP > Firewall > NAT), so the server uses its own address and not the router’s.

5. Firewall

If your router still has MikroTik’s default configuration, it already has a firewall (rules marked defconf). Compare it with the rules below and change it, rather than adding a second set. The rules below assume an empty filter list.

These rules protect the router itself (the input chain) and the network behind it (the forward chain). Anything from the internet that is not a reply, a VPN connection or a port you have opened is dropped. Add them in this order:

/ip firewall filter
add chain=input connection-state=established,related,untracked action=accept comment="Allow replies"
add chain=input connection-state=invalid action=drop comment="Drop invalid"
add chain=input protocol=icmp action=accept comment="Allow ping"
add chain=input protocol=udp dst-port=13231 action=accept comment="Allow WireGuard"
add chain=input in-interface=!ether1 action=accept comment="Allow from LAN and VPN"
add chain=input action=drop comment="Drop everything else"
add chain=forward connection-state=established,related,untracked action=accept comment="Allow replies"
add chain=forward connection-state=invalid action=drop comment="Drop invalid"
add chain=forward in-interface=ether1 connection-nat-state=!dstnat action=drop comment="Drop internet traffic not sent to a server"

The last rule still lets in traffic that a NAT rule sends to a server. If the server from step 4 should only be reachable on some ports, add rules for it, for example for a web server:

/ip firewall filter add chain=forward in-interface=ether1 dst-address=192.168.88.10 protocol=tcp dst-port=80,443 action=accept comment="Web server"
/ip firewall filter add chain=forward in-interface=ether1 dst-address=192.168.88.10 action=drop comment="Nothing else to the server"

New rules are added at the end of the list, and the firewall reads the list from the top, so move these two above the “Drop internet traffic not sent to a server” rule: drag them in Winbox, or use /ip firewall filter move with the numbers shown by /ip firewall filter print.

Finally, turn off the router services you do not use, and only allow Winbox and SSH from the LAN and the VPN:

/ip service disable telnet,ftp,www,api,api-ssl
/ip service set winbox address=192.168.88.0/24,10.10.10.0/24
/ip service set ssh address=192.168.88.0/24,10.10.10.0/24

In Winbox: IP > Firewall > Filter Rules and IP > Services.

6. WireGuard VPN

WireGuard gives you fast, secure access to your network from a laptop or phone. Create the WireGuard interface and give it an address:

/interface wireguard add name=wg-vpn listen-port=13231
/ip address add address=10.10.10.1/24 interface=wg-vpn comment=VPN
/interface wireguard print

The last command shows the router’s public key; you need it for each device.

Add a device

Install the WireGuard app on the device (from wireguard.com, the App Store or Google Play) and create an empty tunnel. The app generates a key pair; copy its public key. Then add the device to the router, giving each device its own VPN address:

/interface wireguard peers add interface=wg-vpn comment=laptop public-key="DEVICE-PUBLIC-KEY" allowed-address=10.10.10.2/32

On the device, complete the tunnel like this:

[Interface]
PrivateKey = (created by the app)
Address = 10.10.10.2/32
DNS = 192.168.88.1

[Peer]
PublicKey = ROUTER-PUBLIC-KEY
Endpoint = 203.0.113.2:13231
AllowedIPs = 192.168.88.0/24, 10.10.10.0/24
PersistentKeepalive = 25

With these AllowedIPs, only traffic to your office network goes through the VPN. To send all internet traffic through the office as well, use AllowedIPs = 0.0.0.0/0 and add a masquerade rule for the VPN network:

/ip firewall nat add chain=srcnat src-address=10.10.10.0/24 out-interface=ether1 action=masquerade comment="VPN to internet"

Connect, and check on the router that the peer shows a recent handshake:

/interface wireguard peers print detail

In Winbox: WireGuard > WireGuard and WireGuard > Peers.

7. Back up the configuration

Once everything works, save a backup and an export of the configuration, and copy both off the router (Files in Winbox):

/system backup save name=office-router
/export file=office-router

The .backup file restores the whole router, including passwords. Keep it safe. The .rsc export is plain text, so you can read it and reuse parts of it.

Need help?

If you run MikroTik equipment in front of servers you host with us, or want help setting up a VPN to your VPS or dedicated server, submit a ticket and our team will help.