Question

How to enable SSH access for non root users

Following the recommendations in online forums, I have configured my new Ubuntu server so that root can only be accessed via my private key.

My understanding is that I’m also supposed to create a different user account that will be used to handle root-like tasks via elevated privileges and sudo. I can create that account with appropriate privileges, but the server will not allow me to connect via SSH. The only way I can use the other account to access the server is via the Digital Ocean console. The console does not play well with my Mac’s external keyboard.

What do I need to do to enable SSH connections to other accounts? I have found instructions online which suggest changing PermitRootLogin from ‘no’ to ‘yes,’ but that seems to defeat the point of using an encrypted key to control root access. I cannot find instructions which explain how to allow password authentication for everyone EXCEPT the root user.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
August 11, 2019
Accepted Answer

Hi adelwhich,

You’ll need to create a user and add it to the sudoers group

You’ll start by creating a user called exampleuser using the adduser command while creating the user’s home directory

# useradd -m -d /home/exampleuser exampleuser

Once you’ve created the user, I’ll recommend setting a password. To do so execute:

# passwd exampleuser

Then use the usermod command to add the user to the sudo group

# usermod -aG sudo exampleuser

You can test if everything was done correctly by using the su command

# su -s /bin/bash username

You can also try and open a file somewhere. Maybe you can try and open a file in /etc just to see if you have permission to edit it.

$ sudo vi /etc/timezone

If you don’t see permission denied you are good to go.

There is something I like to point out. If you add your users to the sudoers group, they’ll have the power of a root user.

Now to allow SSH access to a certain user

Update on 10.07.2021 Initially, this answer contained information how to enable a user or a group to SSH however this has been outdated.

As such, I’ve updated the answer to providing information on how to configure your SSH keys on the user.

Now that you have the user-created, SSH to your Droplet using the root user and switch to the newly created one:

# su -s /bin/bash username

Once inside, go to your home folder with the cd command

$ cd

If you followed the whole answer this will bring you in the /home/exampleuser directory.

Now, you need to create the folder .ssh and an authorized_keys file, you can do it like that:

$ mkdir /home/exampleuser/.ssh                                                                                $ touch /home/exampleuser/.ssh/authorized_keys                                                                $ chmod 600 /home/exampleuser/.ssh/authorized_keys

All that is left to do is copy your SSH key in the authorized_keys file and you are good to go.

Kind regards, KFSys

@KDSys

I just followed your advice, and now I can’t login with my created user nor root… Wtf!? 😢

Also there is something to do at: /etc/passwd specify the user space for logged user and shell.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel