Home Alpine Linux Add, Delete And Grant Sudo Privileges To Users In Alpine Linux

Add, Delete And Grant Sudo Privileges To Users In Alpine Linux

By sk
Published: Updated: 63.5K views

We already have shown you how to create a sudo user, assign sudo permissions to existing users and remove sudo privileges from an user in Arch Linux, CentOS and Ubuntu. Today, we will see how to add, delete and grant sudo privileges to users in Alpine Linux.

For the purpose of this guide, I will be using a minimal Alpine Linux box.

Add, Delete And Grant Sudo Privileges To Users In Alpine Linux

Login as root user or any other existing sudo user. Since it is a fresh and minimal Alpine Linux box, there are no other sudo users in my system, so I logged in as root user.

First of all, make sure the sudo package is installed in your Alpine Linux system. By default, sudo is not installed.

To install sudo in Alpine Linux as root user, run:

$ sudo su
# apk update
# apk add sudo

Next, let us create a new user in Alpine Linux and grant sudo privileges to the newly created user.

1. Create a sudo user in Alpine Linux

To create a new user in Alpine Linux, we use adduser command as shown below:

# adduser ostechnix

Here, I am creating a new user named "ostechnix".

Enter the password twice for the new user and complete the user creation.

Changing password for ostechnix
New password: 
Retype password: 
passwd: password for ostechnix changed by root

We have just created a new normal user. The user doesn't have any administrative rights yet.

You can verify if an user has sudo privileges or not in Alpine Linux using command:

# sudo -lU ostechnix

Replace "ostechnix" in the above example with you own username.

Sample output:

User ostechnix is not allowed to run sudo on alpine38.

Let us add the new user to sudoers list, so he/she can perform administrative operations.

1.1. Grant sudo privileges to users in Alpine Linux

We can do this in two ways.

Method 1 :

To assign sudo permissions to an user in Alpine Linux, simply add him/her to wheel group. For those who don't know yet, wheel is a special group in some Unix-like operating systems. All the members of wheel group can perform administrative tasks. Wheel group is similar to sudo group in Debian-based systems.

Run the following command to allow the members of wheel group to perform any command:

# echo '%wheel ALL=(ALL) ALL' > /etc/sudoers.d/wheel

Next, add the user "ostechnix" to the wheel group:

# adduser ostechnix wheel
Grant sudo privileges to users in Alpine Linux
Grant sudo privileges to users in Alpine Linux

We added the user ostechnix to sudoers list. Now skip to the "1.2. Check if an user has sudo access in Alpine Linux" section and check if the user has sudo permissions.

Method 2:

The another way to assign sudo permissions to an user is by directly adding him in the /etc/sudoers configuration file.

To give sudo permissions to the user "ostechnix", edit "/etc/sudoers" file:

# visudo

Add the following line:

ostechnix ALL=(ALL) ALL
Add users in sudoers configuration file in Alpine Linux
Add users in sudoers configuration file in Alpine Linux

Hit ESC key and type :wq to save the file and exit.

Done! The user has been added to the sudoers list. Let su go ahead and check if he is able to run administrative operations.

1.2. Check if an user has sudo access in Alpine Linux

To verify if an user has sudo rights in Alpine Linux, run this command:

# sudo -lU ostechnix

Sample output:

User ostechnix may run the following commands on alpine38:
    (ALL) ALL
Check if user has sudo permission in Alpine Linux
Check if user has sudo permission in Alpine Linux

As you can see, the user "ostechnix" can run all commands in my Alpine Linux box.

Let us switch to the new sudo user and verify if he can able to run sudo tasks.

To switch to the new user i.e. ostechnix in our case, run:

# su - ostechnix

Run any sudo operation to verify if the user really has sudo permissions.

$ sudo apk update

Sample output:

Verify if an user has sudo privileges in Alpine Linux
Verify if an user has sudo privileges in Alpine Linux

Yes, the user has become part of the administrative users group.

2. Delete sudo privileges from an user in Alpine Linux

To revoke the sudo permissions from an user in Alpine Linux, just remove him/her from the wheel group using gpasswd command. The gpasswd command is not available in Alpine base image. You need to install shadow package in order to get gpasswd command.

To install shadow package in Alpine Linux, run the following command as root user:

# apk add shadow

Now you can remove the sudo privileges from an user, for example ostechnix, using command:

# gpasswd -d ostechnix wheel

Sample output:

Removing user ostechnix from group wheel

The user ostechnix has been removed from wheel group. You can verify it using command:

# sudo -lU ostechnix
Delete sudo privileges from an user in Alpine Linux
Delete sudo privileges from an user in Alpine Linux

The user ostechnix has now become a regular user. He can't perform any sudo operations anymore.

If you don't want that user anymore, remove him entirely from the system using this command:

# userdel -r ostechnix

Here, -r flag is used to delete the $HOME directory of the user.

That's it. This tutorial taught you how to create a sudo user, and how to assign sudo privileges to existing users and finally how to delete the sudo privileges from an user in Alpine Linux. Hope it helps.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More