On a new server, bots start trying to log in as root within hours, with thousands of passwords. Two steps stop them almost completely: you work with a regular user that becomes an administrator only when you ask (sudo), and the server accepts logins only with an SSH key, not with a password. This guide assumes you have already followed VPS server: first login over SSH and log in as root with your key.
What you'll achieve
- have your own user, with administrator rights through
sudo; - log in with it using an SSH key;
- turn off direct root login and password login without locking yourself out.
Before you start
- You are logged into the server as root, with your SSH key (step D in VPS server: first login over SSH).
- Keep a root SSH session open the whole time you work on the settings. If you get something wrong, you fix it from there. Run your tests from a new window.
- In the examples, the new user is called
ana, and203.0.113.7is the server's IP.
The steps
A. Create the user
Ubuntu and Debian:
adduser ana usermod -aG sudo anaadduserasks you for a password foranaand a few optional details (you can press Enter). You need the password forsudo. If Debian reports that thesudogroup or thesudocommand is missing, install them first withapt install sudo.AlmaLinux and Rocky:
adduser ana passwd ana usermod -aG wheel anaOn these, the administrators group is called
wheel.
B. Give it your SSH key
- Copy the public key from root to
ana, with the correct permissions:mkdir -p /home/ana/.ssh cp /root/.ssh/authorized_keys /home/ana/.ssh/ chown -R ana:ana /home/ana/.ssh chmod 700 /home/ana/.ssh chmod 600 /home/ana/.ssh/authorized_keys - From a new terminal window on your computer, log in as
ana:ssh ana@203.0.113.7 Check
sudo:sudo whoamiAfter you enter the password for
ana, you should seeroot. If it works, move on. If not, fix it from the root session you kept open.
C. Turn off root login and password login
- In the root session, create a separate settings file for SSH:
nano /etc/ssh/sshd_config.d/00-securizare.conf Write this in it:
PermitRootLogin no PasswordAuthentication no KbdInteractiveAuthentication noSave with Ctrl+O, Enter, and exit with Ctrl+X. If
nanois missing (this happens on AlmaLinux and Rocky), install it withdnf install nano.The name starts with
00-on purpose: SSH uses the first value it finds for each setting, and the files insshd_config.dare read in alphabetical order. On some images, a file such as50-cloud-init.confturns on password login; yours is read before it, so it wins.Check the settings before you apply them:
sshd -t && sshd -T | grep -Ei 'permitrootlogin|passwordauthentication'You should see
permitrootlogin noandpasswordauthentication no, with no errors.- Apply them:
- Ubuntu and Debian:
systemctl restart ssh - AlmaLinux and Rocky:
systemctl restart sshd
- Ubuntu and Debian:
D. Check that you haven't locked yourself out
- New window:
ssh ana@203.0.113.7must work, with the key. - New window:
ssh root@203.0.113.7must be refused. - Only now close the root session you opened at the start. From now on, run administration commands as
ana, withsudoin front.
Good to know
- One key per computer. For your office laptop and your home laptop, create a separate key on each (
ssh-keygen) and add each public key on a new line in/home/ana/.ssh/authorized_keys. If you lose a computer, you delete only its line. - Other people, other users. Don't share the
anaaccount: create a separate user for each person, withsudoonly if they need it. - The SSH port. Moving SSH from port 22 to another port reduces the noise in the logs, but it isn't protection in itself; the key is the protection. If you move it, open the new port in the firewall first (see VPS server: updates and the firewall).
If something doesn't work
ssh ana@…asks for a password even though you added the key. The permissions on/home/ana/.sshare wrong, or the owner isn'tana. Repeat step 3 from the root session.- “Permission denied (publickey)” for
ana. The public key isn't inauthorized_keys, or you are using a different computer. Check withcat /home/ana/.ssh/authorized_keysfrom the root session. sudosays “ana is not in the sudoers file”. The user isn't in thesudo(orwheel) group. Repeat step 1 or 2, then log out and log back in asana.- You closed all sessions and can't log in at all. We don't log into your server. Contact us: if you have the automatic backup or a recent snapshot, we restore your server from it; otherwise, we'll tell you what options you have.
