The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
 

 sudo

1.0  sudo : Standing on the Shoulders of Giants
       visudo : Modify /etc/sudoerss
       /etc/sudoers : Configuration File
           Alias : Alias Settings
           TAG : Tag Settings
       sudo : Examples of Usage


ENG⇒中ENG⇒中
 
1.0 sudo : Standing on the Shoulders of Giants

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.




^ back on top ^

sudo : Examples of Usage
Usage examples of the sudo command are much simpler compared to its configuration file "/etc/sudoers". The usage is as follows:
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

Example:
$ 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

Other less commonly used usages are as follows:

Example:
$ 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")

The last example illustrates that sudo does not support executing shell built-in commands (you can use the type command to check if a command is a shell built-in). For example, trying to use sudo cd to secretly enter "/root" or any other directory will not work.

In addition, the "/etc/sudoers" file is configured as follows:
kevin         SERVERS=(root)        ALL

In this example, granting the user "kevin" authorization to use ALL commands is highly dangerous because if this user runs sudo su or sudo /bin/bash, they will immediately acquire superuser privileges.

^ back on top ^


 


 
[note]: If Fedora does not generate the log file "/var/log/sudo.log", please add the following settings in "/etc/sudoers":
# 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.