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

Posted August 3, 2013 37.6k views
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

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.

Submit an Answer
15 answers

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)
adduser --system --group mynewuser
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

  • Thank you for posting this. These instructions helped me out with a similar issue. May I ask what is the purpose of the following two lines?

    chown -Rfv mynewuser.mynewuser /home/mynewuser/.ssh
    chown -R mynewuser:mynewuser /home/mynewuser/
    • You may have found this in your own research already, but my understanding is these two lines change the ownership of the directories /home/mynewuser/.ssh and /home/mynewuser so that they are owned by the USER mynewuser and the GROUP mynewuser which were created in the first line of these instructions

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 authorizedkeys 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.

Scroll down to the Disable Username/Password Logins section of this article: How to Create SSH Keys with PuTTY to Connect to a VPS.
by Pablo Carranza
This tutorial runs through creating SSH keys with PuTTY to connect to your virtual server.
So with PasswordAuthentication and UsePAM set to no, there would be no concerns with having no password for the root user or the secondary user?
I think I did something wrong. It seems like SSH keys are not working. Also, it appears that I can't login via the VNC console on DO because I setup the server with SSH keys, not a password. It also asked for my password for the secondary user, I didn't expect this. Is this only becuase it needed it to run sudo?

This is what I am getting on the secondary account:

new-host-2:~ mymac$ ssh -p PORT USERNAME@IPADDRESS
The authenticity of host '[IPADDRESS]:PORT ([IPADDRESS]:PORT)' can't be established.
RSA key fingerprint is FINGERPRINTKEY.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[IPADDRESS]:PORT' (RSA) to the list of known hosts.
Permission denied (publickey).
new-host-2:~ user$ sudo apt-get install fail2ban
sudo: apt-get: command not found

This is what I am getting when I try to login as root:
new-host-2:~ mymac$ ssh -p NUM root@IP
Permission denied (publickey).
Did you add you ssh key to /home/user/.ssh/authorized_keys?
Nope. But the error even occurs when I try logging in as root.
So right now I can't access my droplet. The root user won't let me in (which did before), the second user doesn't have the authorized_keys set. I used the DO VNC console to login as my second user but I cannot login as root. Changing PermitRootLogin to yes didn't fix logging in as root (on desktop or VNC) as it gives me "Permission denied (publickey)." which was not an issue when I first logged in with my keys. It would be great to get a tutorial that actually explained these settings instead of just telling us what to type.
Log in as root via the console (sshd's settings shouldn't affect the console), and revert sshd_config to allow password login so you can add your public ssh keys. If you do not know the root password, you can reset it by clicking the Reset Root Password button in the Access tab.
Previous 1 2 Next