Difference Between sudo and su Command

Q: Can you describe what the `sudo` command does and how it differs from the `su` command?

  • Linux
  • Mid level question
Share on:
    Linked IN Icon Twitter Icon FB Icon
Explore all the latest Linux interview questions and answers
Explore
Most Recent & up-to date
100% Actual interview focused
Create Interview
Create Linux interview for FREE!

In the world of Unix-like operating systems, understanding command-line tools is essential for effective system management. The `sudo` and `su` commands are two pivotal utilities that play significant roles in user authentication and administrative privileges, making them vital for both system administrators and regular users. But what distinguishes these two tools, and why is each one necessary? The `sudo` command, which stands for 'superuser do,' allows a permitted user to execute a command as the superuser or another user, as specified by the security policy in the sudoers file.

This command is pivotal because it enables users to perform administrative tasks without needing to log in as the superuser. Instead, users can temporarily elevate their privileges for executing specific commands, promoting a safer and more controlled use of administrative rights. This feature is particularly important in environments where security and auditing are paramount, as it limits the exposure to potentially destructive actions while providing a traceable record of what commands were run with elevated privileges. On the other hand, the `su` command, which stands for 'substitute user,' allows a user to switch their current user identity to that of another user, including the superuser.

This is achieved by providing the password of the target user. The `su` command is especially useful when a user requires sustained access to another user's capabilities without needing to prepend `sudo` to every command. While it offers more direct control, it can also be seen as less secure since it grants full access to the other user’s environment without restrictions. For candidates preparing for technical interviews, a solid grasp of these commands is crucial.

You may encounter questions that not only ask for definitions but also explore practical scenarios where one command may be preferred over the other. Understanding the implications of using `sudo` vs. `su`, such as permission management and security considerations, will enhance your technical expertise, making you more appealing to potential employers in the IT field..

The `sudo` command stands for "superuser do" and allows a permitted user to execute a command as the superuser or another user, as specified by the security policy in the `/etc/sudoers` file. It is typically used to perform administrative tasks without needing to switch to the root user entirely. For example, a user can run `sudo apt update` to update package lists without logging in as root.

On the other hand, the `su` command stands for "substitute user" and is used to switch users within the terminal. When executed without any arguments, it switches to the root user and requires the root password. For example, running `su` and entering the root password allows the user to execute commands with root privileges until they exit that session.

In summary, `sudo` is used to execute a single command with elevated privileges while remaining in the user's session, whereas `su` switches the entire session to another user, usually root. This makes `sudo` more secure for avoiding full administrative access and can provide better audit capabilities since each command is logged.