How to create user with ssh key

Add new user accounts with remote access rights to a Linux instance. Each account can use SSH to connect to the instance from another computer or instance. The new user will be able to use SSH to connect to the instance from another computer or instance.

1. Add a new user to a Linux instance

Add a new user account to an instance, where new_user is a placeholder for the new account name. This command creates an associated group, home directory, an entry in the /etc/passwd file of the instance.

sudo adduser new_user

Note: If you add a new user to an Ubuntu instance, include the –disabled-password option to avoid adding a password to the new account.

sudo adduser new_user --disabled-password

2. Change the security context to the new user account so that folders and files you create will have correct permissions.

sudo su new_user

Note: When you run the sudo su new_user command, the name at the top of the command shell prompt changes to reflect the new user account context of your shell session.

3. Go to user folder

cd /home/new_user

4. Create a .ssh directory in the new_user home directory and change its file permissions to 700. This enforces that only the new_user can read, write, or open the directory.

mkdir /home/new_user/.ssh ; chmod 700 /home/new_user/.ssh

5. Create the authorized_keys file in the .ssh directory, and then restrict file permissions to 600 to enforce that only the new_user has read or write access to the file.

touch /home/new_user/.ssh/authorized_keys ; chmod 600 /home/new_user/.ssh/authorized_keys

6. Add the user public key.

nano /home/new_user/.ssh/authorized_keys

Now, this example shows, how to add SSL key for a newly created user, but if you want your root user to allow login with an SSH key, click on this How-to and you will learn, how to allow root login with an SSH key.

How to generate SSH key