When the first version of Unix was released, one of the most important features it had was multi-user support, which allowed users to share a single computer. For the administration of such systems, a user switching tool was needed.
In this tutorial, we’ll learn about su and the possible implications of the – argument.
2. Linux User Environment
When a new terminal starts in Linux, operating system creates a new shell session. Unless we specify otherwise, Bash is used by default for almost all of the distributions.
When a shell starts, it prepares an environment for itself. The environment acts like small key-value storage that holds the basic information that some of the commands need to work.
For example, the pwd command on Linux-based systems reads the current working directory from the PWD environment variable. The cd – command needs the OLDPWD variable to find the last visited directory. When we run any command on the shell, history about that command gets written to file at the path HISTFILE. Environment variables manage a lot of information for us in the background.
The shell creates the environment variables by invoking specific scripts. Not all shells have identical environment variables, different shell types might invoke different scripts or same scripts with a different order.
~# env TERM=xterm-256color HISTFILE=/home/test/history PWD=/home/test OLDPWD=/home/test
su is one of the core utilities in Linux. It allows users to execute commands as another user.
The most common use of the su is to get superuser privileges. It is often mistaken as an abbreviation for “super user”, but it is an abbreviation for “substitute user”.
When using su, we can run it with or without the – argument. This argument tells su to invoke a login shell, which resets all the environment variables and creates them again. If we omit this option, almost all of the existing environment variables will stay the same. This single character has a lot of implications for the commands which will be run after changing the user.
3.1. Folder Differences
When we use su – , command changes our current working directory to the home directory of the target user. In the other hand, su command doesn’t change our directory, we stay on the same directory after we make the user switch:
[email protected]:~$ pwd /home/test [email protected]:~$ su - Password: [email protected]:~# pwd /root [email protected]:~# logout [email protected]:~$ su Password: [email protected]:/home/test# pwd /home/test
3.2. Path and Command Differences
All users have their paths in environments.
For example, ls usually resides in /usr/bin/ls. But, we can call it without typing its full path. This is possible because the /usr/bin/ is already in our path, therefore commands from this location can be invoked directly.
su – changes our path to the path of the target user, while su doesn’t. Let us try to use fdisk command.
[email protected]:~$ fdisk bash: fdisk: command not found [email protected]:~$ env | grep PATH PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games [email protected]:~$ su - Password: [email protected]:~# fdisk --version fdisk from util-linux 2.33.1 [email protected]:~# env | grep PATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin [email protected]:~# logout [email protected]:~$ su Password: [email protected]:/home/test# fdisk bash: fdisk: command not found [email protected]:/home/test# env | grep PATH PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
The important thing to note here is that different users can have different commands in their paths. For example in Debian, fdisk exists in the path of the root user but not in the paths of the other users. This doesn’t mean we can’t use fdisk without invoking su – , it means we have called it with its full path.
Environment variables also affect some commands in different ways. Most shells log the commands you write in a history file. Shells read the location of this file from HISTFILE= variable.
Using su without – argument will write new commands to the history of the old user. If the argument is used, new commands are saved to the history of the target user.
3.3. Window System Differences
Some commands come with graphical user interfaces. Such commands read which display to use from environment variables. Using su – will reset our environment variables. To start a graphical application in our current display, we need to keep our environment variables by not using -.
[email protected]:~# su - Password: [email protected]:~# gparted (gpartedbin:4118): Gtk-WARNING **: 14:50:36.017: cannot open display: [email protected]:~# logout [email protected]:~# su Password: [email protected]:/home/test# gparted bash: gparted: command not found [email protected]:/home/test# /usr/sbin/gparted ====================== libparted : 3.2 ======================
Note that, gparted command is missing from the path for the existing environment, therefore we specified the absolute path for the binary.
In this article, we covered the main differences between su and su – commands in Linux.
We saw the differences in paths, directly usable commands, history files, and graphical differences.