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:
Feature | Description |
---|---|
Selective Access | Linux Systems Administrators can configure which commands a user can run with sudo in the /etc/sudoers file. |
Temporary Privilege Escalation | Users remain in their session, and they only gain root privileges for the duration of the command. |
Audit and Security | Every sudo command is logged. This increases security and user accountability. |
Root Password Not Required | Users 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:
Feature | Description |
---|---|
User Switching | Allows switching to any user account, including root, but to do that you have to know the user’s password. |
Full Environment Change | When switching users, it allows changing to the environment of the target user (e.g. PATH and environment variables). |
Root Access | Commonly 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.
Feature | Sudo | Su |
---|---|---|
Authentication | Requires the current user’s password when executing a command. | Requires the password of the user you are switching to. |
Privilege Scope | Executes 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. |
Logging | Logs each command executed. | Does not log commands executed after switching user. |
Environment | Maintains the current user’s environment. | Changes to the target user’s environment if the - flag is provided (su - newuser ). |
Usage | Ideal for executing specific privileged commands. | Used for switching to another user, especially for root. |
Granularity of Control | Allows fine-grained control over commands and privileges. | Grants complete access to the target user’s privileges |
Typical Use Case | Administrative tasks requiring specific privileges. | Full environment access as another user, often for root. |
Security | Enhanced security through specific permissions. | Less granular control and a higher risk if used improperly. |
Common Practice | Preferred 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
- 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, usesudo
.
- A: You should use
- 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.
- A: The
- 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.
- A: Commands that require
- 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.
- A: Yes, multiple users can be configured in the
- 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.
- A: If you don’t specify a username,
- Q: What is the meaning of the command “sudo su – <user>?”
- A: The command
sudo su - <user>
combinessudo
andsu
to switch to another user account with a login shell. It first usessudo
to gain superuser (root) privileges and thensu
to switch to the specified user account. The-
aftersu
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 usesudo
privileges to bypass the password requirement ofsu
. For instance,sudo su - codefather
would switch to the ‘codefather’ user account, with the environment and home directory of ‘codefather’.
- A: The command
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.
Claudio Sabato is an IT expert with over 15 years of professional experience in Python programming, Linux Systems Administration, Bash programming, and IT Systems Design. He is a professional certified by the Linux Professional Institute.
With a Master’s degree in Computer Science, he has a strong foundation in Software Engineering and a passion for robotics with Raspberry Pi.