The Ubuntu VPS you spun up in AWS Lightsail comes with a default username as ubuntu. With that account, you can perform all the admin tasks on that server. But sometimes, you need to assign another user with or without admin privileges for other tasks.
And here is how to create a new user in Ubuntu with a home directory and SSH access with the private key.
First off, create the account.
sudo useradd -s /bin/bash -d /home/username -m -G sudo username
And that creates a new user with bash as the default login shell, a home directory, and sudo rights. Remove -G sudo if you don’t want the user to have the sudo rights.
Then reset the password for the user.
sudo passwd username
You can set up any password and you don’t have to remember it because you are going to use the SSH key to log in.
Now let’s generate a pair of SSH keys.
When prompted for the path and filename, type it in. By default, it will be saved in ./.ssh/id_rsa.
Once you have the key, copy the public key to the new user’s authorized_keys file.
First, use the following to view the public key.
And copy the key that displays on the screen and then run the following command to add it to the new user’s authorized_keys file.
sudo mkdir /home/username/.ssh sudo chmod 0700 /home/username/.ssh sudo -- sh -c "echo 'the_copied_public_key' > /home/username/.ssh/authorized_keys" sudo chown -R username:username /home/username/.ssh/
The last step is to send off the private key to the new user and ask them to try.