Sudoers
The sudoers file is a configuration file used in Unix-like operating systems (such as Linux) to define user permissions for running commands with superuser privileges via the sudo
command. It controls who can run what commands as which user (often root), and under what conditions.
The typical location of the sudoers file is located at the /etc/sudoers
location on the machine but it is always advisable to edit it with the visudo utility to avoid common syntax & logic errors while editing the configuration. Here are some files related to users & root management which will be used in this section
Adding users to `sudo` group to allow sudo access
When creating a user with sudo access we need to add the user into the sudo group first to allow accessing root. We can do the following with the following command :
Here there are some parameters used which means :
-m : Creates the user directory
-s : defines the user welcome shell
After creating the user we will add the user into the sudo group to allow access root
Here the parameter -aG
defines to adding user into the group.
Adding users to sudoers file to allow sudo access
When we require the user to be able to access root but we dont want to add the user into sudo group then we can directly give access to users from the sudoers file.
This will allow the user to get same level of access as the root user on the system
The block structure of the following files is like this :
Limiting user root access to certain path or files
As we have configured the user above with all the rights we can also configure user to be able to access certain files only.
This will only allow the user to access the apt
command with the root level rights and limiting the overall root access of the user.
If we want that user should not require the password while performing allowed root levels we can configure the file like :
Locking and unlocking users
When we require that certain users should not be able to access we can lock their access using the following commands :
To unlock the user back we can use :
To verify the users account are locked or unlocked we can pass the following command :
Here there are two conditions :
If we have
!
sign before the user password which means the user is lockedIf dont the user is unlocked and we can access it
Allowing root level perms based on groups
You want to give 10 users permission to run the command /usr/bin/apt
with sudo
(for package management) without giving them full access to all commands.
reate a new group (e.g.,
apt-users
):Add the 10 users to the
apt-users
group:Repeat this for each of the 10 users.
Edit the
sudoers
file (usingvisudo
for safety):Add the rule for the
apt-users
group:
Permanently blocking root account by changing shells
Changing welcome shells of the user can be the most prominent ways to block the logins. To check the current shells of the user we can see using the following command :
Here are some common shells seen in that files :
/bin/bash: A widely used, feature-rich shell that supports scripting, job control, and command-line editing.
/usr/sbin/nologin: Prevents login access, often used for system or service accounts that should not have a shell.
/bin/sync: A command shell that syncs file system data before shutdown or restart, primarily for the
sync
user./bin/false: A shell that immediately exits with a failure status, preventing login; often used for service accounts.
Here the shell /bin/bash
is the shell which provides the terminal access so changing shell of the user root from /bin/bash
will make the user shell inaccessible , So to change the shell of user we can pass the following command :
Changing the shell of user root :
This is how we can manage sudoers file and limit the attack vectors to minimum. In the next section we will learn about ufw.
Last updated