How to create a new sudo user and secure with SSH key
Log in to your server as the root user or as yourself if you are a sudo or can become a root user.
ssh root@server_ip_address
or
ssh you@server_ip_address
su
enter su password
Then use the adduser command to create a new user on the system. (replace username with the user that you want to create).
adduser username
Set and confirm the new user’s password at the prompt. A strong password is highly recommended!
Set password prompts:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Follow the prompts to set the new user’s information. It is fine to accept the defaults to leave all of this information blank.
User information prompts:
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]
Next you use the usermod command to add the newly created user to the sudo group.
usermod -aG sudo username
By default, on Ubuntu, members of the sudo group have sudo privileges. To test sudo access on new user account, use the switch user command, su, to switch to the new user account.
su - username
Once logged in as the new user, verify that you can use “sudo” to the command, that would require superuser privileges to run. A good test command for example would be to list the contents of the /root directory, which is normally only accessible to the root user.
sudo ls -la /root
Now it is time to add the SSH key. While logged in as the new user go to the home directory for that user and create a SSH directory
mkdir .ssh
Then change to the new directory to add the authorized keys file
cd .ssh
Next create a file called authorized_keys using your favorite file editor – I will use nano.
nano authorized_keys
Paste the contents of the new users public key into the file and save.
Congrats the new user is on the server and secured!
Comments are currently closed.