Working on a server directly as root is risky: one typo in a command and nothing stops you. It is safer to use a normal user who asks for root rights with sudo only for a specific command. This tutorial shows how to create such a user, allow them to use sudo, and finally disable root login over SSH.
Prerequisites #
- A Linux or FreeBSD server with root access, for example a Hukot.net Linux VPS.
- Run the commands up to the verification as root. From the SSH part on, run them as the new user through
sudo.
Replace <user> in the commands with the name of the new user, for example john.
Steps #
Installing sudo
apt update && apt install sudo
dnf install sudo
zypper install sudo sudo-policy-wheel-auth-self
pkg install sudo
On openSUSE, sudo asks for the root password by default. The sudo-policy-wheel-auth-self package changes that: members of the wheel group enter their own password.
Creating the user
adduser <user>
adduser asks for the password and some other details right away; you can skip those with Enter.
useradd -m <user>
passwd <user>
An adduser command exists here too, but it does not set a password, so passwd is needed.
useradd -m <user>
passwd <user>
adduser
The wizard asks for the name and password. When it asks Invite into other groups?, you can answer wheel and skip the next step.
More on managing users is in Adding and removing users in Linux.
Allowing sudo
The simplest way is to add the user to the group that is allowed to use sudo.
usermod -aG sudo <user>
The sudo group is allowed in /etc/sudoers by default.
usermod -aG wheel <user>
The wheel group is allowed in /etc/sudoers by default. The same steps apply to CentOS Stream.
usermod -aG wheel <user>
The rule for the wheel group comes from the sudo-policy-wheel-auth-self package.
pw groupmod wheel -m <user>
echo '%wheel ALL=(ALL:ALL) ALL' > /tmp/wheel
visudo -cf /tmp/wheel && install -m 440 /tmp/wheel /usr/local/etc/sudoers.d/wheel
On FreeBSD the wheel group is commented out in sudoers by default. Add the rule as a separate file; sudoers lives in /usr/local/etc/sudoers here.
Custom sudoers rules (optional)
Rules outside the groups belong in a separate file in /etc/sudoers.d/ (/usr/local/etc/sudoers.d/ on FreeBSD). Always open it with visudo, which checks the syntax and refuses to save a broken file.
visudo -f /etc/sudoers.d/<user>
Sudo for a single user without group membership:
<user> ALL=(ALL:ALL) ALL
Sudo without a password (not recommended: whoever gets into the account gets root right away):
<user> ALL=(ALL:ALL) NOPASSWD: ALL
Never edit /etc/sudoers directly with an editor such as vim or nano. A single syntax error, for example ALL=(ALL:ALL) ALL NOPASSWD: ALL, invalidates the whole file and sudo stops working for everyone. If you then disable root login, you can no longer get into the server as an administrator.
File names in sudoers.d must not contain a dot or end with ~; sudo skips such files.
Verification #
Group membership takes effect from the next login. Log out, log in as the new user, for example ssh <user>@<server-ip>, and run:
sudo whoami
After you enter the user's password, it prints:
root
Open an interactive root shell with sudo -i. You do not need to restart the SSH server for sudo; sudo reads its rules every time it runs.
Disabling root login over SSH #
Once sudo works, disable root login over SSH. Attackers try passwords for exactly the root account.
Keep one more session open in which you are logged in and have root. If something goes wrong, you fix it there.
echo 'PermitRootLogin no' | sudo tee /etc/ssh/sshd_config.d/00-no-root-login.conf
sudo sshd -t && sudo systemctl restart ssh
echo 'PermitRootLogin no' | sudo tee /etc/ssh/sshd_config.d/00-no-root-login.conf
sudo sshd -t && sudo systemctl restart sshd
AlmaLinux ships its own 25-permitrootlogin.conf. SSH uses the first value it finds, which is why our file starts with 00.
echo 'PermitRootLogin no' | sudo tee /etc/ssh/sshd_config.d/00-no-root-login.conf
sudo sshd -t && sudo systemctl restart sshd
Set the line PermitRootLogin no in /etc/ssh/sshd_config (FreeBSD uses it by default) and restart SSH:
sudo sshd -t && sudo service sshd restart
Check the value the SSH server actually uses:
sudo sshd -T | grep -i permitrootlogin
permitrootlogin no
Finally, try ssh root@<server-ip> in a new terminal. The login must fail, while ssh <user>@<server-ip> and sudo -i keep working. Only then close the backup session.
You can secure SSH access further with key-based login, limiting which users may log in and Fail2ban.
Looking for a server where you alone have root? #
On a Hukot.net Linux VPS you can choose Debian, Ubuntu, AlmaLinux, Rocky Linux, openSUSE, Fedora or FreeBSD, with full root access.