Skip to content
MikroTik RouterOS Docs

SSH (Secure Shell)

For the impatient: secure SSH with key-based authentication.

On your workstation:

Terminal window
ssh-keygen -t rsa -b 4096 -f ~/.ssh/mikrotik_rsa
scp ~/.ssh/mikrotik_rsa.pub [email protected]:

On the router:

/user/ssh-keys/import public-key-file=mikrotik_rsa.pub user=admin
/ip/ssh/set always-allow-password-login=no

Now connect without password:

Terminal window
ssh -i ~/.ssh/mikrotik_rsa [email protected]

What this does: SSH provides encrypted remote access to RouterOS via command line. It supports password and public key authentication, with options to enforce key-only access for enhanced security.

When to use this:

  • Secure remote administration
  • Automated scripting and configuration management
  • Replacing insecure Telnet access
  • Passwordless authentication for scripts/automation

Prerequisites:

  • SSH service enabled (default: yes, port 22)
  • Network access to router on TCP port 22
  • For key auth: SSH key pair generated on client
/ip/ssh/print

Default output:

forwarding-enabled: no
host-key-size: 2048
strong-crypto: no
always-allow-password-login: no

Use stronger encryption and larger DH primes:

/ip/ssh/set strong-crypto=yes

This enables:

  • AES-256 and AES-192 (vs AES-128)
  • Stronger HMAC algorithms
  • Larger Diffie-Hellman primes

Move SSH to a non-standard port:

/ip/service/set ssh port=2222

Allow SSH only from specific IP ranges:

/ip/service/set ssh address=192.168.1.0/24,10.0.0.0/8

Configuration Steps: Key-Based Authentication

Section titled “Configuration Steps: Key-Based Authentication”

Linux/macOS:

Terminal window
ssh-keygen -t rsa -b 4096 -f ~/.ssh/mikrotik_rsa -N ""

For RouterOS 7.12+ (Ed25519 preferred):

Terminal window
ssh-keygen -t ed25519 -f ~/.ssh/mikrotik_ed25519 -N ""

Using SCP:

Terminal window
scp ~/.ssh/mikrotik_rsa.pub [email protected]:

Or using SSH:

Terminal window
cat ~/.ssh/mikrotik_rsa.pub | ssh [email protected] "tee mikrotik_rsa.pub"
/user/ssh-keys/import public-key-file=mikrotik_rsa.pub user=admin
/user/ssh-keys/print

Expected output:

Columns: USER, BITS, KEY-OWNER
# USER BITS KEY-OWNER
0 admin 4096 user@workstation

Enforce key-only authentication:

/ip/ssh/set always-allow-password-login=no
Terminal window
ssh -i ~/.ssh/mikrotik_rsa [email protected]

Scenario: SSH from Router to Another Device

Section titled “Scenario: SSH from Router to Another Device”

Use the router as an SSH client:

/system/ssh 192.168.1.100 user=admin

Execute a single command:

/system/ssh 192.168.1.100 user=admin command="/system/identity/print"

Scenario: SSH in Scripts (Non-Interactive)

Section titled “Scenario: SSH in Scripts (Non-Interactive)”

For automation, use /system/ssh-exec:

/system/ssh-exec address=192.168.1.100 user=admin command="/ip/address/print"

Returns exit-code and output for script processing.

Use SSH key authentication for automation. Never put passwords in scripts.

Generate new host keys (invalidates existing keys):

/ip/ssh/regenerate-host-key

Export host keys for backup or transfer:

/ip/ssh/export-host-key key-file-prefix=router1

Creates router1_rsa (private) and router1_rsa.pub (public) files.

Restore previously exported host keys:

/ip/ssh/import-host-key private-key-file=router1_rsa

Scenario: Configure SSH Client (Modern OpenSSH + RouterOS 6.x)

Section titled “Scenario: Configure SSH Client (Modern OpenSSH + RouterOS 6.x)”

If using RouterOS 6.x with OpenSSH 8.8+, add to ~/.ssh/config:

Host 192.168.88.1
PubkeyAcceptedAlgorithms +ssh-rsa
HostKeyAlgorithms +ssh-rsa

This enables RSA support deprecated in modern OpenSSH.

Allow SSH tunneling:

/ip/ssh/set forwarding-enabled=both

Options:

  • no: Disabled (default)
  • local: Local forwarding only
  • remote: Remote forwarding only
  • both: Both directions

Scenario: Multiple Keys for Different Users

Section titled “Scenario: Multiple Keys for Different Users”

Import keys for multiple users:

/user/ssh-keys/import public-key-file=admin_key.pub user=admin
/user/ssh-keys/import public-key-file=backup_key.pub user=backup

Each user can have their own key.

Confirm SSH is properly configured:

/ip/service/print where name=ssh

Expected: disabled: no, correct port and address restrictions.

/ip/ssh/print

Expected: strong-crypto: yes, always-allow-password-login: no for secure config.

/user/ssh-keys/print

Expected: Keys listed for appropriate users.

From workstation:

Terminal window
ssh -v -i ~/.ssh/mikrotik_rsa [email protected]

Look for “Authenticated” in verbose output.

SymptomCauseSolution
”Permission denied (publickey)“Key not imported or wrong userVerify key imported for correct user
Password prompt despite keyalways-allow-password-login=yesSet to no; re-import key
”no matching host key type found”OpenSSH 8.8+ with RouterOS 6.xAdd HostKeyAlgorithms +ssh-rsa to SSH config
”no matching key exchange method”Algorithm mismatchEnable strong-crypto=yes or adjust client config
Connection refusedSSH service disabled or firewalledEnable service; check firewall rules
Key import failsWrong key formatUse OpenSSH format; ensure no extra whitespace
Can’t connect after key importWrong key file usedVerify using correct private key with -i flag
/ip/service/print where name=ssh

Ensure disabled: no and port is correct.

Terminal window
ssh -vvv -i ~/.ssh/mikrotik_rsa [email protected]

Shows detailed authentication process.

Public key should start with ssh-rsa or ssh-ed25519:

Terminal window
cat ~/.ssh/mikrotik_rsa.pub

Common Mistakes

  • Importing private key instead of public - Only .pub file should be imported
  • Wrong user specified during import - Key only works for the specified user
  • Key file permissions too open - Private key must be chmod 600
  • Forgetting -i flag - Specify key with ssh -i ~/.ssh/keyfile
  • RouterOS 6.x with modern OpenSSH - Requires client-side algorithm configuration
  1. Use key-based authentication - Disable password login after importing keys
  2. Enable strong-crypto - Use stronger encryption algorithms
  3. Change default port - Reduces automated brute-force attempts
  4. Restrict by IP - Allow SSH only from management networks
  5. Use Ed25519 keys - Faster and more secure than RSA (v7.12+)
  6. Disable unused services - Turn off Telnet, MAC-Telnet if not needed
  • Console - serial console access
  • RoMon - remote management over MikroTik network
PropertyTypeDefaultDescription
always-allow-password-loginyes/nonoAllow password auth for users with keys
strong-cryptoyes/nonoUse stronger encryption/HMAC
forwarding-enabledenumnoPort forwarding: no/local/remote/both
host-key-sizeinteger2048RSA key size (1024-8192)
host-key-typeenumrsaKey type: rsa or ed25519
CommandDescription
/ip/ssh/printView SSH configuration
/ip/ssh/setModify SSH settings
/ip/ssh/regenerate-host-keyGenerate new host keys
/ip/ssh/export-host-keyExport host keys
/ip/ssh/import-host-keyImport host keys
CommandDescription
/user/ssh-keys/importImport public key for user
/user/ssh-keys/printList imported keys
/user/ssh-keys/removeRemove imported key
CommandDescription
/system/sshInteractive SSH connection
/system/ssh-execNon-interactive command execution
Key TypeRouterOS VersionOpenSSH Compatibility
RSA6.31+Requires config for OpenSSH 8.8+
Ed255197.12+Works with all modern OpenSSH
DSALegacyDeprecated, not recommended
CipherSecurityNotes
aes-256-ctrHighRecommended with strong-crypto
aes-192-ctrHighGood alternative
aes-128-ctrMediumDefault without strong-crypto
aes-256-gcmHighAuthenticated encryption
3des-cbcLowLegacy, avoid if possible