Difference Between Sudo and Su Commands in Linux

Learning how to use the Linux operating system can be a complex journey, especially for beginner system administrators. Two fundamental commands that you will encounter while using Linux are sudo and su.

Understanding the difference between the sudo and su commands is important for you to follow Linux system security best practices.

This article will show you the key features of sudo and su, and it will go through an in-depth comparison between the two commands.

Understanding the Basics of sudo and su

The commands sudo and su are both used to handle user security and control in Linux. They allow you to execute commands with different user privileges than those of the current logged-in user.

Sudo stands for “superuser do”. It is a command used to run programs or commands with the security privileges of another user, typically root (the Linux superuser). It is a powerful tool that offers fine-grained control over permissions, allowing specific users to execute certain commands as root or another user while providing an audit trail of the commands run with sudo.

Su stands for “switch user” or “substitute user”. It is a command used to switch the current user to another user. When used without specifying a username, su switches to the root user. It requires the password of the user you are switching to.

Features of the sudo and su Linux commands

Features of the sudo command

The following table explains the key features of the sudo command:

FeatureDescription
Selective AccessLinux Systems Administrators can configure which commands a user can run with sudo in the /etc/sudoers file.
Temporary Privilege EscalationUsers remain in their session, and they only gain root privileges for the duration of the command.
Audit and SecurityEvery sudo command is logged. This increases security and user accountability.
Root Password Not RequiredUsers authenticate with their own password, not the root password.

Example of sudo command

Let’s say you want to update the package lists on your Ubuntu system. This action requires root privileges since it affects system-wide configurations. Here is how you would do it using sudo:

sudo apt update

After entering this command, you might be prompted for your password. Once authenticated, the command will execute with elevated privileges.

Features of the su command

The table below shows the key features of the su command:

FeatureDescription
User SwitchingAllows switching to any user account, including root, but to do that you have to know the user’s password.
Full Environment ChangeWhen switching users, it allows changing to the environment of the target user (e.g. PATH and environment variables).
Root AccessCommonly used to gain root access by simply typing su and entering the root password. This can be dangerous if you are not an experienced systems administrator because root has full control of the Linux system.

Example of su command

Suppose you need to execute tasks or run commands as the ‘apache’ user, which is commonly used for managing the Apache HTTP Server on a Linux system. To switch to the ‘apache’ user from your current user session, you can use the su command as follows:

su - apache

After entering this command, you will be prompted to enter the ‘apache’ user’s password. Once authenticated, your user session switches to the ‘apache’ user, allowing you to perform tasks or run commands as this user.

Differences Between Sudo and Su Commands

To better comprehend the differences between the sudo and su commands, I have created a table that compares the two commands based on a set of criteria important when managing Linux systems.

FeatureSudoSu
AuthenticationRequires the current user’s password when executing a command.Requires the password of the user you are switching to.
Privilege ScopeExecutes a single command with elevated privileges.Switches to a different user account entirely.
Configuration File/etc/sudoers file for command permissions (sudo rules).No specific configuration file for permissions.
LoggingLogs each command executed.Does not log commands executed after switching user.
EnvironmentMaintains the current user’s environment.Changes to the target user’s environment if the - flag is provided (su - newuser).
UsageIdeal for executing specific privileged commands.Used for switching to another user, especially for root.
Granularity of ControlAllows fine-grained control over commands and privileges.Grants complete access to the target user’s privileges
Typical Use CaseAdministrative tasks requiring specific privileges.Full environment access as another user, often for root.
SecurityEnhanced security through specific permissions.Less granular control and a higher risk if used improperly.
Common PracticePreferred for administrative tasks due to logging.Preferred when a complete switch of user environment is needed.

When managing the security of your Linux systems, it is recommended to use sudo for limited and pre-approved administrative tasks due to its logging capabilities and granularity of control.

On the other side, use su when a complete environment change to another user, like root, is necessary.

IMPORTANT: Make sure full root access via the su command is only granted if absolutely necessary and only if the user has extensive Linux experience.

FAQ

  1. Q: Can I use su instead of sudo?
    • A: You should use su only when you need to switch to another user account completely (e.g. the root user) for an extended period. If you just have to execute a single command or a limited number of commands with elevated privileges, use sudo.
  2. Q: What are the disadvantages of using su?
    • A: The su command requires the password of the user you are switching to. Also, it has less granular control and introduces a higher risk if used improperly or by a user with limited Linux experience.
  3. Q: What commands require sudo?
    • A: Commands that require sudo are those that need elevated privileges to execute. For example, commands to apply system-wide configuration changes, install or update software, modify system files, or access restricted directories.
  4. Q: Can multiple users be configured to use sudo?
    • A: Yes, multiple users can be configured in the /etc/sudoers file. Each user can have their specific permissions and command restrictions. This allows granular access control for each user.
  5. Q: What happens if I don’t specify a username with su?
    • A: If you don’t specify a username, su defaults to switching to the root user. You will be prompted to enter the root password.
  6. Q: What is the meaning of the command “sudo su – <user>?”
    • A: The command sudo su - <user> combines sudo and su to switch to another user account with a login shell. It first uses sudo to gain superuser (root) privileges and then su to switch to the specified user account. The - after su ensures that the environment is changed to that of the target user. This command is often used when you need to switch to another user but do not know their password; instead, you use sudo privileges to bypass the password requirement of su. For instance, sudo su - codefather would switch to the ‘codefather’ user account, with the environment and home directory of ‘codefather’.

Conclusion

Both sudo and su are integral to Linux system administration. Understanding their differences and when to use each one is very important for maintaining secure systems.

Make sure to familiarize yourself with both commands, their configurations, and their key features to enhance your Linux systems administration skills.

Related article: as a next step, go through the following tutorial that will give you practical knowledge about the sudo command in Linux.

Leave a Comment