Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash sudo uso: Execute commands with elevated privileges
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash sudo uso: Execute commands with elevated privileges

Overview

The sudo command allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. It is commonly used to perform administrative tasks that require higher privileges than those available to a standard user.

Usage

The basic syntax of the sudo command is as follows:

sudo [options] [command]

Common Options

  • -u [user]: Run the command as the specified user instead of the default superuser.
  • -k: Invalidate the user’s cached credentials.
  • -l: List the user’s privileges or the commands they are allowed to run.
  • -i: Execute the command in a login shell as the target user.
  • -s: Run the command with the target user’s shell.

Common Examples

Here are some practical examples of using the sudo command:

  1. Update package lists:
    sudo apt update
    

    This command updates the package lists for upgrades and new package installations.

  2. Install a package:
    sudo apt install vim
    

    This command installs the Vim text editor.

  3. Edit a system file:
    sudo nano /etc/hosts
    

    This command opens the /etc/hosts file in the Nano text editor with elevated privileges.

  4. Restart a service:
    sudo systemctl restart apache2
    

    This command restarts the Apache web server.

  5. Check user privileges:
    sudo -l
    

    This command lists the commands the user is allowed to run with sudo.

Tips

  • Always be cautious when using sudo, as it can modify system files and settings.
  • Use sudo -k to clear your cached credentials if you want to require a password for the next sudo command.
  • Consider using sudo -u [user] to run commands as a different user when necessary, which can help in managing permissions effectively.