This brief tutorial shows students and new users how to use the su command on Ubuntu 18.04 LTS.
The su command, short for substitute user or switches user, enables the current user to act as another user during the login session.
If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for beginners.
Ubuntu is an open-source Linux operating system that runs on desktops, laptops, servers, and other devices.
About su command:
The su command, short for substitute user or switches user, enables the current user to act as another user during the login session. It changes the current user ID to that of a superuser or another user that you specified.
The syntax is the rule and format of how the su command can be used. These syntax options can be reordered, but a straight format must be followed.,.
Below is an example syntax of how to use the su command.
su [options] [LOGIN]
The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the commands. they are separated by spaces and followed after the commands.
Below are some options for the su command:
|LOGIN.||Replace LOGIN.. with the username or login name you want to switch to.|
|-c, –command COMMAND||Use the -c or –command to pass COMMAND to the invoked shell|
|-, -l, –login||Use the – or -l or –login to make the shell a login shell. This provides an environment similar to the user session|
|-s, –shell SHELL||Use the -s or –shell to specify a SHELL instead of the default in passwd|
|-h, –help||Use the -h or –help to display the help message|
|Use the -m or -p or –preserve-environment to not reset environment variables, and|
keep the same shell
|–help||Display a help message and exit.|
Below are some examples of how to run and use the su on Ubuntu Linux.
Simply run the su command to invoke it.
If you run the su command but don’t specify a login name, the command automatically switches to the superuser or root and runs its interactive shell.
When you run the su command above, you’ll be prompted for the root password. If the root password is authenticated and validated, the user running the command will automatically become root.
By default, authentication will fail when you want to change to root since the root account doesn’t have a password created with your installed Ubuntu.
To add a password to the root account so you can authenticate, run the commands below
sudo passwd root
When you do that, you’ll be prompted for your password. then continue with asking to create and re-type a new password for the root account.
Output: [sudo] password for richard: Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
You’ll now be able to use the su command and it temporarily becomes root.
You can also run the su command with – or -l or –login. This makes the mimic the shell environment similar to a real login, in this case, the root.
To run another SHELL instead of the one defined by default in the passwd file, use the -s or –shell option . The below example will use the /usr/bin/zsh shell.
su -s /usr/bin/zsh
When you use su with -c or –command option, it causes the next argument to be treated as a command by most command interpreters.
su -c ls
The su command acts almost similarly to the sudo command. However, the sudo command allows users to execute programs with root privileges.
On the other hand, the su command gives a temporary root shell to users. But users must know the root password. This could be dangerous when sharing root passwords among users to gain root privileges.
In most cases, sudo is used.
When you run su with the –help option, you’ll see the help text below:
Usage: su [options] [LOGIN] Options: -c, --command COMMAND pass COMMAND to the invoked shell -h, --help display this help message and exit -, -l, --login make the shell a login shell -m, -p, --preserve-environment do not reset environment variables, and keep the same shell -s, --shell SHELL use SHELL instead of the default in passwd
Congratulations! You’ve learned how to use the su command on Ubuntu.