Sys Admin: How to configure sudoers file, Admin privileges.
Tags: security, devops, opensource, linux
Published: 2023-09-10 16:35:09

Hi guys, hope you are doing are doing well. I'm excited to share another piece of informative writeup today.

In this article I will be showing you some basic but useful configurations commands which includes configuring the sudoers file, admin rights, group right etc.

### Configuring the Sudoers file

Have you seen this error message before when trying to run a sudo command even if you installed the linux O.S yourself on your PC or when you install Linux on VM machines.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frdjpsx7usllx5kcj7ac.png)

*The above image is showing us that the user "afeez" cannot use the sudo command* which mean user afeez cannot perform elevated tasks using sudo.

**How do we fix this?**

There are numerous ways of fixing this issue. But we will go with the most common solution, that is we can 
1. Add the user into the sudoers file with all Admin privileges.

But there is a problem with this solution...

Let's say we have about 10 new users that needs to have an admin privilege, that means we have to keep doing this. Editing the sudoers file for every user. Which will definitely waste our time and above all, not smart at all.

2. Create groups and add each user(s) with the similar tasks to the same group. Sounds smart enough??

Lemme show you how to do this.

Let us create a group called devZ, we will give it all the access we require to run sudo then add user afeez to the group.

```
sudo groupadd devZ
```

Yeah, you guessed it. It throws an error, because we still need sudo to create a new user.

So, the workaround is to enter the command 

`su` (su = switch user  => $ )you will see the $ prompt once you have gained that access.

followed by your password then we can continue with our solution. Just wanted to throw that in, incase you encounter such issue. You are welcome :) 

then run the `groupadd` command to create a group and add the user afeez to be a part of the group.

```
sudo usermod -a -G genZ afeez
```

The above command will add user afeez into the genZ group

You can confirm this by running the command `id afeez`


![Group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/af9p0b3vq0kd7x4xboa7.png)


![group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/842bnf2iv13wbh6sjt92.png)

### Step 2:
Let's now edit the sudoers file.

All configuration files can be found in the /etc/ folder, the sudoers file is in the /etc/sudoers

Use the ```sudo visudo /etc/sudoers``` command to open the sudoers file and lemme quickly go through the output

![Sudoers output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzb0nvreocyetg3m0is9.png)

*All line starting with # represents comments. (Not executed by the shell)

* root ALL=(ALL:ALL)ALL - this mean that the root user has unlimited privileges and can run any command

* %admin ALL=(ALL)ALL - All % specifies a group, which mean anyone in the admin group has the same privilege as d root user.

* %sudo ALL=(ALL:ALL) ALL - All users in the sudo group have the same privileges as the root user and can run any command.

I added the group devZ under the %sudo with the line

%genZ ALL=(ALL:ALL) ALL


![Group add](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqeqtaktk85dp5kwimwk.png)

then save the file, depending on the editor you are using.

Another trick to know if your config file save successfully without any errors, use command

```sudo visudo -c```

![File save](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/helxsxizno6phks8c81h.png)

We need to confirm the file is OK, this is because it has permissions configuration which if mis-configured could log you completely and not have access to your server AGAIN. You want to be very careful with these configuration files.

You need to reboot the system then try the sudo command again. 

```reboot now ```

*Apply the exit command to take you back into the user, in this case afeez and try the sudo commands again*


![Sudo works now](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9fefb9w69t1gho9ij3y.png)

And BOOM! we now have access to the sudo command.

I hope this helps.

Thanks for reading :)