Setting up a sudo User with SSH key access an Azure Virtual Machine (Ubuntu)
May 26, 2021
- You can create a new user from the Support + troubleshooting > Reset password screen. By default, it'll update the password for the given account. But if the given account does not exist, it'll create one, add it to sudoers list (make sure it can run
sudocommands) and then set the password for it too. - The
~/.sshfolder doesn't exist when you create a new user. So you have to create it, and theauthorized_keysfile to add accepted keys that can connect to the server and set the right permissions for both - Setting permissions is important because SSH will not connect if the permissions are too open
authorized_keysis where you add the contents of the.pubfile of your key pair.
Set the server up to accept SSH key for the user
Login to the Azure Virtual Machine with your password and create the required files and folders
code
# create the ~/.ssh folder and set perms
mkdir ~/.ssh
chmod 700 ~/.ssh
# create the authorized_keys file and set perms
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
# reload the SSH service for the changes to take effect
sudo service ssh reload
Create an SSH key pair
On your local computer, generate an SSH key that you'll use to connect to the remote Azure virtual machine
code
cd
ssh-keygen -t ed25519 -C "your_email@example.com"
Copy the key to the server
Copy the key you just created to the Azure Virtual Machine
code
# MacOS may not include ssh-copy-id with OpenSSH
# brew install ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed2551 user@host
- it'll ask you for the password when you add the key. afterwards, once the key has been copied, it'll just use the key to login
ssh-copy-idis part of OpenSSH, and i prefer this over manually copy/pasting the key usingpbcopy,xcliporcat- if you don't provide the
-itossh-copy-idit adds all keys to the remote server - you don't have to specify the
.pubextension for the key, it only copies the public part by default
You can now do ssh user@host and it shall log you in without asking for the password