Skip to content

Instantly share code, notes, and snippets.

@andokai
Last active September 12, 2023 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andokai/20dc3cf3b6f995db6969db2b03d23ac2 to your computer and use it in GitHub Desktop.
Save andokai/20dc3cf3b6f995db6969db2b03d23ac2 to your computer and use it in GitHub Desktop.
Add SSH key only (no password) user with sudo permission to Ubuntu VM on Azure (and probably everywhere else)

Login to VM using an account with sudo permission.

Add the new user with the username "new-user".

sudo useradd -c 'New User' -m -G sudo new-user

-m creates the home folder and -G sudo adds them to the sudo user group.

Switch to the newly created account .

sudo su new-user

Create an SSH key on your local machine and add it to the SSH agent. Instructions on how you can do this can be found here. You can also use an existing key. https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

NOTE: If you are using 1Password as your SSH agent then simply create a new SSH key in 1Password and skip the previous step.

Create and open the file to store your authorised SSH key.

nano ~/.ssh/authorized_keys

Paste the public key into the file, save and exit your editor. This should start with ssh-.

Exit out of the new-user session.

exit

Start a superuser session.

sudo su

Create and open a file that tells Ubuntu that this new user does not use a password.

nano /etc/sudoers.d/new-user

Add the following line to the file, save and exit your editor.

new-user  ALL=(ALL) NOPASSWD:ALL

Exit out of the super user session.

exit

You are now done. Terminate the connection to the VM using exit or closing the terminal window and you should now be able to SSH into the VM using the new account without using a password. You should also be ale to run sudo commands on the machine without needing to enter a password either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment