Command line tip - banish ‘command not found’ when using su

Sometimes, if you're following a tutorial on doing something on your Linux box and you're trying to fix something, you'll be asked to go to a terminal and type in some commands. Often, these commands need to be run as root, the administrator.

In almost all distributions except Ubuntu, you gain root access at the terminal with the command:

$ su

You might also be asked to use this:

$ su -

While they look exactly alike apart from the dash, they do something very different. If you omit the dash, you will gain root privileges, but you aren't given a login shell. Practically speaking, what this means is that you won't inherit the settings that root has set up, such as search paths.

A search path is a list of folders that the OS looks in when you type in a command. For example, ls is stored at /bin/ls, but it would be a pain to have to type the full path all the time. Because /bin is in your search path, however, you don't have to type the full path, the OS will fill it in for you.

Root's login shell by default includes more search paths, including /usr/sbin, /sbin and more. These folders contain programs designed to be run by root, so it's mostly pointless to put them in the normal user's search path.

Going back to our example now. If you omit that dash and don't get a login shell, you won't inherit the updated search path, so if you try and launch a program located in /usr/sbin or /sbin without typing the full path, it won't work.

The easy solution - always remember that dash, or always remember to type the full location of the program. No more 'command not found' messages! Well, provided you're trying to run something that is installed of course, but that's a whole separate issue. :)

Avatar for peter Peter Upfold -

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Command line tip - banish ‘command not found’ when using su

Home » Articles » Command line tip - banish ‘command not found’ when using su