Skip to content

Creating a New User with SSH Access

Overview

This guide explains how to create a new user on a Linux system, configure SSH access, and grant sudo privileges if needed.

Steps

1. Create a New User

To create a new user, run the following command:

Important

Replace <username> with the desired username

sudo useradd -m -d /home/<username> -s /bin/bash <username>

This command: - -m creates a home directory. - -d /home/<username> specifies the home directory. - -s /bin/bash sets Bash as the default shell.

2. Set the User’s Password

Assign a password to the new user:

passwd <username>

Note

If you don’t want the user to have a password, set it to an empty string.

3. Configure SSH Access

Create the .ssh Directory

Run:

mkdir /home/<username>/.ssh

Add the Public Key

To enable SSH key authentication, copy the user's public key into the authorized_keys file:

vim /home/<username>/.ssh/authorized_keys

This fetches the SSH key from the user's GitHub profile.

4. Set the Correct Permissions

Ensure the .ssh directory is owned by <username>:

chown -R <username>:<username> /home/<username>/.ssh

Set appropriate permissions:

chmod 700 /home/<username>/.ssh
chmod 600 /home/<username>/.ssh/authorized_keys

These permissions: - Ensure only <username> can access the .ssh directory. - Secure the authorized_keys file.

5. Grant Sudo Privileges (Optional)

Change to root and then install sudo

apt-get install sudo

To give <username> sudo access, add them to the sudo group:

sudo usermod -a -G sudo <username>

If the sudo group does not exist, manually edit the /etc/sudoers file:

visudo

Then add:

<username> ALL=(ALL) NOPASSWD: ALL

Warning

Be cautious when editing /etc/sudoers. Incorrect changes can lock you out of sudo access.

Conclusion

You have now successfully created the user <username>, configured SSH access, and optionally granted sudo privileges.