Question

Secure Ubuntu server for non-root user using only SSH keys

I would like to have a secure Ubuntu server. I believe the best way to accomplish this is to only use SSH keys and disable use of the root user account. Accomplishing this is not made easy. The tutorials either show you how to use SSH keys on root or how to use a second account with a password, not a combination of the two. I have tried to do both and failed. The following is what I have attempted to do to accomplish this task.

Create Ubuntu droplet with SSH keys in DO panel. Login to root using SSH key and create secondary user. Paste the SSH key into the secondary users’ ~/.ssh/authorized_keys file. Either disable root login or require SSH keys: PermitRootLogin No || without-password <— which one do I choose here?? Kill the ‘/usr/sbin/sshd -D’ process reload ssh


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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I realize this is a bit late, but I had the same problem and managed to solve it. Here are the set of commands that you need to run as root on a new digital ocean droplet (assuming you have already setup root to have ssh access). This will setup mynewuser with passwordless sudo rights and the ability to ssh into the machine without a password (using only your ssh-key)

mkdir /home/mynewuser/.ssh
chmod 0700 /home/mynewuser/.ssh/
cp -Rfv /root/.ssh /home/mynewuser/
chown -Rfv mynewuser.mynewuser /home/mynewuser/.ssh
chown -R mynewuser:mynewuser /home/mynewuser/
gpasswd -a mynewuser sudo
echo "mynewuser ALL=(ALL) NOPASSWD: ALL" | (EDITOR="tee -a" visudo)
service ssh restart
usermod -s /bin/bash mynewuser```

Now you should be able to ssh into your new droplet with
`ssh mynewuser@your-new-digitalocean-droplet-ip-address`

These directions were very helpful to me - but I still was not able to ssh in as the non-root user. My issue (that others may have experienced) was that I copy/pasted my public key into the authorized_keys file - and that paste included splitting the line. One way to see if you have this issue is to run the command wc -l /home/newuser/.ssh/authorized_keys and the response should be 1. In my case it was 5. So I edited the file to remove the line-breaks, saved it, restarted ssh - and all was well. Hope this helps.

For anyone out there scratching their heads over this still… I had this problem…

the only issue for me was that the authorized_keys had to be chmod to 644 in the users .ssh folder

hope it helps

Siggi