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 LinuxAPT, 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. ]
This article covers how to use the su command in Linux and the difference between su and sudo. In fact, the su command changes the current user ID to that of the superuser, or another specified user.
su command syntax is:
su [options] [username]
If no username is specified, su defaults to becoming the superuser (root).
options to use with the su command:
- Username – Replace username with the actual username you want to log in with. This can be any user, not just root.
- –c or –command [command] – Runs a specific command as the specified user.
- – or –l or –login [username] – Runs a login script to change to a specific username. You’ll need to enter a password for that user.
- –s or –shell [shell] – Allows you to specify a different shell environment to run in.
- –h or –help – Show the help file for the su command.
- –p or ––preserve–environment – Preserve the shell environment (HOME, SHELL, USER, LOGNAME).
How to enable the Root User in Ubuntu ?
To enable the root user account on Ubuntu, use the following command to set a password for it. Bear in mind that Ubuntu recommends against this:
$ sudo passwd root
Sudo will prompt you for your current user account's password before you can set a new password. Use your new password to log in as root from a terminal login prompt or with the su command. You should never run a full graphical environment as the root user – this is a very poor security practice, and many programs will refuse to work.