Community discussions

MikroTik App
 
mr.incredible
just joined
Topic Author
Posts: 6
Joined: Thu Apr 24, 2008 2:03 pm

ssh-copy-id for Mikrotik to add public key to authorized_keys

Thu Aug 19, 2021 1:46 pm

Hi, I thought I would share this useful one-liner to copy your ssh key from Linux to RouterOS: (from https://stackoverflow.com/questions/688 ... 9#68846249)
ssh 192.168.88.1 "/file print file=mykey; file set mykey contents=\"`cat ~/.ssh/id_rsa.pub`\";/user ssh-keys import public-key-file=mykey.txt;/ip ssh set always-allow-password-login=yes"
Now you won't need to enter your router password to log in to RouterOS with SSH. Hope it helps! It does the same as ssh-copy-id Linux command that nowadays works even on Windows and OSX.

Another tip is you can ssh into Routeros with username+ct@router instead of just username@router, then login will be even quicker. Add "user myname+ct" under "host router" section in /etc/ssh_config to make it the default. Also
/print detail
prints all the infos you need, that print hides because of its silly formatting.
 
aboulfad
just joined
Posts: 23
Joined: Sun Dec 26, 2021 2:25 pm
Location: QC

Re: ssh-copy-id for Mikrotik to add public key to authorized_keys

Mon Dec 27, 2021 5:57 pm

thank you a bunch ! I couldn't believe that ss-copy-id wasn't support even in routerOS 7.1.1. I was getting this error after entering my router's password during ssh-copy-id:
syntax error (line 1 column 6)
 
parablazer
just joined
Posts: 2
Joined: Sat Sep 26, 2020 4:40 am

Re: ssh-copy-id for Mikrotik to add public key to authorized_keys

Thu Mar 23, 2023 3:53 am

When I do this I get an error that it is the wrong format. I have also tried rsa encryption as well.
 
kujo
Member Candidate
Member Candidate
Posts: 169
Joined: Sat Jun 18, 2016 10:17 am
Location: Ukraine
Contact:

Re: ssh-copy-id for Mikrotik to add public key to authorized_keys

Tue Oct 03, 2023 8:08 pm

Tested on 6.49.8
Create new user with 'write' permission
Generate random password for newly created account.
Must be run from linux console.
ssh admin@192.168.88.1 "/file print file=mykey; /file set mykey.txt contents=\"`cat ~/.ssh/id_rsa.pub`\";  /user add name=ansible group=write password=\"`tr -dc A-Za-z0-9 </dev/urandom | head -c 20`\"; /user ssh-keys import user=ansible public-key-file=mykey.txt;"

Who is online

Users browsing this forum: No registered users and 18 guests