I have created an azure vm server which is ubuntu 1804 and I need to enable root ssh public key login with it. Azure do not want me to enable root ssh public key login when the server is created, so I need to do it myself with another no root account with password.
sshpass -p 'xxxYourPassword' ssh -C -o StrictHostKeyChecking=no -o ConnectTimeout=10 xxxYourServerUsername@xxxYourServerIp 'sudo bash -c '"'"'umask 077 && mkdir -p .ssh && echo '"'"'"'"'"'"'"'"'xxxYourSshPublicKey'"'"'"'"'"'"'"'"'>> /root/.ssh/authorized_keys'"'"''
Replace xxxYourPassword with your password. Replace xxxYourServerIp with your server ip. Replace xxxYourServerUsername with your server username .Replace xxxYourSshPublicKey with your SshPublicKey content (should like
ssh-rsa xxx root@xxx)
I know this is not secure way to do it, but I just want to finish my job with one line of command and automatic called from golang. If you have another automatic and secure way, please tell me.
There is a lot of vps company that can only create with no root account and password, so I have to make it automatic...
Thanks to https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings to tell me how to escape and escape string in bash again and again.