Skip to content

Instantly share code, notes, and snippets.

@eugen-hoppe
Last active June 9, 2026 22:12
Show Gist options
  • Select an option

  • Save eugen-hoppe/4d66bb0c5ccc879d5f9ab4625992b24a to your computer and use it in GitHub Desktop.

Select an option

Save eugen-hoppe/4d66bb0c5ccc879d5f9ab4625992b24a to your computer and use it in GitHub Desktop.
Cheat sheet to create a new user (Linux) with SSH public key. #linux #ssh

How to create a new user (Linux) with SSH Public Key?

To create new users and add their public keys on an Ubuntu server, follow these steps:

1. Create a New User

First, open a terminal and enter the following command to create a new user. Replace newuser with your desired username.

sudo adduser newuser

You'll be prompted to enter a password for the new user and fill out some optional additional information.

2. Grant the New User Sudo Privileges (Optional)

If the new user needs administrative rights (sudo), add them to the sudo group:

sudo usermod -aG sudo newuser

3. Set Up Public Key Authentication

To 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 ~/.ssh

4. Add Public Key

Now, 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_keys

Make sure you use the actual public key of the user and replace public_key accordingly.

5. Switch Back to Original User

After adding the public key, you can return to the original user by using exit.

exit

That'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.

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