Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to debug a shell environment issue on a machine running lucid.

Both root & user have /bin/bash as their shell in /etc/passwd When I use "sudo su - user", my PATH includes the following directory: /usr/local/rvm/rubies/ruby-1.9.3-p194/bin/ruby when I "ssh user@machine" it has: /usr/local/rvm/rubies/ruby-1.9.2-p290/bin/ruby

Now, there are probably a number of ruby/rvm specific answers here -- that's not what I'm looking for, what I'm trying to understand is the general problem of where to look to find whatever it is that is setting the path. I know that /etc/profile gets executed, but it is being executed in both cases so I'm not sure what the issue is -- are there some other files that also get loaded on login -- in 1 case but not the other? / anything else that is being loaded that could be setting the PATH?

So it appears that something is happening before /etc/profile is loaded. I echo'd the environment on the first line of /etc/profile, and in the ssh case, the path already included a reference to /usr/local/rvm/rubies/ruby-1.9.2-p290/bin -- in the sudo su - case it did not. It appears that /etc/environment is used in the ssh case, but not the sudo su - case...

share|improve this question

1 Answer 1

up vote 2 down vote accepted

This is probably due to the difference between login and interactive shells. See here for a nice summary.

The first file the system reads to set variables is /etc/environment. After that, which files are read depends on the way that bash has been invoked. When you ssh user@machine, you start a login shell but when you su username, you start an interactive, non-login shell. Bash will read its initialization settings from different files in each case. The following is from the bash man page (emphasis mine):

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

So, bash reads different start up files depending on its invocation. Have a look at the contents of these files in your machine, you will probably find $PATH is being defined in different ways.

That said, as @mpy correctly pointed out in the comments, sudo su - user should start a login shell. Are you sure you are using sudo su - and not simple sudo su or just su? The - should start a login shell which should read exactly the same start up files as ssh user.

share|improve this answer
But shouldn't su - user also start a login shell? –  mpy Mar 23 '13 at 13:35
@mpy yes, quite right, I hadn't noticed the OP was using -. –  terdon Mar 23 '13 at 13:48
right -- when I su without the -, the effect is the same as when I ssh -- it uses the older version of ruby. Sounds like the files should be the same, so I'll keep digging. –  Kem Mason Mar 24 '13 at 0:47
Thanks for the info on include order -- if you'll edit your answer to include an /etc/environment reference, I'll be happy to accept it. –  Kem Mason Mar 25 '13 at 20:09
@KemMason done. Never thought to include it since it is read before everything else and should be the same no matter how you start your shell. –  terdon Mar 25 '13 at 20:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.