In an earlier article, we have explained to you the difference between sudo and su commands in Linux. These are two important commands used to implement security in Linux, in regards to user management policy and user permissions.
The su command is used to switch to another user, in other words change user ID during a normal login session (that is why it is sometimes referred to as switch (-)
user by a number of Linux users). If executed without a username, for example su -
, it will login as root user by default.
A common challenge faced by new Linux users is understanding the difference between “su” and “su -“. This article will help you briefly understand the difference between “su” and “su -“ in Linux systems.
Usually, to become another user or login to other user, you can invoke the following command, then you will be prompted for the password of the user you’re switching to.
$ su tecmint
Considering the scenario in the screenshot above, it is significant to note that the user tecmint
keeps the environment from user aaronkilik’s original login session, the current working directory and the path to executable files also remain the same.
As a result, when user tecmint tries to list the working directory (which is still user aaronkilik’s working directory), the error: “ls: cannot open directory .: Permission denied” is displayed.
But at the end, user tecmint can list his home directory after running cd command without any options.
Secondly, when you invoke su
with a -
, or -l
or --login
flags, it offers you a login interface similar to when you are logging on normally. All the commands below are equivalent to each other.
$ su - tecmint OR $ su -l tecmint OR $ su --login tecmint
In this case, the user tecmint is provided his own default login environment, including path to executable files; he also lands into his default home directory.
Importantly, when you run su
without a username, you will automatically become the superuser. You will be given root’s default environment, including path to executable files changes. You will also land into root’s home directory:
$ su
Also check out: How to Show Asterisks While Typing Sudo Password in Linux
We hope you find this article informative. You can ask any questions or share your thoughts via the comment section below.
The examples would be more convincing if you used pwd instead of ls.
you are correct
How can i use (su) to switch to another user account and then use
(su -)
to switch to another user and notice the difference ?@Ali
By trying to run the two commands against a user account.
You didn’t answer the question of the title of the article and you gave erroneous information (for my Debian installation).
su – will ask for the root password and switch me to root user and give me the root environment, pwd shows /root
su (without the dash) will ask for the root password and switch me to root user and leave me in the original user’s environment, pwd shows /home/originaluser
@V
We will review and update the article soon. Thanks for the feedback.
The article does NOT answer the question posed: What is the difference between “su” and “su –“?
E.g., on Sparky Linux, if you switch to root in a terminal by “su” then the root password, you will NOT get access to “swaplabel“. One has to switch to root as “su –” and the root password NOT simply “su” in order to be accorded all root paths.
The question was simple, so’s my answer.
@Larry
We will review and update the article soon to answer the question. Many thanks for the useful feedback.
When running
su
and typing the password it returns “Authentication failure”@Review
The password you entered is possibly wrong. Ensure that you enter the correct password.
From the manpage of ‘su‘ (on opensuse).
#####8<#####
NAME
su – run a command with substitute user and group ID
SYNOPSIS
su [options] [-] [user [argument…]]
DESCRIPTION
su allows to run commands with a substitute user and group ID.
When called without arguments, su defaults to running an interactive shell as root.
For backward compatibility, su defaults to not change the current directory and to only set the environment variables HOME and SHELL (plus USER and LOGNAME if the target user is not root). It is recommended to always use the
–login option (instead of its shortcut -) to avoid side effects caused by mixing environments.
#####8<#####
So, the su means 'switch user', with the default user being the "top" administrative user, as Aaron explains
@Jonix
We do not have to copy and paste the definition from man pages in our articles, we simply explain in terms users can easily understand and relate with. Of course, you can always find more information in man pages.
And different distros offer different command/program descriptions. For example on Ubuntu or Linux Mint, the man page says:
“NAME su – change user ID or become superuser”
Thanks for sharing your thoughts with us.
su without a username and you become the root user not super user. There is no such thing as super user, just root user. That super user crap started years ago and is still perpetuated. sheesh
@Marshall
I guess we have fallen victims of using superuser to mean root user. However, the two words “root” and “superuser” are used interchangeably in relation to Unix/Linux operating systems today. According to Wikipedia, “superuser is a special user account used for system administration. Depending on the operating system (OS), the actual name of this account might be root, administrator, admin or supervisor.”
Thanks for sharing your concern with us.
During the install, some distros ask if the first user created is to be the administrator. If you say YES, that user becomes the superuser but he DOES NOT become the root.
@dragonmouth
Yes, this is correct. But in the article, we are referring to root as the superuser(vice versa), who has absolute permission to perform any kind of task on a Linux system, without using sudo command.
Thanks for pointing this out.
One interesting thing that can be done with sudo is starting a “longtime” shell by issuing ‘sudo bash‘ ; instead of issuing root-claimimg commands one at a time (do remember to logout of this subshell when finished, to avoid stupid things done with the power-of-root)
@Jonix
Many thanks for the useful tip.
And what about”sudo su” and “sudo su -“
@Phil
When a user invokes sudo, then that user gains root privileges. But the cases explained above( for su and su -) will apply but to the root user.