1. Routers with MikroTik and Alpine Linux¶
In this exercise, two router templates will be prepared: one for MikroTik and another for Alpine Linux. These templates will be used for the following scenario:
xxxx x xxx
xxx xx
xxxxxxxx xx
┌───────────┐ xx x
│ ROUTER │ xx x
│ default ├───────────xx Internet x
│ isard │ xx x
└─────┬─────┘ xxxxxx x
│.1 x xx x x x x x x
│ default network
──┬────────────────┴────────────────────────────
│ 192.168.1.0/24
│
dhcp │.x.y
┌──────┴────────────┐
│ inet │
│ │
│ DESKTOP │dhcp wireguard-vpn network
│ ROUTER ISARD vpn├───────────────────────────────────────┬────────
│ │.X.Y 10.2.0.0/16 │
│ lan1 │ │.0.1
└──────┬────────────┘ ┌───────┴────────┐
│.1 │ FIREWALL │
│ personal network 1 │ ISARD │
───┴────────┬────────────────────── └──────┬─────────┘
│ 192.168.88.0/24 │
dhcp│.X │
┌──────┴──────┐ ┌┴┐
│ eth0 │ │V│
│ │ │P│
│ DESKTOP │ │N│
│ │ └┬┘
│ Internet │ │10.0.X.Y
│ access via │ ┌──────────┴──────────┐
│ personal │ │ wireguard │
│ network │ │ │
│ │ │ MY PC │
│ configure │ │ with wireguard file │
│ MikroTik │ │ of the isard user │
└─────────────┘ │ │
└─────────────────────┘
MikroTik RouterOS in IsardVDI¶
A desktop with the MikroTik RouterOS operating system is needed to give Internet access to a desktop connected to the personal network. This router will perform NAT "MASQUERADE" towards the Default network (Internet exit). This means that it will replace the source IP of the internal devices with its own public IP, which lets them browse the Internet. It will also include a DHCP server. It can be reached through the personal VPN to configure it more comfortably from your own computer.
This configuration can serve as a basis for other, more complex network practices detailed in the following sections.
Create the desktop with base RouterOS by cloning the disk from an image¶
Create a virtual desktop from the SystemRescue CD ISO (from Media → Create new desktop).
Boot the Rescue CD system.
Once inside the desktop, make these configurations:
# Change the keyboard to Spanish
setkmap es
# Remove Firewall rules and set the default policy to ACCEPT
# iptables -X: Removes all custom rule chains except the default ones.
# iptables -F: Clears all rules within all chains.
# iptables -P INPUT ACCEPT: Sets the default policy for the INPUT chain.
# All incoming connections will be accepted by default
iptables -X; iptables -F; iptables -P INPUT ACCEPT
Check that no firewall rules remain and that the SSH service is listening:
iptables-save
ss -tlnp
# The expected output of these commands is:
[root@sysrescue ~]# iptables-save
*filter
:INPUT ACCEPT [78:13518]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [172:18618]
:LOGDROP - [0:0]
COMMIT
[root@sysrescue ~]# ss -tlnp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=375,fd=3))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=375,fd=4))
Change the root password:
SSH connection from your own computer
To enter the system from your own computer, SSH is enough, because it has no graphical interface. The user VPN tunnel must be established: User VPN
The MikroTik website has the download links for all versions: https://mikrotik.com/download/archive
Look for the latest stable image in img format. It is important that it has this format: chr-VERSION-img.zip. Currently, the latest stable version is: chr-7.18.2.img.zip.
The download URL for this version is: chr-7.18.2.img.zip
The same download website gives the sha256 signature of each file (different for each file and version), which is used to check that the file that will be unzipped later is authentic. For the file and version of this exercise, the signature is: 5452ab6b298458a3e266e2cdd4cf664cddbe44f5861991843d5924e3fa4d576f
With the following command, check that there are 4 GB free in the temporary directory to download the image:
Download the file to the /tmp directory and verify its signature:
wget -O /tmp/routeros.img.zip https://download.mikrotik.com/routeros/7.18.2/chr-7.18.2.img.zip
sha256sum /tmp/routeros.img.zip
Once the signature has been verified as valid, unzip the content:
This unzips a file with a 128M disk image:
[root@sysrescue /tmp]# ls -lh /tmp/*.img
-rw-r--r-- 1 root root 128M Apr 17 13.50 /tmp/chr-7.18.2.img
Next, clone this image to the virtual disk /dev/vda:
[root@sysrescue /tmp]# hdparm /dev/vda
/dev/vda:
readonly = 0 (off)
readahead = 256 (on)
geometry = 41610/16/63, sectors = 41943040, start = 0
To do this, the dd tool is used:
# if: input file, in this case the disk image file.
# of: output file, in this case it is a virtual disk device.
# bs: read and write block size, for large files it speeds up writing compared to the default small blocks.
# status: indicates that we want to see the progress, useful if the file were very large; in our case, it will be almost instantaneous.
dd if=/tmp/chr-7.18.2.img of=/dev/vda bs=1M status=progress
Now the virtual desktop can be shut down with:
Next, edit the desktop to prepare it as a template, with these parameters:
- vCPUs: the 2 vCPUs set before can be kept.
- Memory (GB): reduce it to 1.
- Boot: change from CD/DVD to Hard Disk.
- Networks: they can be added or changed depending on what should be fixed in the template; afterwards, the students can change them when creating the desktop. It is common for the first interface to be the Internet exit (Default), the second the VPN access (Wireguard VPN) and the third the personal network (Personal1).
- Remove the ISO of SystemRescue CD.
- Optional: add an image to the desktop card.
Once the hardware changes are made, create a template from the desktop with the name routeros 7.18.2.
The desktop is now based on this new template and you can keep working with it.
Initial MikroTik configuration¶
When the desktop is started, it boots with the MikroTik image and gives access to the command line, as if it were the first connection to the router via Telnet or the serial port. An output like the one in the following image should appear on the screen:
Log in with:
-
Login: admin
-
Password: (press Enter, it has no password yet)
First it asks you to set a password; in this case, the usual template password. Then the MikroTik prompt appears:
Check that it has Internet access:
Prepare the desktop to connect to it via SSH from your own computer
To enter the system from your own computer, SSH is enough, because it has no graphical interface. The user VPN tunnel must be established: User VPN
Before continuing, it is convenient to work with a terminal where commands can be copied and pasted. Since it has no graphical interface, the viewer does not allow the clipboard to be used between the virtual desktop and your own computer.
To do this, enable the Wireguard VPN interface and tell the router that this interface must obtain a dynamic IP address via DHCP. The Wireguard VPN network gives a dynamic IP, but without a default gateway.
Check the active interfaces and their names:
Check that the second interface, the Wireguard VPN one, is named ether2 (it depends on the position of the interface in the virtual desktop edit form).
Configure the IP address via DHCP and wait for it to obtain it:
Check that the IPs and routes are correct. Routing rules have been added so that it can be reached from your own computer through the personal VPN:
In this case, the IP on the ether2 interface is 10.2.239.24.
Now it should be possible to ping it and access it via SSH from your own computer.
Since there was already an SSH connection to this same IP before (to the SystemRescue CD), a key conflict will probably appear in ~/.ssh/known_hosts, which is resolved by deleting the previous key:
To access, use the username admin.
The result should be something similar to:
➜ ~ ssh admin@10.2.239.24
The authenticity of host '10.2.239.24 (10.2.239.24)' can't be established.
RSA key fingerprint is SHA256:+xpN6EIEw3LTfCzrmH7VWOwGN1cRqCH4RhaB8.....
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.2.239.24' (RSA) to the list of known hosts.
admin@10.2.239.24's password:
MMM MMM KKK TTTTTTTTTTT KKK
MMMM MMMM KKK TTTTTTTTTTT KKK
MMM MMMM MMM III KKK KKK RRRRRR OOOOOO TTT III KKK KKK
MMM MM MMM III KKKKK RRR RRR OOO OOO TTT III KKKKK
MMM MMM III KKK KKK RRRRRR OOO OOO TTT III KKK KKK
MMM MMM III KKK KKK RRR RRR OOOOOO TTT III KKK KKK
MikroTik RouterOS 7.14.3 (c) 1999-2024 https://www.mikrotik.com/
Press F1 for help
[admin@MikroTik] >
There is now access to the command line through Wireguard VPN.
Configure internal network with DHCP server and VPN¶
At this point, an internal LAN network is configured where the router acts as a DHCP server and masquerades the Internet exit.
The steps are:
-
Group the interfaces into WAN and LAN. ether2 and ether3 belong to the internal/personal network (LAN). And ether1 to the external network, the exit to the Internet (WAN).
# INTERFACES LIST /interface list add comment=defconf name=WAN add comment=defconf name=LAN /interface list member add comment=defconf interface=ether3 list=LAN # Interfaz Personal 1 add comment=defconf interface=ether2 list=LAN # Interfaz WireguardVPN add comment=defconf interface=ether1 list=WAN # Interfaz Default/Salida a internet. # NEIGHBOR DISCOVER # Apply this command so that hosts on the LAN network can see each other. /ip neighbor discovery-settings set discover-interface-list=LAN -
Assign a fixed IP to the ether3 interface (the Personal1 interface).
-
DHCP server:
-
Create an IP address range (pool).
-
Create a DHCP server with this range.
-
Add the configuration to the DHCP server.
-
-
DNS server:
- Create a DNS server that forwards requests to an external DNS server.
# DNS SERVER # Allow devices on the network to use the router as a DNS server, and define the Google DNS server as the external server. /ip dns set allow-remote-requests=yes servers=8.8.8.8 # Assign the domain name "router.lan" for the router's IP. /ip dns static add address=192.168.88.1 comment=defconf name=router.lan
- Create a DNS server that forwards requests to an external DNS server.
-
Configure the firewall to masquerade the Internet exit from the internal network.
-
Restrict access and allow incoming connections by default only from the internal network (LAN).
# FIREWALL /ip firewall filter add action=accept chain=input comment="defconf: accept established,related,untracked" \ connection-state=established,related,untracked add action=drop chain=input comment="defconf: drop invalid" connection-state=invalid add action=accept chain=input comment="defconf: accept ICMP" protocol=icmp add action=accept chain=input comment="defconf: accept to local loopback (for CAPsMAN)" dst-address=\ 127.0.0.1 add action=drop chain=input comment="defconf: drop all not coming from LAN" in-interface-list=!LAN add action=accept chain=forward comment="defconf: accept in ipec policy" ipec-policy=in,ipec add action=accept chain=forward comment="defconf: accept out ipec policy" ipec-policy=out,ipec add action=fasttrack-connection chain=forward comment="defconf: fasttrack" connection-state=\ established,related hw-offload=yes add action=accept chain=forward comment="defconf: accept established,related, untracked" \ connection-state=established,related,untracked add action=drop chain=forward comment="defconf: drop invalid" connection-state=invalid add action=drop chain=forward comment="defconf: drop all from WAN not DSTNATed" connection-nat-state=\ !dstnat connection-state=new in-interface-list=WAN -
Change the router name.
-
Adjust the clock and time zone.
-
Rename the interfaces:
-
ether1: inet (internet Default network)
- ether2: vpnisard (Wireguard VPN network, to connect to it with the IsardVDI user's VPN file)
-
ether3: lan1 (Personal1 network)
Finally, make a backup of the current configuration so that it can be recovered:
The router can be shut down from inside with this command:
Prepare the router configuration as a template¶
Making this configuration work as a template is not as simple as with a Windows or Linux operating system.
RouterOS associates the configuration of each interface with its MAC address. When the disk is cloned, new MAC addresses are generated, so the configuration assigned to the interfaces is unlinked and the template does not work for other desktops.
Each desktop derived from a template is created with a different MAC address on each card; that is why some extra steps are needed to prepare the template.
Save the configuration as a command list:
Copy the file locally from your own computer or from a Linux virtual desktop:
Modify the inicial.rsc file by adding remove [find] to the DHCP client configuration part, since one is created automatically by default. The DHCP server configuration section of the inicial.rsc file contains:
Change this part to:
All that remains is to upload the file to the router again and overwrite it (using the same name is enough):
Inside the router, you can see the list of files and check that the file has been uploaded correctly by looking at the LAST-MODIFIED column (date and time of the last modification):
Finally, tell the router that, when it starts for the first time, it must import the commands from the inicial.rsc file.
It restarts; log in with the user admin / [Enter] (no password), as the first time.
Lastly, repeat the configuration reset, this time so that it shuts down and the current state of the router's disk can be cloned into a template.
The desktop shuts down and you can now create the template with the name router 7.14 inet-vpn-lan.
To check that everything works, create a new test desktop based on this template, because the current one keeps the same MAC addresses and will always work.
Create a desktop named prova based on the "router 7.14 inet-vpn-lan" template and check that the router configuration takes effect on the client. If the result is satisfactory, this template is ready to be used in the exercises.
Client on the internal network¶
Create a desktop for the client:
- Template: Vitalinux, Ubuntu, Windows (any of your preference)
- Networks: only one Personal1 network
With the SPICE viewer you can copy and paste into the desktop through the clipboard.
From inside the client desktop (in this case, Vitalinux), check the following:
-
The first interface (Personal1) acquires IP via DHCP.
ip -c a s ens3 # Resultat esperat isard@vitalinux:~/Desktop$ ip -c a s ens3 2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:75:d3:0b brd ff:ff:ff:ff:ff:ff inet 192.168.88.254/24 brd 192.168.88.255 scope global dynamic noprefixroute ens3 valid_lft 1581sec preferred_lft 1581sec inet6 fe80::a8d3:848f:4f11:3ca8/64 scope link noprefixroute valid_lft forever preferred_lft forever -
Internet connectivity, with ping to 8.8.8.8.
ping -c 1 8.8.8.8 # Resultat esperat isard@vitalinux:~/Desktop$ ping -c 1 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=15.0 ms --- 8.8.8.8 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 15.017/15.017/15.017/0.000 ms -
The DNS configuration is correct.
-
That you can ping a domain name.
ping -c 1 www.google.es # Resultat esperat isard@vitalinux:~/Desktop$ ping -c 1 www.google.es PING www.google.es (203.0.113.10) 56(84) bytes of data. 64 bytes from 203.0.113.10: icmp_seq=1 ttl=116 time=19.3 ms --- www.google.es ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 19.352/19.352/19.352/0.000 ms
Use Winbox (graphical tool) to configure the MikroTik from Linux¶
From a client connected to the internal LAN network, the Winbox tool can be used.
To use Winbox on Linux distributions, the Wine package is needed, which lets certain applications compiled for Windows run on Linux. On some Linux distributions it may already be installed.
Winbox can be downloaded from the MikroTik website or from the terminal:
It is started with the command:
The first time Wine starts, messages asking to install additional elements may appear. They can be ignored, as they are not needed for the Winbox tool to work.
A screen like the one in the image appears; to log in, fill in the fields with the router's IP and the password:
Additional configurations for the Winbox experience
Download the desktop icon:
wget -O ~/winbox_icon.png https://github.com/juanchixd/Mikrotik-linux/blob/main/icons/winbox-128x128.png?raw=true
Create a shortcut on the desktop:
cat <<'EOF' > ~/Desktop/Winbox_Mikrotik.desktop
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=Open Desktop
Name[en]=Winbox Mikrotik
Name[es]=Winbox Mikrotik
Name[es_ES]=Winbox Mikrotik
Exec=sh -c "wine /home/isard/winbox.exe"
Categories=;
Type=Application
Terminal=false
Icon=/home/isard/winbox_icon.png
EOF
Copy it to the autostart directory so that it runs on startup:
This client desktop can be kept by creating a template with the name "client Winbox Vitalinux".







