Introduction

This guide simplifies the process of installing MikroTik Router OS on a VPS. By automating the installation with a script, there’s no need to manually enter commands in the MikroTik CLI console. Once complete, you’ll have a fully operational VPS running MikroTik Router OS.

Getting Started: Prerequisites and VPS Preparation

  1. Install Ubuntu 22.04 on VPS

  2. Once installed, turn off the VPS.

  3. Access the KVM Settings in the server management interface:

    • Change the Disk Driver to IDE.

    • Set the Network Driver to E100.

  4. Apply the changes and restart the VPS.

Running the Installation Script

Create the Script File

Connect to the server via SSH

Open a terminal and create a script file:

nano mikrotik.sh

Copy and paste the following script into the file

#!/bin/bash
export PATH=$PATH:/usr/bin:/bin

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get -y install gzip

# Retrieve the latest version dynamically
latest_version=$(curl -s https://mikrotik.com/download | grep -oP 'chr-\K[0-9]+\.[0-9]+\.[0-9]+(?=\.img\.zip)' | sort -V | tail -n 1)

# Construct the download URL for the latest version
download_url="https://download.mikrotik.com/routeros/$latest_version/chr-$latest_version.img.zip"

# Download and extract the image file
wget "$download_url" -O chr.img.zip
gunzip -c chr.img.zip > chr.img && \

kpartx -av chr.img
mount /dev/mapper/loop3p2 /mnt/ && \

# Find the primary network interface with an IP address
interface=$(ip -o -4 addr show up primary scope global | awk '{print $2}' | head -n 1)

# Retrieve network information
ADDRESS=$(ip addr show $interface | grep global | cut -d' ' -f 6 | head -n 1)
GATEWAY=$(ip route list | grep default | cut -d' ' -f 3)

# Create autorun script
echo "/ip address add address=$ADDRESS interface=[/interface ethernet find where name=ether1]
/ip route add gateway=$GATEWAY
/ip service disable telnet" > /mnt/rw/autorun.scr && \

# Unmount and clean up
sudo umount /mnt/ && \

# Sync and write image to disk
echo u > /proc/sysrq-trigger && \
sudo dd if=chr.img bs=1024 of=/dev/sda && \
echo "sync disk" && \
echo s > /proc/sysrq-trigger && \
echo "Waiting 5 seconds..." && \
#Waiting
end_time=$((SECONDS + 5))
while [ $SECONDS -lt $end_time ]; do
    :
done && \

echo "Reboot OS" && \
echo b > /proc/sysrq-trigger

Press CTRL+O to save your changes, then exit by pressing CTRL+X.

Set Script Permissions

Set the script execution rights:

chmod +x mikrotik.sh
chmod 755 mikrotik.sh

Run the Script

./mikrotik.sh

The VPS will reboot automatically once the installation completes.

Connecting to Server Using Winbox

After the VPS reboots, connect to MikroTik Router OS using Winbox:

  1. Download and Install Winbox from the MikroTik website.

  2. Open Winbox and create a new connection:

    • Enter your VPS IP address.

    • Use admin for the Login field and leave the Password field blank.

    • Click Connect.

  3. During the first login, you’ll be prompted to set a new password for added security.

Congratulations! MikroTik Router OS is now successfully installed and operational on your VPS. With the automated script, accessing and managing Mikrotik CHR via Winbox is quick and efficient. After securing your credentials and testing the setup, your infrastructure setup is ready for seamless network management.