Introduction
su stands for switch user is the command that allows you to run commands under other users without changing functionality.
Using su is the best way to switch to the root user allowing you to perform administrative tasks in special cases that cannot be done by normal users.
The following article will guide you in detail on how to use the su command in Linux.
The syntax of su command
$ su [options] [username [arguments]]
[options]
-h show help information
$ su -h
Output:
-c run command with specified user
For example, I want to run the df command as the root user:
$ su -c df
Output:
-l change the user name
For example, my original user name is ubuntu. Now, I will change it to guest. You must enter the password of guest to change:
$ su -l guest
Output:
-s run the shell environment you want
For example, I want to run the bash shell:
$ su -s /usr/bin/bash
Output:
-p replace the user name you want to switch to
For example, I want to switch to guest:
$ su -p guest
Then run the echo $HOME command to check the directory you are in:
$ echo $HOME
su vs. sudo
Both sudo and su are used to run as root but in different ways. But how are they different? To know this difference, we first need to understand about root privileges and root users. In short, the root is the maximum authority you can gain, allowing you to do anything with the system. Besides the fact that the root user can install/remove some packages, root privileges also act as an extra layer of security.
Key difference:
The su command represents the highest authority of the root user. su will launch a new shell.
sudo uses a config file (/etc/sudoers) to define the permissions of different users.
After the comparison, we see sudo for users to use their own account to run system commands. su forces the user to share the root password with other users. That’s why sudo doesn’t start any new shell windows.
Conclusion
You have just seen a detailed tutorial on how to use the su command in Linux and the difference between su and sudo.
Thank you for reading.