Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

How to Grant su (Superuser) Access to a User in Ubuntu

Give an Ubuntu user administrator rights the right way: the sudo group, a root shell with sudo -i, limited rights for single commands, and why su needs extra care.

2 min readBy Shailabh

Reviewed by the AmazingRDP technical team

Granting superuser access to a user in Ubuntu

A Linux VPS comes with root access, so you decide who gets which rights. See Linux VPS from $6.99

On Ubuntu there are two ways to act as the superuser, root. sudo runs a command as root after asking for your own password. su switches to the root account and asks for root's password. Ubuntu is built around the first: the root account has no password, so su does not work until you give it one. For almost every need, granting sudo is the right answer.

1. Grant sudo: add the user to the sudo group

As a user who already has sudo rights (or as root):

sudo usermod -aG sudo alex

Keep the -a. Without it, the command removes the user from all other groups. The change applies at the user's next login. Check it:

groups alex
sudo -l -U alex

The user tests it after signing in again:

sudo whoami

The answer should be root.

2. A root shell without su

A user with sudo rights can open a full root shell, with no root password involved:

sudo -i

The prompt ends in #. Leave with exit. This covers what people usually want from su.

3. If you really need su

su needs a password on the root account:

sudo passwd root

After that, any user who knows that password can become root with su -. Think before you do this:

  • One shared password, known to several people, cannot be withdrawn from one of them.
  • The log shows "root did it", not which person.
  • If SSH allows root logins with a password, the root password is now a target for guessing from the internet. Check that /etc/ssh/sshd_config has PermitRootLogin prohibit-password or no.

To allow su for chosen users only, open /etc/pam.d/su, remove the # in front of the line containing pam_wheel.so, and add those users to the group it names:

sudo groupadd wheel
sudo usermod -aG wheel alex

To undo the root password later:

sudo passwd -l root

4. Rights for a few commands only

A user who should restart one service does not need to be a full administrator. Create a rule file with the safe editor:

sudo visudo -f /etc/sudoers.d/alex

and write one line:

alex ALL=(root) /usr/bin/systemctl restart nginx, /usr/bin/systemctl status nginx

visudo checks the file when you save. A mistake in a sudoers file can lock everyone out of sudo, so never edit these files with a plain editor.

5. Take the rights away

sudo gpasswd -d alex sudo

and delete the user's file in /etc/sudoers.d/ if you made one.

Which to choose

SituationUse
A trusted administratorThe sudo group
Someone who needs one or two commandsA file in /etc/sudoers.d/
A script or tool that insists on suA root password, with su limited to a group

If the sudo command itself is missing, see how to fix "sudo: command not found". On a Linux VPS you start as root and create your everyday user with these steps. Related: check open ports on Ubuntu and the Ubuntu firewall.

Questions

How do I give a user sudo access in Ubuntu?

Run sudo usermod -aG sudo USERNAME. It applies at the user's next login.

What is the difference between su and sudo?

su switches to the root account and needs root's password. sudo runs commands as root with your own password.

Why does su not work on Ubuntu?

The root account has no password by default. Use sudo -i for a root shell, or set a root password with sudo passwd root.

How do I check whether a user has sudo rights?

Run groups USERNAME and look for sudo, or run sudo -l -U USERNAME.