How to Grant Sudo Privileges (root permission) to Non-Sudo Users in Linux Without Rebooting

If you’re using Linux, you might encounter situations where you need to give a non-sudo user the ability to run commands as a superuser (sudo). This could be for a variety of reasons—whether it’s for a co-worker, a team member, or even yourself if you forgot to add sudo access when setting up the system. The good news is that it’s pretty simple to do, and you don’t need to reboot the system for it to take effect.

In this post, I’ll walk you through the steps to give a non-sudo user sudo privileges in Linux, and I’ll make sure to explain things clearly so even if you’re new to Linux, you can follow along. Ready? Let’s dive in!

Step 1: Log in as Root or a User with Sudo Access

First, to make any changes to a user’s permissions, you’ll need to be logged in as either the root user or as a user who already has sudo access.

If you are logged in as a user with sudo privileges, you can open a terminal and proceed with the following steps.

Step 2: Check the User’s Current Group Membership

Before adding a user to a group, it’s a good idea to first check which groups the user is already part of. This way, you can make sure you’re not inadvertently changing something that shouldn’t be changed.

To check the groups that a user is currently in, run the following command (replace username with the name of the user you’re checking):

groups username 

For example, if you want to check the groups for the user amir, you would run:

groups amir 

This will output a list of the groups the user belongs to. Here’s an example output:

amir : amir sudo docker

In this case, amir is already a member of the sudo group, which means they already have sudo access. If the user is not in the sudo group or any group with the necessary privileges, you can proceed with adding them to the correct group.

Step 3: Add the User to the Sudo Group

Most Linux distributions, including Ubuntu and Debian-based systems, use a group called sudo to manage sudo permissions. Adding a user to this group will automatically give them sudo access.

Run the following command (replace username with the actual username of the person you’re adding to the sudo group):

sudo usermod -aG sudo username 

Let’s break this down:

  • usermod: The command used to modify a user’s account.
  • -aG: This option adds the user to a group without removing them from any other groups.
  • sudo: The name of the group you’re adding the user to.
  • username: The name of the user you’re granting sudo access to.

For example, if you wanted to give a user named alex sudo access, you’d run:

sudo usermod -aG sudo amir 

Step 4: Apply the Changes Immediately (No Need to Reboot)

You might think that after adding the user to the sudo group, you’ll need to reboot your system for the changes to take effect. The good news is, you don’t need to reboot!

The change will apply as soon as the user starts a new session. Here’s how to do it:

  • Option 1: The user can log out and log back in. This is the simplest option, as the new session will automatically load the updated group settings.
  • Option 2: If the user doesn’t want to log out or you need the changes to take effect immediately, you can use the newgrp command. This command changes the user’s current group without logging out. Run this command:
newgrp sudo

This will apply the new group to the current session without needing to log out.

Step 5: Verify the User’s Sudo Access

To ensure that the user now has sudo privileges, you can test it by running the following command while logged in as the user:

sudo whoami

This command asks the system who the current user is, but it requires sudo access to run. If everything is set up correctly, it will return root, which confirms that the user now has sudo privileges.

Step 6: Practical Example – Docker

A common situation where users need sudo access is when working with Docker. By default, Docker requires sudo to run commands like docker run or docker ps, but you might want to give a non-sudo user access to Docker without needing to type sudo every time.

Here’s how you can give the user access to Docker:

  1. Add the User to the Docker Group:

Docker creates a special group called docker. If you add the user to this group, they’ll be able to run Docker commands without sudo.

Run this command (replace username with the actual username of the person you want to give Docker access to):

sudo usermod -aG docker username 

For example, if you want to give the user amir Docker access, you’d run:

sudo usermod -aG docker amir
  1. Apply the Changes:

Just like with the sudo group, the changes won’t take effect immediately unless the user starts a new session. They can log out and log back in, or you can use the newgrp command:

newgrp docker
  1. Verify Docker Access:

Now that the user is in the Docker group, they should be able to run Docker commands without sudo. You can test it by running:

docker --version

If the user has Docker access, the command will output the Docker version without requiring sudo.

Previous Post

What is actually proxy server? And how to setup squid proxy

Next Post

How to Create Users in Linux, A Complete Guide

Related Posts

How to Create Users in Linux, A Complete Guide

Discover how to create and manage users in Linux using the useradd command, with step-by-step instructions for Ubuntu, CentOS, Fedora, and other major distributions. Learn essential commands and options like setting home directories, managing user groups, and customizing shell settings. This comprehensive guide is perfect for beginners and experienced Linux users alike, helping you efficiently manage user accounts and permissions on any Linux system.
Read More
Total
0
Share