su, su -, and You

A reader recently brought to my attention the fact that he had to type su - on the command line to obtain root access rather than just su as is commonly displayed in software documentation and HOWTOs. I found that strange, as I’ve never run into that problem before; I’ve always just used su without any arguments. Some of my co-workers, meanwhile, were trained to use su - on our FreeBSD systems by a previous systems administrator.

Curious, I decided it bore a little more investigation.

I suspect part of it is distro-specific. According to the man page for su, or “switch user,” the su command alone is supposed to change the user, nothing more, and by default, if no user name is given as an argument, the default is the super user. If the dash is added (su -), then the alternate user’s environment replaces the current user’s. In other words, if the root user has more directories in his/her path statement, then these will now be available to you in su mode.

I seem to recall being forced to type in full paths after su’ing to root in the past, but it’s been a while. This is where my distro-specific theory comes in, as I tested it on my system and found I had root’s paths available to me without using the – argument.

Here’s the easy test: as a normal user, type which ifconfig. Chances are it will come back as not found as /sbin typically is not part of a regular user’s path. Now run su (without the -), then run which ifconfig again. If it’s still not found, then your path has not changed and you will need the – argument with su on your system. In Slackware 10, which ifconfig returns /sbin/ifconfig, so root’s paths have become available to me in su mode.

The obvious solution is remembering to use the – argument when you use su. However, if you’re in the habit of just entering su alone, you could add /sbin and other directories to your path statement. To do so (if you’re using the BASH shell), edit .bash_profile in your home directory and create your own path statement:

PATH="/usr/local/bin:/usr/bin:/bin:/sbin:/usr/X11R6/bin:/usr/games"

You can include as many as you need. If .bash_profile doesn't exist, you can create it, or look for .bash_login. Your default environment is probably established in /etc/profile or /etc/bashrc, and .bash_profile overrides these defaults. More information on these files and directories can be found here.

Article Written by