# How to set up sudo on Debian, Ubuntu, AlmaLinux, openSUSE and FreeBSD

> Creating a sudo user, editing sudoers safely with visudo and disabling root login over SSH. Steps for Debian, Ubuntu, AlmaLinux, Rocky Linux, Fedora, openSUSE and FreeBSD.

Source: https://www.hukot.net/community/en/tutorials/how-to-set-up-sudo-on-debian-ubuntu-almalinux-opensuse-and-freebsd
Updated: 2026-11-25

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](https://www.hukot.net/en/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

::: tabs
+++ Debian / Ubuntu
```bash
apt update && apt install sudo
```
+++ AlmaLinux / Rocky / Fedora
```bash
dnf install sudo
```
+++ openSUSE
```bash
zypper install sudo sudo-policy-wheel-auth-self
```
+++ FreeBSD
```bash
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

::: tabs
+++ Debian / Ubuntu
```bash
adduser <user>
```
`adduser` asks for the password and some other details right away; you can skip those with Enter.
+++ AlmaLinux / Rocky / Fedora
```bash
useradd -m <user>
passwd <user>
```
An `adduser` command exists here too, but it does not set a password, so `passwd` is needed.
+++ openSUSE
```bash
useradd -m <user>
passwd <user>
```
+++ FreeBSD
```bash
adduser
```
The wizard asks for the name and password. When it asks **Invite <user> into other groups?**, you can answer `wheel` and skip the next step.
:::

More on managing users is in [Adding and removing users in Linux](tutorial:92).

### Allowing sudo

The simplest way is to add the user to the group that is allowed to use sudo.

::: tabs
+++ Debian / Ubuntu
```bash
usermod -aG sudo <user>
```
The `sudo` group is allowed in `/etc/sudoers` by default.
+++ AlmaLinux / Rocky / Fedora
```bash
usermod -aG wheel <user>
```
The `wheel` group is allowed in `/etc/sudoers` by default. The same steps apply to CentOS Stream.
+++ openSUSE
```bash
usermod -aG wheel <user>
```
The rule for the `wheel` group comes from the `sudo-policy-wheel-auth-self` package.
+++ FreeBSD
```bash
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.

```bash
visudo -f /etc/sudoers.d/<user>
```

Sudo for a single user without group membership:

```plaintext
<user> ALL=(ALL:ALL) ALL
```

Sudo without a password (not recommended: whoever gets into the account gets root right away):

```plaintext
<user> ALL=(ALL:ALL) NOPASSWD: ALL
```

> [!WARNING]
> 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:

```bash
sudo whoami
```

After you enter the user's password, it prints:

```plaintext
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.

> [!WARNING]
> Keep one more session open in which you are logged in and have root. If something goes wrong, you fix it there.

::: tabs
+++ Debian / Ubuntu
```bash
echo 'PermitRootLogin no' | sudo tee /etc/ssh/sshd_config.d/00-no-root-login.conf
sudo sshd -t && sudo systemctl restart ssh
```
+++ AlmaLinux / Rocky / Fedora
```bash
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`.
+++ openSUSE
```bash
echo 'PermitRootLogin no' | sudo tee /etc/ssh/sshd_config.d/00-no-root-login.conf
sudo sshd -t && sudo systemctl restart sshd
```
+++ FreeBSD
Set the line `PermitRootLogin no` in `/etc/ssh/sshd_config` (FreeBSD uses it by default) and restart SSH:
```bash
sudo sshd -t && sudo service sshd restart
```
:::

Check the value the SSH server actually uses:

```bash
sudo sshd -T | grep -i permitrootlogin
```

```plaintext
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](tutorial:26), [limiting which users may log in](tutorial:130) and [Fail2ban](tutorial:31).

::: cta
## 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.

[Choose a VPS configuration](https://www.hukot.net/en/linux-vps/)
:::
