Skip to main content

Command Palette

Search for a command to run...

Beginner's Guide to Linux: Mastering sudo and su Command :

Beginner's Tutorial for Mastering Linux Commands :

Published
β€’4 min read
Beginner's Guide to Linux: Mastering sudo and su  Command :
R

A πŸš€ Passionate Linux and Cloud Computing Student . 🌐 Enthusiast in DevOps and System Administration πŸ§‘β€πŸ’».

Managing user permissions and access is a critical aspect of Linux administration. Two powerful commands, sudo and su, play a significant role in controlling user privileges. In this blog, we'll explore these commands, explaining their functionalities, concepts, and providing a comprehensive list of flags and commands.

Introduction to sudo and su :

In Linux, regular users typically operate with limited permissions for security reasons. However, certain tasks require elevated privileges (root access). This is where the sudo and su commands come into play:

  • sudo : Allows a permitted user to execute a command as the superuser or another user, as specified by the security policy.

  • su : Switches to another user account in the current session, typically used to switch to the superuser account.

The sudo Command :

The sudo command stands for "superuser do." It enables users to run specific commands with elevated privileges without logging in as the root user.

Basic Usage :

To run a command with superuser privileges, use:

shCopy codesudo command

Example :

shCopy codesudo apt update

This command updates the package lists for upgrades and new installations. The sudo prefix ensures the command runs with the necessary permissions.

Common Flags and Options for sudo :

  1. -l or --list : List the allowed (and forbidden) commands for the invoking user.

     shCopy codesudo -l
    
  2. -v or --validate : Update the user's timestamp, extending the sudo session.

     shCopy codesudo -v
    
  3. -k or --reset-timestamp : Invalidate the user's timestamp, requiring a password for the next sudo command.

     shCopy codesudo -k
    
  4. -u [user] : Run the command as a specified user.

     shCopy codesudo -u username command
    

    Example :

     shCopy codesudo -u postgres psql
    
  5. -i or --login : Run the shell specified by the target user's password database entry as a login shell.

     shCopy codesudo -i
    
  6. -s or --shell : Run the shell specified by the SHELL environment variable if set, or the shell listed in the passwd entry.

     shCopy codesudo -s
    
  7. -b or --background : Run the command in the background.

     shCopy codesudo -b command
    
  8. -H or --set-home : Set the HOME environment variable to the home directory of the target user.

     shCopy codesudo -H command
    
  9. -E or --preserve-env : Preserve the user's environment when running the command.

     shCopy codesudo -E command
    
  10. -- : End of command options. Useful if the command to be run with sudo has flags or arguments starting with a dash (-).

    shCopy codesudo -- command --option
    

The su Command :

The su command stands for "substitute user" or "switch user." It allows you to switch to another user account in your current session.

Basic Usage :

To switch to the root user, use:

shCopy codesu

You'll be prompted to enter the root password.

To switch to a specific user, use:

shCopy codesu - username

Example :

shCopy codesu - john

This command switches to the user account john and loads the user's environment.

Common Flags and Options for su :

  1. - : Start a login shell, which resets all environment variables.

     shCopy codesu -
    
  2. -c [command] : Run a specific command as the target user.

     shCopy codesu -c "command" username
    

    Example :

     shCopy codesu -c "ls /home" john
    
  3. -m or -p : Preserve the current environment, including the HOME variable.

     shCopy codesu -m
    
  4. -l or --login : Equivalent to -.

     shCopy codesu -l username
    
  5. -s [shell] : Run a specified shell instead of the default shell.

     shCopy codesu -s /bin/bash
    
  6. -f : Fast login. Only set the user ID and group ID.

     shCopy codesu -f username
    

Practical Examples :

Using sudo to Install a Package :

shCopy codesudo apt install vim

This installs the vim text editor using superuser privileges.

Using su to Switch to Another User :

shCopy codesu - jane

This switches the current session to the user jane.

Running a Command as Another User with sudo :

shCopy codesudo -u postgres psql

This runs the psql command as the postgres user.

Running a Command with su and Returning to Original User :

shCopy codesu -c "ls /home" john

This lists the contents of /home as the user john and then returns to the original user.

Conclusion :

Understanding and effectively using sudo and su is essential for managing user permissions and maintaining security in Linux. These commands offer flexibility and control, allowing users to execute tasks with the necessary privileges while keeping the system secure. By mastering their usage and flags, you'll enhance your ability to manage a Linux system efficiently.

If you have any questions or need further clarification, feel free to leave a comment below! Happy Linux-ing!

Thank You πŸ™β€οΈπŸ˜Š.

Linux Basics To Advance

Part 1 of 9

In This Series I Will Upload Linux Blogs From Basic To Advance. Stay TunedπŸ™β€οΈπŸ˜Š .

Up next

Simplifying Password Management with the chage Command in Linux :

Linux Password Aging Explained for Beginners :