Sometimes, system administrators are overwhelmed with too many tasks and unable to handle everything on their own. Some of these tasks can actually be delegated to others, such as account management, which can be handled by HR personnel. While a simple command like "su" can allow the delegated person to become a superuser and perform tasks, the main problem with su is that it requires sharing the superuser's password with the delegate, potentially posing security risks.
Is there a way for the delegate to execute specific necessary tasks and elevate their privileges to the highest level, like "root," but without going completely unchecked? Yes, there is a tool called "sudo" (superuser do) command that fulfills this purpose.
System administrators can use sudo to release some of their privileges to delegates, who are called "sudoers." The main advantages of using sudo over su are:
In summary, sudo allows delegates to perform tasks that would normally require superuser privileges without having to know the superuser's password. It prevents delegates from becoming too powerful and making unauthorized changes to the system, such as altering the "root" account password or causing accidental chaos.
By default, regular user accounts (excluding "root") cannot use sudo because the system cannot predict whom the system administrator wants to delegate authority to and how much authority should be released. To use sudo, the system administrator needs to edit the "sudoers" list in "/etc/sudoers" before users can become "sudoers" and utilize the command.
In "/etc/sudoers," lines starting with "#" are mostly comments and are not interpreted by sudo. Therefore, many sample lines are temporarily commented with "#" to be used as references and can be modified by removing the "#" to apply them.
The basic format of "/etc/sudoers" is "user host=[(Account to Delegate)] [TAG:] command_to_execute." The "[ ]" brackets denote optional elements. If the "privileged_user" field is omitted, the default user is set to root.
For example, after logging in as the Superuser and executing visudo to edit "/etc/sudoers," you can search for the following text:root | ALL | = | (ALL) | ALL |
↑ | ↑ | ↑ | ↑ | |
Account | Host | (Account to Delegate) | Executable Commands |
n the above example, it indicates that the "root" user can delegate authority to any user, execute any command, on any host. (ALL indicates that any condition applies)
Since the "root" account already has full power, sudo authorizations are already fully enabled for it. However, we can add another line following the same pattern:root | ALL | = | (ALL) | ALL |
aaa | ALL | = | (ALL) | ALL ←Added line following the same pattern |
↑ | ↑ | ↑ | ↑ | |
Account | Host | (Account to Delegate) | Executable Commands |
n this example, it allows the "aaa" user to execute any command authorized by any user on any host, including remote logins.
However, granting "aaa" the ability to execute any command is very dangerous. In practice, it's more common to restrict the scope of executable commands. Here's a practical example:aaa | ALL | = | (root) | /sbin/shutdown |
↑ | ↑ | ↑ | ↑ | |
Account | Host | (Account to Delegate) | Executable Commands |
In this case, it allows the "aaa" user to execute the "shutdown" command, but only when authorized by the "root" user.
Next, you can test whether "aaa" can execute a command typically restricted to the Superuser by using sudo:
Example: (Testing the ability to execute "root"-restricted "shutdown" using sudo while logged in as "aaa")$ sudo /sbin/shutdown -k now ←If the command is not in the user's "$PATH," enter the full path Passwd: ←Enter the password for the "aaa" user, not the "root" password! Broadcast message from root (tty1) (The Oct 8 12:45 2016): ←Initiating shutdown |
The example above is common since many systems do not allow regular users to issue shutdown commands, and only the Superuser can do so. Other commands like reboot and poweroff might have their permissions removed or modified.
For instance, if the "aaa" user is always the last to leave the company, the system administrator can make the modification as shown above to allow the "aaa" user to execute the shutdown command via sudo at the end of the day to save energy and reduce carbon footprint.
Additionally, in "/etc/sudoers," the user field can also be a group, but the group name should start with "%" For example:%wheel ALL=(root) /sbin/shutdown |
aaa ALL=(root) NOPASSWD: /sbin/shutdown ←Adding NOPASSWD: to the TAG section |
aaa,bbb | ALL | = | (root) | /sbin/shutdown, /sbin/halt |
↑ | ↑ | ↑ | ↑ | |
Account | Host | (Account to Delegate) | Executable Commands |
Account | Host | (Account to Delegate) | Executable Commands | |
aaa,bbb | ALL | = | (root) | /sbin/shutdown, /sbin/halt |
↑ | ↑ | ↑ | ↑ | |
User_Alias |
Host_Alias |
Runas_Alias |
Cmnd_Alias |
The syntax for setting aliases is: "Alias_Type NAME = item1, item2, ..."
where "Alias_Type" can be one of the following four types: User_Alias, Runas_Alias, Host_Alias, or Cmnd_Alias.
Let's take an example using "User Alias." Suppose we have the following alias declaration: "User Alias ACC = john, candy, lily." This means that the user alias "ACC" is equal to the user accounts "john," "candy," and "lily."
It's important to note that the "NAME" used for the alias (e.g., "ACC" in the example) must be in uppercase. If you want to add or remove user accounts from the "ACC" alias, you can simply modify this line.
Here are the detailed usages of the four Alias Types:
User_Alias TURN_OFF = aaa,bbb ←"TURN_OFF" alias includes users "aaa" and "bbb" TURN_OFF ALL=(root) /sbin/shutdown |
# Starting with "#" denotes comments, although not interpreted by sudo, adding comments is recommended for future maintenance User_Alias ADMI = %wheel # ↑ "ADMI" alias includes "wheel" group User_Alias PRINT = ken,emma,cherry # ↑ "PRINT" alias includes users "ken," "emma," and "cherry" # "!" at the beginning of an item excludes members from the alias User_Alias PT = USER, PRINT, !ken, !ADMI # ↑ "PT" alias includes users from both "USER" and "PRINT" aliases, but excludes users "ken" and members of "ADMI" alias |
User_Alias ADMI = %wheel User_Alias PRINT = ken,emma,cherry User_Alias PT = USER, PRINT, !ken, !ADMI ADMI ALL=(root) /usr/sbin/useradd, /user/sbin/userdel PRINT ALL=(ALL) /usr/sbin/lpc, /usr/sbin/lprm PT xxx=(xxx) NOPASSWD: xxx ←"xxx" to be defined by the user |
Host_Alias LAN = 192.168.0.0/255.255.255.0 #↑This is for all of network Host_Alias SERVERS = master, mail, www Host_Alias RD_DEP = net_sw, net_hw, !SERVERS ADMI LAN=(root) /sbin/ip |
Runas_Alias PT = #505, austin, jam, !WHEEL_GRP Runas_Alias ADMIN = #0 ← "uid"=0, i.e., root %wheel ALL=(ADMIN) ALL ←Example using "Runas_Alias" to replace the privileged user. |
Cmnd_Alias SHUTDOWN_CMD = /sbin/shutdown, /sbin/halt Cmnd_Alias ACC_CMD = /usr/sbin/useradd, /usr/sbin/passwd, /usr/sbin/visudo aaa ALL=(root) SHUTDOWN_CMD ←Example using command alias to replace commands |
aaa ALL=(root) NOPASSWD: /sbin/shutdown, /sbin/init #↑The user "aaa" can execute the "shutdown" command without entering a password |
aaa ALL=(root) NOPASSWD: NOEXEC: /sbin/shutdown, /sbin/init #↑ Multiple tags can be used together like this |
Syntax:sudo [-otpiton][--option] [USER_NAME][COMMAND] | ||
Command name/Function/Command user | Ooptions | Function |
sudo/ superuser do/ Any |
-b | Run the command in the background |
-E | Preserve the current user's environment variables | |
-H | Set the HOME environment variable to the new identity's HOME | |
-k | Require entering the user's password again when running sudo | |
-l | List the delegated commands (lowercase L) | |
-p [%u][%h][%H] |
Change the password prompt. Options include: %u: Prompt with the username. %h: Prompt with the hostname. %H: Prompt with the hostname + domain name |
|
-u [username] | xecute the command as the specified user (default is root). sudo -v # Extend the password expiration period by 5 minutes | |
-v | Extend the password expiration period by 5 minutes | |
-V | Display version information | |
--help | Display the command's built-in help |
$ sudo -l ←To see which commands you can run with sudo User aaa may run the following commands on this hust: (root) /sbin/shutdown /sbin/halt $ sudo -u lee cp fileA fileB ←To copy fileA to fileB with "lee" as the owner |
$ sudo -k ←Next time you run sudo, you'll need to enter the password $ sudo -p %u /sbin/shutdown -k now ←Use the username as the password prompt $ sudo -H -u smith ←Set HOME environment variable to "smith"'s HOME $ sudo cd /root ←Trying to use sudo with "cd" command sudo: cd: command not found ←sudo doesn't support shell built-in commands (like "cd") |
kevin SERVERS=(root) ALL |
# Defaults specification Defaults syslog=auth Defaults log_year, logfile=/var/log/sudo.log |
[note1A]:The default is to operate sudo within 5 minutes without entering the password.