
Su vs. Sudo: A Practical Guide to Linux Privilege Management
In the world of Linux system administration, managing user privileges is a critical task. Performing administrative actions, from updating software to modifying system configurations, requires elevated permissions. The most powerful account on any Linux system is the root user, which has unrestricted access to everything.
While you can log in directly as root, this practice is highly discouraged due to its significant security risks. A single mistake as root can have catastrophic consequences. This is where two fundamental commands come into play: su and sudo. Understanding the difference between su vs. sudo is essential for maintaining a secure and manageable system.
What is the su Command?
The su command, short for “substitute user” or “switch user,” is the traditional way to gain root privileges. When you execute su by itself, the system prompts you for the root user’s password.
$ su
Password:
#
Upon entering the correct password, you are dropped into a new shell session running as the root user. You now have complete administrative power until you type exit to return to your original user session.
While effective, this approach has major drawbacks:
- Password Sharing: To allow another administrator to use
su, you must share the root password. This is a significant security vulnerability. If that user leaves, you must change the root password and redistribute it. - Lack of Accountability: When multiple users know the root password, actions performed as root are logged as being done by “root,” not by the specific user who ran the
sucommand. This makes auditing and troubleshooting nearly impossible.
Introducing sudo: The Secure Alternative
The sudo command, which stands for “superuser do,” provides a more secure and flexible approach to privilege escalation. Instead of switching to a new root shell, sudo allows an authorized user to execute a single command with root privileges.
$ sudo apt update
[sudo] password for your_username:
Crucially, sudo prompts you for your own user password, not the root password. This fundamental difference is the foundation of its security model.
Key Differences: su vs. sudo at a Glance
Let’s break down the core distinctions between these two powerful commands.
Password Required:
- su: Requires the target user’s (usually root’s) password.
- sudo: Requires the current user’s own password.
Scope of Access:
- su: Grants a full, interactive shell with the target user’s privileges. The elevated access is persistent until you exit the shell.
- sudo: Grants elevated privileges for a single command by default. The access is temporary and limited to that specific execution.
Logging and Auditing:
- su: Provides minimal logging. System logs will show that a user switched to root, but won’t detail which specific commands were run by that user.
- sudo: Provides a detailed audit trail. Every command executed with
sudois logged in/var/log/auth.logor a similar file, clearly indicating which user ran which command and when.
Configuration and Flexibility:
- su: Offers very little configuration. You either know the root password, or you don’t.
- sudo: Is highly configurable via the
/etc/sudoersfile. You can grant users permission to run all commands, or only very specific ones, providing granular control.
Why sudo is the Preferred Choice for Modern Systems
For nearly every use case, sudo is the superior option for system administration. The benefits are clear and directly impact system security and manageability.
Enforces the Principle of Least Privilege: This security concept dictates that a user should only have the specific permissions they need to perform their job, and nothing more. With
sudo, you can grant a user permission to restart a web server without giving them the ability to delete user accounts or format a drive. This granular control dramatically reduces the potential attack surface.Enhances Accountability: The detailed logging provided by
sudois invaluable for security audits and incident response. If a malicious or accidental command is executed, you can immediately trace it back to the specific user account responsible. This clear audit trail is essential for compliance and security forensics.Improves Administrative Workflow: Managing user permissions is far simpler with
sudo. To grant a user administrative rights, you add them to a specific group (likesudoorwheel). To revoke access, you simply remove them from the group. You never need to share or change the central root password.
Getting Started with sudo Configuration: The sudoers File
The power of sudo lies in its configuration file, located at /etc/sudoers. However, you should never edit this file directly with a standard text editor. A syntax error in this file could lock you out of your system, preventing anyone from using sudo.
Instead, always use the visudo command.
$ sudo visudo
The visudo command locks the sudoers file and performs a syntax check before saving your changes. If it detects an error, it will prevent you from saving the broken file, saving you from a potential disaster.
The basic syntax for a rule in the sudoers file is:
user HOST=(runas_user) COMMAND
user: The username or group (prefixed with%) being granted permission.HOST: Which host(es) this rule applies to (usuallyALL).(runas_user): Which user the command can be run as (usuallyALLfor root).COMMAND: The specific command or command alias (ALLfor all commands).
For example, to give the user jane full sudo privileges, the line would be:
jane ALL=(ALL) ALL
To allow a user support_tech to only run package updates, the rule might be:
support_tech ALL=/usr/bin/apt update, /usr/bin/apt upgrade
Final Thoughts: Choose Smart Privilege Management
While su is a foundational Linux command, its use cases in modern administration are limited. For secure, auditable, and manageable systems, sudo is the undisputed industry standard. By leveraging its granular configuration and detailed logging, you can empower users to perform necessary tasks without compromising the security and integrity of your entire system. Adopting a sudo-first policy is a critical step toward building a more robust and secure Linux environment.
Source: https://www.tecmint.com/su-vs-sudo-and-how-to-configure-sudo-in-linux/


