Linux Commands

Understanding Sudo and Su: Syntax and Functions

You are guaranteed to use the sudo or su commands when using Linux. However, most people are still confuse with the two and fail to understand their difference.

In this guide, we will understand each command and its syntax and offer some examples of the different ways of using it. That way, you will be comfortable knowing what to use and at what point.

The Sudo Command

In Linux, sudo stands for SuperUser Do. It lets the users execute the commands with administrator privileges. When you run a command with sudo, you must enter the password for the current user to authenticate the running of particular command.

You can only execute a command using sudo if you are in the sudoers file. The system administrator uses the sudoers file to allow the users to execute the different commands as root. All commands are executed as the root is logged for the administrator to check which user executes the command and what command are executed.

Here’s the syntax for the sudo command:

$ sudo command

Here’s an example of updating the apt repository. Let’s try to update it without using sudo and see what happens.

We get an error which states that we don’t have the permission to execute the command.

However, if we run the same command with sudo, we manage to execute it. But we must enter the current user’s password.

If you are logged in as the root, you don’t need to add sudo when running a command. For instance, you can run the update command as shown in the following:

Suppose you try on executing sudo on your terminal and get an error like the one in the following. It means that you are not added to the sudoers file.

However, the administrator can add you using the following syntax:

$ sudo usermod -aG [username]

That way, you can execute a privileged command as root.

The Su Command

The su command is used when you want to switch your account and log in as another user. You can also use the command to switch to the root account.

Here’s the syntax to switch to a different user account:

$ su [username]

With the su command, you will need to enter the password for the user account that you want to switch to.

To switch to the root account, use the following command:

$ su

The previous command prompts you to enter the password for the root account. However, you may run into the following error:

If that happens, it means that you have not enabled the root account.

If you want to switch to the root account without enabling it, use sudo su as shown in the following:

Alternatively, you can enable the root account with the following command:

$ sudo passwd root

You can now switch to the root account and enter the root password that you created in the previous step.

You can use various options with the su command.

To change the directory after switching to the root account and make the shell appear like a real login, add a hyphen when executing the su command.

$ su -

If you have a different shell that you want to run after switching to the root account, you can specify the -s flag and the path to the shell using the following syntax:

$ su -s [path-to-shell]

Also, you can preserve the entire environment when switching using the -p option. Here’s an example:

To exit the session and switch to the active user, type exit.

Difference Between Sudo and Su

The sudo command is used when you want to run a command that requires the root privileges. Adding sudo lets you run the command as root without the root account. If you want to switch to the root account or another user account, you can use the su command followed by the target user account.

With sudo, you must enter the password for the active account. However, su requires you to enter the password of the account that you want to switch to. Using sudo is recommended instead of switching to the root account to minimize the risks of running the commands with the root account.

Conclusion

Sudo and su are command-line utilities available for Linux. Su lets you temporarily switch to another user account, while sudo lets you run a command with administrator privileges. We’ve seen how to use the two commands with different examples. You now understand how to use the two options on Linux.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.