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

August 3, 2013 12.8k 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
13 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

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.
Okay. I can login as root now. It was because it wasn't an AllowedUser. My bad. I'll copy the key to my authorized_keys and I should be good. Hopefully.
Would this be the correct command to copy the key from root the secondaryuser (when logged in as root)? Or do I not need home?

root@droplet-01:~# cp i ~/.ssh/authorized_keys /home/secondaryuser/.ssh/authorized_keys
cp: target `/home/secondaryuser/.ssh/authorized_keys' is not a directory
The easiest way would be running this command locally:

ssh-copy-id user@host

I am having a similar problem. My key works when login in for root but not for non-root users.

Have another answer? Share your knowledge.