The Linux Process Journey — su (Substitute\Switch User)

Shlomi Boutnaru, Ph.D.
2 min readJun 25, 2024

--

“su” is an ELF binary located at “/usr/bin/su” (or “/bin/su”) and used for running a command with a substitute user\group identifier. “su” stands for “Substitute User” or “Switch User” (https://linuxize.com/post/su-command-in-linux/). “su” is part of the “util-linux” package, we can go over the implementation of the command as part of the package’s Github repository (https://github.com/util-linux/util-linux/blob/master/login-utils/su-common.c).

Overall, when executing “su [USERNAME]” we are prompted for the password of [USERNAME]. In case we don’t provide any [USERNAME] by default we are prompted for the password of root (https://www.tecmint.com/difference-between-su-and-su-commands-in-linux/) — as shown in the screenshot below. By the way, if we are running as root we can change to a different user without providing its password — as shown in the screenshot below.

Lastly, there is a difference between “su” to “su -”. The first one retains the user’s environment variables, working directory, current user’s shell setting and the target user’s PATH variable is not updated. The second option “su -” resets the environment variables, changes the working directory to the target home folder, resets all shell’s settings and updates the PATH variable (https://www.geeksforgeeks.org/difference-between-su-and-su-command-in-linux/). By the way, it is recommended to use “su — login” instead of “su -” as a shortcut to avoid side effects caused by mixing environments (https://man7.org/linux/man-pages/man1/su.1.html).

See you in my next writeup ;-) You can follow me on twitter — @boutnaru (https://twitter.com/boutnaru). Also, you can read my other writeups on medium — https://medium.com/@boutnaru. You can find my free eBooks at https://TheLearningJourneyEbooks.com.

--

--