su stands for switch user and it is a very important command used by Linux users. In fact this command allows users 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.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux systems command queries.
In this context, we shall look into how to use the su command in Linux.
What is the syntax of su command ?
The syntax of su command takes the following form:
$ su [options] [username [arguments]]
Some of its [options] is explained below:
- -h show help information and the below command shows how it works: $ su -h
- -c run command with specified user. For example, We 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, We 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, We want to run the bash shell: $ su -s /usr/bin/bash
- -p replace the user name you want to switch to. For example, We 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 fact, 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.
What is the Key difference between su and sudo?
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.
[Need assistance in fixing Linux system issues ? We can help you. ]