Host Security  «Prev  Next»

Lesson 10sudo
ObjectiveExplain the use of the sudo command.

Linux sudo Command

Explain the use of the sudo command in Red Hat Linux
The 'sudo' command, which stands for "superuser do," is a powerful utility in Red Hat Linux that allows authorized users to execute commands with the privileges of another user, typically the superuser or root. This provides a controlled way for users to perform administrative tasks without requiring them to log in as the root user, thereby enhancing security and reducing the risk of unintentional damage or system compromise.
The sudo command works in conjunction with a configuration file called 'sudoers,' which defines the specific privileges and restrictions for each authorized user or user group. The sudoers file is typically located at /etc/sudoers and is managed using the 'visudo' command, a specialized text editor designed to safely edit the sudoers file while preventing syntax errors and maintaining file integrity.
The general syntax for using the sudo command is as follows:
sudo [options] command [arguments]

Here, 'command' represents the command to be executed with elevated privileges, and 'arguments' are any optional parameters for the command. The 'options' can be used to modify the behavior of the sudo command, such as specifying the target user or preserving the environment variables.
Some common use cases for the sudo command in Red Hat Linux include:
  1. Running a single command as the root user:
    sudo command
    

    For example, to update the system package list, you would use:
    sudo yum update
    
  2. Running a command as another user:
    sudo -u username command
    

    For instance, to run a command as the user 'john', you would use:
    sudo -u john command
    
  3. Opening a shell with root privileges:
    sudo -i
    

    This command provides a root shell, allowing you to execute multiple commands with elevated privileges without having to prepend 'sudo' to each command.
  4. Editing a protected file:
    sudo vi /path/to/protected-file
    

    For example, to edit the sudoers file using the 'vi' text editor, you would use:
    sudo vi /etc/sudoers
    

    However, it is recommended to use the 'visudo' command instead to safely edit the sudoers file:
    sudo visudo
    


The sudo command in Red Hat Linux is an essential tool for managing system privileges and performing administrative tasks securely. By allowing authorized users to execute specific commands with elevated privileges, the sudo command promotes a safer operating environment and minimizes the risks associated with direct root access.

The sudo command allows selected non-root users to execute restricted commands.
By using this command, access to the root account can be controlled. The user does not need access to the root password when using the sudo commad; the user can use his or her own password, and the access to the specified commands can be set to a limited amount of time.
Question: People in your office have been abusing the printer, so it has been determined that only select individuals will be able to print. Using your superuser ability, print the file named myfile.txt using lpr.
Answer:
[redhat@localhost redhat]sudo lpr myfile.txt

Explanation: The sudo command allows you to execute the "lpr" command, passing to lpr the filename "myfile.txt".

Installing and using sudo


The sudo package is supplied with Red Hat Powertools and can be installed by simply typing:
rpm -Uvh sudo-*.rpm

Using the sudo command, an ordinary user can use their own password to execute commands with superuser privileges:
sudo <command>

sudo Advantages

sudo has the advantage that it allows you to grant extended power to sub-administrators without having to grant them full access to the entire system; you can also control exactly which users can execute which commands. For ease of use, sudo remembers for a short period of time that a user has entered his or her password, so the user can execute several commands by entering a password only once.

Disadvantages of sudo

Although sudo allows certain users the ease of executing root-only commands as a regular user, it does have its disadvantages. If sudo is not configured very securely, it can be used to obtain full administrative access. By issuing the following command, sudo /bin/sh, a user could start a root shell. Also, if an attacker compromises a sudo user's account, they may have a much easier time obtaining root access.
The next lesson explains finding modified and sticky files.