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.