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
Explore all the latest Linux interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Linux interview for FREE!
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.
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.


