Linux sudo command explained

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Any commands that require root privileges, which are the commands that generally make changes to the system are going to need access to the root or sudo. Sudo can be used in place of root, which allows particular users to run various commands as the root user, without needing the root password. One of the benefits of using sudo is that it allows you to forego the root account completely. Once the sudo is set up, the root account can be locked because technically it is not needed anymore.
sudopackage is not always installed on every instance, but for example Ubuntu always has that installed as part of the defaults. To provesudois installed, run the command:which sudo
💡If Ubuntu is installed manually, then the root account is locked by default.Find out what is a sudo group on your distribution. Usually, the group will be named either
sudoorwheel. Look at the /etc/sudoers file:sudo cat /etc/sudoersOn Ubuntu 20.04, the group name is "sudo"

To find out which groups the currently logged-in user belongs to, run the command:
groups usernameIf your user is not a member of the sudo group to access the sudo, run the command:
sudo usermod -aG sudo usernameTo list the
sudoprivileges for the invoking user and to tell what commands the current user is allowed to run undersudo, and with what privileges, run:sudo -l
💡It's common for Vagrant boxes (the package format for Vagrant environments) to come pre-configured with thevagrantuser having passwordless sudo access. This is intended to make development and testing easier, as it removes the need to constantly enter passwords when configuring the VM.To safely edit the
sudoersfile, use the command:sudo visudoThis command opens the file in the system's default editor, set by the
EDITORenvironment variable. After editing, when you exitvisudo, it automatically checks the syntax. If there are no errors, the changes are saved; if there are errors, it gives you the option to fix them or abandon your changes.To restrict the user to be able to do one specific thing, open the
sudoersfile and edit the line for specific users changing from ALL to a specific command:
The full path to the command can be found with the command
whichWhen
visudodetects a syntax error upon attempting to save and exit thesudoersfile, it typically presents a prompt asking what you want to do next.
There are 3 possible options:
e: to edit the sudoers file again and fix the error.
x: to exit without saving the changes (on some systems, this might be a different letter or option).
q: to quit and save changes despite the error, which is not recommended because it can leave your system in an unusable state as far as
sudois concerned.
The syntax format of sudoers file
The syntax format used in the sudoers file is quite flexible and allows for specifying a wide range of permissions for different users and groups. Here's a breakdown of the general syntax and some examples to illustrate how permissions can be defined:
General Syntax
The general syntax for a rule in the sudoers file is:
User_Alias HOST_Alias = (Runas_Alias:Runas_Group) COMMAND_Alias
User_Alias: Specifies one or more users or a group of users. Aliases are defined elsewhere in thesudoersfile.HOST_Alias: Specifies one or more hosts. Like user aliases, host aliases are defined elsewhere in thesudoersfile.(Runas_Alias:Runas_Group): Specifies the user and/or group as whom the commands can be run. The user and group can be specified directly or through aliases.COMMAND_Alias: Specifies one or more commands that can be executed. Command aliases are defined elsewhere in thesudoersfile.
Examples
Specific User on Any Host for Specific Commands:
john ALL=(ALL) /bin/ls, /usr/bin/grepThis allows the user
johnto run/bin/lsand/usr/bin/grepas any user on any host.Group of Users for Any Command as Specific User:
%admin ALL=(www-data) ALLThis rule allows any user in the
admingroup to run any command as thewww-datauser on any host.Alias Example:
Define Aliases:
User_Alias ADMINS = john, jane Cmnd_Alias WEB_SERVICES = /etc/init.d/apache2, /etc/init.d/nginxUse Aliases in Rule:
ADMINS ALL=(ALL) WEB_SERVICES
This setup allows users john and jane to run commands to start or stop apache2 and nginx services as any user on any host.
Command with No Password:
alice ALL=(ALL) NOPASSWD: /usr/bin/apt-get updateThis rule allows the user
aliceto run the command/usr/bin/apt-get updateas any user without being prompted for a password.Denying Commands:
jeff ALL=(ALL) ALL, !/usr/bin/suThis allows
jeffto run any command except/usr/bin/suas any user on any host.
Notes
When editing the
sudoersfile, always use thevisudocommand to ensure syntax correctness and prevent configuration errors.The
sudoersfile syntax is powerful and allows for very granular control over permissions. Always review and test rules carefully to ensure they meet your security requirements.




