How to Use sudo in Linux

Original Source: https://www.hongkiat.com/blog/linux-command-sudo/

The sudo command in Linux stands for “SuperUser DO”. It’s a powerful command that allows users to run programs or other commands with the security privileges of another user, typically the “superuser”. This can be particularly useful when certain operations require higher-level permissions than those granted to standard user accounts.

The superuser, also known as the root user in Linux, has the highest level of access to the system, being able to read, write, and modify any file. However, running commands as a superuser can potentially be risky as it could inadvertently lead to system-wide changes or data loss if a command is misused or a mistake is made.

So, to protect the system, most Linux distributions restrict direct access to the superuser account. That’s where sudo comes into play. It serves as a gatekeeper, allowing authorized users to execute sensitive operations or modify system settings while logging all commands and their respective outcomes. This can be useful for system audits and to identify how certain changes were made.

When you type sudo before a command, the system checks a special file called the sudoers file to see if the user has permission to run the command as a superuser. If they do, they might be prompted to enter their password (depending on the system settings). After authentication, the system will execute the command with superuser privileges.

Here are some ways to use the sudo command:

1. Run Command as Root

The basic usage of sudo is to run a command as the root user.

Example:

To run the ls command as root, you would use:

sudo ls
2. Switch to Root User

You can switch to the root user using sudo su. This will prompt you for your password, not the root password.

sudo su
3. Run Command as a Different User

You can use sudo to run a command as a different user using the -u option followed by the username.

Example:

To run ls as user john, you would use:

sudo -u john ls
4. Edit Files as Root

You can use sudo to edit files as the root user.

Example:

To edit the /etc/hosts file using nano as the root user, you would use:

sudo nano /etc/hosts
5. Run a Command without a Password

You can configure sudo to run specific commands without a password by adding a line to the sudoers file (use sudo visudo to edit this file).

Example:

To allow all users to run ls without a password, you would add:

ALL ALL = NOPASSWD: /bin/ls
6. Update sudo Cached Credentials

By default, sudo caches your password for 15 minutes. If you want to update the cached credentials before they expire (for example, if you’re about to leave your computer), you can use sudo -v.

sudo -v
7. Invalidate sudo Cached Credentials

If you want to manually invalidate sudo’s cached credentials (for example, if you’re done performing administrative tasks), you can use sudo -k.

sudo -k

The post How to Use sudo in Linux appeared first on Hongkiat.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *