Difference Between “su” and “su -” Commands in Linux

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Introduction

As a new Linux user, you may always face confusion regarding the difference between su command and su – command.

This article will help you to understand the difference between “su” and “su -“ in Linux.

Brief

  • The su command used to switch to another user.
  • That’s meaning the it changes user ID during a login session, For this reason it referred to as switch (-) user by a number of Linux users.

If it has being run without a username as su –, it will login as root user by default.

  • Also we need to know what is the Linux User Environment 

Linux User Environment: Linux’s systems are multi-user environments. Whenever Linux operating system creates a new shell session it started preparing an environment for itself. This environment holds the Environment variable(Environmental depends on shell type, Bash used by most of the Linux distribution).

For example, pwd command is an environmental variable.

Difference between su & su – command

  • su command is an abbreviation for “substitute user” because it is used for switching to another user during a normal login session, but it is often mistaken as an abbreviation for super user .
  • su command used for getting “super user” privileges as when su command run without a username, we automatically become the superuser after giving the correct root password and after that, we will enter root’s default environment, by only typing the following command:
su

Now you logged in as root (super user)

Directory Differences between su & su –

  • When we switch from one user to another user using the normal su command the current directory remains the same as of the previous user as shown below:
su user
  • When we switch from one user to another user using su – command the current directory changes to the home directory of the target user as shown below:
su - user

Path Differences between su & su –

  • su command does not create a new User Environment but su – creates a totally new User Environment that we can check with environment variables as shown below
echo $PATH

For su command

For su – command

Note: when you invoke these flages (su or or -l or –login), it offers you a login interface similar to when you are logging on normally. All the commands below are equivalent to each other.

su - unixcop
su  -l unixcop
su --login unixcop
  • For more info check the su man page with running the following command:
man su
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
MQ-Jr
MQ-Jr
unixcop Admin

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook