Generate SSH public/private keys on Ubuntu 24.04 and upload to Mikrotik without password

SSH key authentication replaces the password prompt with a cryptographic key pair. Instead of typing a password every time you connect to a router, the client proves its identity with a private key that never leaves your machine.


In this guide, we will generate an SSH key pair on Ubuntu 24.04, upload the public key to a MikroTik router, import it for a RouterOS user and finally disable password authentication so that only key-based logins are accepted.


Requirements


Before starting, make sure you have a machine running Ubuntu 24.04 with the OpenSSH client installed, and a MikroTik router running RouterOS 6 or RouterOS 7 that is reachable over the network.


You will also need a RouterOS user account with the ssh, read and write policies. In this guide the user is nocnmt and the router address is 192.168.11.1.


Check that the SSH client is available on Ubuntu:

ssh -V


If the command is not found, install the client package:

sudo apt update && sudo apt install openssh-client -y


Generate an SSH Key Pair on Ubuntu


Generate a 4096-bit RSA key pair with ssh-keygen. RSA is the safest choice because it is supported by every RouterOS version, including RouterOS 6:

ssh-keygen -t rsa -b 4096


The utility asks where to store the key and whether to protect it with a passphrase. Press Enter to accept the default path and leave the passphrase empty for a fully unattended login:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:2YJoflACBukJ6Jh66iYPoaV1ym1hmbHkx0529VdfEz4 root@ubuntu-vlab
The key's randomart image is:
+---[RSA 4096]----+
|ooo . |
|+. . . .|
|=.. + . . Eo|
|o+ o X . + . .=|
|o o % * S . . . .|
|o*.B B . . . |
|+oo + o |
|oo . . |
|+o. |
+----[SHA256]-----+


A passphrase adds a second layer of protection in case the private key file is ever stolen. It has to be entered on every connection unless ssh-agent is used, so for automation and scripted backups an empty passphrase is the usual choice.


The key was generated as root in the example above, which means it is stored in /root/.ssh. If you generate the key as a regular user, the files are placed in the home directory of that user and only that user will be able to use them.


Using an Ed25519 Key Instead


RouterOS 7 also accepts Ed25519 keys. They are shorter, faster and considered the modern default:

ssh-keygen -t ed25519 -C "nocnmt@ubuntu-vlab"


The resulting files are named id_ed25519 and id_ed25519.pub. Use these names instead of id_rsa in the commands that follow. If your router still runs RouterOS 6, stay with the RSA key.


Review the Generated Key Files


List the contents of the .ssh directory:

ls -l ~/.ssh


Two files were created. id_rsa is the private key: it stays on the Ubuntu machine and must never be copied to the router or shared with anyone. id_rsa.pub is the public key, and this is the file that gets uploaded to the MikroTik router.


Display the public key to confirm it is a single line starting with ssh-rsa:

cat ~/.ssh/id_rsa.pub


Verify that the permissions are correct. OpenSSH refuses to use a private key that is readable by other users:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub


Upload the Public Key to the MikroTik Router


Copy the public key to the root of the router file system with scp. This is the last time a password will be requested:

scp ~/.ssh/id_rsa.pub nocnmt@192.168.11.1:/


Ubuntu 24.04 ships with OpenSSH 9.x, where scp uses the SFTP protocol by default. RouterOS 6 does not provide an SFTP subsystem, so the transfer may fail with subsystem request failed on channel 0. In that case force the legacy SCP protocol with the -O option:

scp -O ~/.ssh/id_rsa.pub nocnmt@192.168.11.1:/


As an alternative, the file can be dragged into the Files window in Winbox, which produces exactly the same result.


Log in to the router and confirm that the file has arrived:

/file print


Import the Public Key in RouterOS


Still on the router, import the uploaded file and bind it to the user account:

/user ssh-keys import public-key-file=id_rsa.pub user=nocnmt


The user parameter is the RouterOS user the key belongs to. A key imported for one user will not authenticate any other user, so repeat the import for every account that needs key-based access.


Verify the imported key:

/user ssh-keys print


The output lists the user, the key type and the key bit count. Once the key is stored in the configuration, the uploaded file is no longer needed and can be removed:

/file remove id_rsa.pub


A key that was imported by mistake can be deleted by its number:

/user ssh-keys remove 0


Test the Passwordless Login


Back on the Ubuntu machine, connect to the router:

ssh nocnmt@192.168.11.1


The RouterOS console should open without a password prompt. If the key is stored under a non-default name, point to it explicitly:

ssh -i ~/.ssh/id_rsa nocnmt@192.168.11.1


Single commands can now be executed remotely without any interaction, which is what makes scripted backups and monitoring possible:

ssh nocnmt@192.168.11.1 "/system resource print"


If the login still asks for a password, run the client in verbose mode to see which key was offered and how the router answered:

ssh -v nocnmt@192.168.11.1


Create an SSH Config Entry (Optional)


When several routers are managed from the same machine, an entry in ~/.ssh/config saves typing the user, address and key on every connection:

nano ~/.ssh/config


Add a host block for the router:

Host mikrotik-core
HostName 192.168.11.1
User nocnmt
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes


Set the permissions required by OpenSSH:

chmod 600 ~/.ssh/config


The router can now be reached by its alias:

ssh mikrotik-core


Disable Password Authentication on the Router


As long as password login remains enabled, the router is still exposed to brute-force attempts. After confirming that the key works, force RouterOS to accept public key authentication only:

/ip ssh set always-allow-password-login=no


It is also worth enabling the stronger cipher and key exchange set, which disables the legacy algorithms in the SSH daemon:

/ip ssh set strong-crypto=yes


Review the current SSH service settings:

/ip ssh print


Important: keep the current session open and verify the key login from a second terminal before closing it. If the key is not working and password login has already been disabled, the only remaining ways in are Winbox, the web interface or a serial console.


Access to the SSH service should also be limited to trusted networks. Restrict the service to a management subnet:

/ip service set ssh address=192.168.11.0/24


Changing the default port reduces the noise from automated scanners:

/ip service set ssh port=2200


Troubleshooting


Permission denied (publickey) - the key was imported for a different RouterOS user, or the client is connecting with a different key. Compare the user name in the connection string with the output of /user ssh-keys print.


The router still asks for a password - the client did not offer the key. Check the output of ssh -v and confirm that the private key exists in the home directory of the user running the command. A key generated as root is not visible to a regular user.


subsystem request failed on channel 0 - OpenSSH 9 tried to use SFTP against a RouterOS 6 device. Repeat the transfer with scp -O.


no matching host key type found - the router offers an algorithm that modern OpenSSH no longer enables by default. Connect once with the algorithm added explicitly and plan a RouterOS upgrade:

ssh -o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedAlgorithms=+ssh-rsa nocnmt@192.168.11.1


Bad permissions / UNPROTECTED PRIVATE KEY FILE - the private key is readable by other users. Restore the permissions with chmod 600 ~/.ssh/id_rsa.


WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED - the router was reinstalled or replaced and its host key is different. Remove the stale entry:

ssh-keygen -R 192.168.11.1


Conclusion


The Ubuntu machine now authenticates to the MikroTik router with an SSH key instead of a password, and the router no longer accepts password logins at all.

The same key pair can be imported on every router in the network, which makes automated configuration backups, scripted changes and monitoring possible without storing a single password in a script.