To create new users and add their public keys on an Ubuntu server, follow these steps:
First, open a terminal and enter the following command to create a new user. Replace newuser with your desired username.
sudo adduser newuserYou'll be prompted to enter a password for the new user and fill out some optional additional information.
If the new user needs administrative rights (sudo), add them to the sudo group:
sudo usermod -aG sudo newuserTo add a public key for the user, first switch to the user account and create the .ssh directory in the user's home directory:
sudo su - newuser
mkdir ~/.ssh
chmod 700 ~/.sshNow, add the user's public key to the authorized_keys file in the .ssh directory. Do this by copying and pasting the key directly into the file. Replace public_key with the actual public key.
echo public_key >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keysMake sure you use the actual public key of the user and replace public_key accordingly.
After adding the public key, you can return to the original user by using exit.
exitThat's it! The new user should now be set up and able to log in via SSH using their public key. It's important to ensure the public key is correctly inserted without any additional characters or spaces.