Difference Between sudo and su Commands in Linux

Both sudo and su are commands used in Linux and other Unix-based systems to switch to a different user account with elevated privileges. However, there are some differences between the two commands, as explained below.

sudo command

The sudo command (short for “superuser do”) is a Linux/Unix command that allows a user with administrative privileges to execute a command as the root or super user. To use sudo, you need to have the appropriate permissions, your user account or group should be defined in the sudoers configuration file, located at /etc/sudoers.

By default, only an administrative user created while installing the operating system can use sudo, but you can add other users to the sudoers file to grant them the necessary permissions to use sudo.

When you prefix a command with sudo, you are requesting the command to be executed with elevated privileges of the root user. This is useful when you need to perform a task that requires administrative rights, such as installing or updating software, modifying system files or directories, or changing system settings. All these and more are actions that can only be performed by the root user.

sudo Usage Examples

For example, to update the package list on a Debian-based Linux distribution such as Ubuntu, using the apt-get or apt package manager, with elevated privileges, run the following command. You will be asked to enter your account password:

$sudo apt-get update 
OR
$sudo apt update

Then you can install a package as follows. This example shows how to install the vim command-line editor:

$sudo apt-get install vim 
OR
$sudo apt install vim 

Here is another example to create a subdirectory called myapp in the directory /var/www/, owned by the root user. Then open the file index.html for editing using the Vim editor:

$sudo  mkdir  /var/www/myapp
$sudo  vim /var/www/myapp/index.html

Note: It’s important to use sudo with caution, as executing commands with elevated privileges can be dangerous. Always double-check the command you’re about to run and make sure you understand what it does before using sudo.

su command

su (short for “substitute user” or “switch user”) allows you to switch to any user account, including the root user account, by providing the correct password. Once you have switched to the new user account, you have access to all the privileges associated with that account.

su Usage Examples

If run without any options and arguments, su will switch to the root account. You will be prompted to provide the root account password:

$su

You can also use sudo command with su, as follows. In this case, you will be asked to provide your account password:

$sudo su

To switch to a different user account, provide the username. You will be prompted to provide the user’s account password:

$su  fossman
OR
$su  -  fossman 

Conclusion

In summary, su is used to switch to another user account with all its associated privileges, while sudo is used to execute a specific command as another user, typically the root user, without having to switch to that user account. For more information, see the man page for each command by running man sudo or man su.

You may also like...

Leave a Reply

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