How to Disable Root Login and Create a Sudo User
Create your own user with sudo rights and then set the PermitRootLogin option to no in /etc/ssh/sshd_config. Attackers can then no longer log in directly as root. Almost all automated attacks try to log in as root. With your own user and sudo you work just as easily, but much more securely.
Step 1: create a new user
adduser beheerder Choose a strong password. You can skip the other questions with Enter.
Step 2: give the user sudo rights
On Ubuntu and Debian you add the user to the sudo group:
usermod -aG sudo beheerder On AlmaLinux this group is called wheel:
usermod -aG wheel beheerder Step 3: test the new user
Open a new window, log in with the new user and check that sudo works. Do not close your root session yet.
ssh beheerder@JOUW-IP
sudo whoami If the last command returns root, sudo works. If you use an SSH key, also copy it to the new user with ssh-copy-id beheerder@JOUW-IP.
Step 4: disable root login
sudo nano /etc/ssh/sshd_config Find the PermitRootLogin line and set it to no:
PermitRootLogin no Step 5: restart SSH
sudo sshd -t
sudo systemctl restart ssh On AlmaLinux the service is called sshd. Then test in a new window whether logging in with your new user still works.
Locked out?
Via the noVNC console in the client area you can always log in as root. The setting only applies to SSH.
Related articles
Rent a Linux VPS with root access
Ubuntu, Debian or AlmaLinux with full root access via SSH, enterprise Ceph storage with high availability and hosting in Amsterdam.