2

I logged on to my Linux server (Redhat AS4) through SSH as the user 'guest', then run the command echo $PATH, and the output is /usr/local/bin:/bin:/usr/bin.

However, when I run the command su - guest to switch to the same user, then run the command echo $PATH, the output becomes /usr/bin:/bin:/usr/local/bin.

Why the same user has different PATH value on Linux server? That makes me get different Python versions and I don't know what causes this problem.

Paul T.
  • 119
  • 6
bluesea007
  • 43
  • 1
  • 4

5 Answers5

1

Check the fields ENV_SUPATH and ENV_PATH in /etc/login.def

For good scripting practice, do not rely on the PATH variable to execute a command as it is ambiguous.

Michael Chan
  • 188
  • 1
  • 9
1

Different scripts (doing different manipulation on the PATH) are sourced in the two cases.
Since you are using the - option, su should source your login script, while ssh should not.

I don't know which behavior you want exactly, but maybe you could check if some su options (e.g. -m, --preserve-environment: do not reset environment variables) could help?

Paolo Tedesco
  • 1,296
  • 7
  • 16
  • 23
  • Thanks a lot :) After ssh to the server, I can get a beautiful look of the terminal text, but when I use "su - thesameuser", the color of text is gone. I want to know whick scripts cause the difference. Can you give me any advice on that? Thanks. –  Sep 01 '09 at 10:12
  • Use the env command. It's prohably $LS_COLORS, if not, some other environment variable. Why not just ssh to localhost instead of using su? – reinierpost Sep 01 '09 at 11:42
1

It goees like this:-

In /etc/passwd where the user id is defined a home directory and a default shell:

auser:!:97:51::/home/user:/usr/bin/ksh

When auser logs in the 'ksh' or bash or whatever will run this will pick up a default 'PATH' from the '/etc/environment' file (and some shells will run a system wide initialisation file), but, it will also run the commands from an initial script in the home directory in the above case the '.kshrc' file.

So the PATH can be set by the initial command, the system wide settings for that initial command and the 'hidden' .rc files in the users home directory.

The 'su -' does not run the same set of initial commands as login only picks up the default settings from the '/etc/environment' file.

1

Try to debug using the technique described in my answer on superuser, using

debug_msg "running ~/.bashrc ($0), PATH = '$PATH'"

and similar for /etc/bashrc, /etc/profile, etc.

hlovdal
  • 1,115
  • 11
  • 18
  • Thanks for you answer:) With the function, I found that the PATH's value is already different at the begining of /etc/profile. I can't found where the PATH is set before the /etc/profile script. Could you give me some advice? Thanks. – bluesea007 Sep 02 '09 at 06:22
  • 1
    Try "find /etc -type f -print0 | xargs -0 grep -l -w PATH" to find potential candidates. – hlovdal Sep 02 '09 at 15:20
0

from su manpage

su is used to become another user during a login session. Invoked with-
       out a username, su defaults to becoming the super  user.  The  optional
       argument  -  may be used to provide an environment similiar to what the
       user would expect had the user logged in directly.

therefore you will always get different env settings on "su" and "su -"

quaie
  • 1,122
  • 6
  • 14