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]]
-h show help information
$ su -h
-c run command with specified user
For example, I want to run the df command as the root user:
$ su -c df
-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
-s run the shell environment you want
For example, I want to run the bash shell:
$ su -s /usr/bin/bash
-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.
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.
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.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.