home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Edition 1: Linux / CD1.iso / doc / HOWTO / mini / Path < prev    next >
Text File  |  1998-10-14  |  23KB  |  574 lines

  1.   PATH HOWTO
  2.   Esa Turtiainen etu@dna.fi
  3.   v0.4, 15 November 1997
  4.  
  5.   1.  Introduction
  6.  
  7.   This document describes common tricks and problems with Unix / Linux
  8.   environment variables, especially with PATH variable. PATH is a list
  9.   of directories where commands are looked for. The details apply for
  10.   Debian Linux 1.3 distribution.
  11.  
  12.   Note! This document is in beta release status.  Please send comments
  13.   and corrections.
  14.  
  15.   2.  Copyright
  16.  
  17.   This documentation is free documentation; you can redistribute it
  18.   and/or modify it under the terms of the GNU General Public License as
  19.   published by the Free Software Foundation; either version 2 of the
  20.   License, or (at your option) any later version.
  21.  
  22.   This documentation is distributed in the hope that it will be useful,
  23.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  25.   General Public License for more details.
  26.  
  27.   You should have received a copy of the GNU General Public License
  28.   along with this documentation; if not, write to the Free Software
  29.   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30.  
  31.   3.  General
  32.  
  33.   All the Unix processes contain an "environment".  This is a list of
  34.   variables that contain name and value, both just strings that can
  35.   contain most characters.  All Unix processes have a parent process -
  36.   the process that created this process as child.  Child processes
  37.   inherit environment from parent process.  They can make some
  38.   modifications to the environment before passing it in turn to their
  39.   child processes.
  40.  
  41.   One important environment variable is PATH, a list of directories
  42.   separated by colons (':').  These directories are searched through to
  43.   find commands.  If you try to invoke command 'foo', all the
  44.   directories in PATH (in that order) are searched for an executable
  45.   file 'foo' (one with x-bit on).  If a file is found, it is executed.
  46.  
  47.   In this howto, I use term 'command' to refer executable program that
  48.   is meant to be called with short names, using the path mechanism.
  49.  
  50.   In Linux, even the low level operating system calls to start processes
  51.   (the exec family of calls) searches through directories in the PATH
  52.   variable: you can use the path mechanism anywhere where you try to
  53.   execute a command.  If exec operating system call gets a file name
  54.   that does not contain '/', it evaluates the PATH environment variable.
  55.   Even if there is no variable PATH in the environment, at least
  56.   directories /bin and /usr/bin are looked for suitable commands.
  57.  
  58.   In sh you use export command to set environment, in csh you use setenv
  59.   command.  For example:
  60.  
  61.   sh:
  62.  
  63.        PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:.
  64.  
  65.   csh:
  66.  
  67.        setenv PATH /usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:.
  68.  
  69.   C-programs can use setenv() library call to change environment.  Perl
  70.   has environment in an associative array %ENV, you can set PATH as
  71.   $ENV{PATH}="/bin".
  72.  
  73.   env command is the basic way of asking the current environment
  74.   variables.  It can be used to modify it as well.
  75.  
  76.   More information of the basic environment mechanism can be found from
  77.   manual pages 'environ', 'execl', 'setenv', info file 'env' and
  78.   documentation of shells.
  79.  
  80.   When Linux boots up, the first normal process that starts is the init
  81.   process.  It is a special process because it does not have parent.
  82.   However, it is the ancestor of all the other processes.  Init
  83.   environment will remain as environment of all the processes if they do
  84.   not touch it explicitly.  Most processes do touch.
  85.  
  86.   Init starts a group of processes.  File /etc/inittab tells what
  87.   processes the system starts.  These processes work in the environment
  88.   that is directly inherited from init - typically they are processes
  89.   like 'getty', the program that writes 'login:' to console.  If you
  90.   start PPP connections here, you must remember that you are working in
  91.   the init environment.  The system initialization is often a script
  92.   that is started here.  In Debian 1.3 initialization script
  93.   /etc/init.d/rc and it calls other initialization scripts in turn.
  94.  
  95.   The system contains many running servers (daemons) that may or may not
  96.   use the default environment.  Most servers are started from the
  97.   initialization scripts and thus they have the init environment.
  98.  
  99.   When user logs in to the system, the environment is affected by the
  100.   settings that are compiled into the programs, system wide
  101.   initialization scripts and user initialization scripts.  This is
  102.   pretty complicated and the current situation is not completely
  103.   satisfactory.  It is totally different if user logs in from text
  104.   console, XDM or from network.
  105.  
  106.   4.  Init
  107.  
  108.   Init is a parent process for all the other processes of the system.
  109.   Other processes inherit environment of the init process and the path
  110.   is the init path in the rare case that no other path is set.
  111.  
  112.   The 'init path' is fixed in the source of the init program and it is:
  113.  
  114.        /usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
  115.  
  116.   Note that init path does not contain /usr/local/bin.
  117.  
  118.   All the programs that are started from /etc/inittab work in init
  119.   environment, especially system initialization scripts in /etc/init.d
  120.   (Debian 1.3).
  121.  
  122.   Everything that is started from system initialization scripts has init
  123.   environment as default environment.  For example, syslogd, kerneld,
  124.   pppd (when started from startup), gpm and most importantly lpd and
  125.   inetd have init environment and they do not change it.
  126.  
  127.   A group of programs are started from startup scripts but the PATH
  128.   environment variable is explicitly set in the startup script.
  129.   Examples are: atd, sendmail, apache and squid.
  130.  
  131.   There are other programs that are started from boot scripts but they
  132.   change the path completely.  One such example is cron.
  133.  
  134.   5.  Login
  135.  
  136.   In text console there is a getty program waiting for user login.  It
  137.   writes 'login:' and other messages.  It is working in init
  138.   environment.  When getty gets user to log in to the system, it invokes
  139.   the 'login' program.  This program sets the user environment and
  140.   invokes the shell.
  141.  
  142.   Login program sets path as defined in /usr/include/paths.h. This
  143.  
  144.   for common users (_PATH_DEFPATH):
  145.  
  146.        /usr/local/bin:/usr/bin:/bin:.
  147.  
  148.   for root (_PATH_DEFPATH_ROOT):
  149.  
  150.        /sbin:/bin:/usr/sbin:/usr/bin
  151.  
  152.   Common user's path does not contain any sbin directories. However, it
  153.   contains the current directory, '.', which is considered dangerous for
  154.   the root user. Not even /usr/local/bin is available for the root user.
  155.  
  156.   Login path is often overwritten by shell initialization.  However, it
  157.   is possible to use other programs in /etc/passwd as user shells.  For
  158.   example, I have used the following line to start PPP when I log in
  159.   using special user name.  In this case, the pppd has exactly login
  160.   path.
  161.  
  162.        etu-ppp:viYabVlxPwzDl:1000:1000:Esa Turtiainen, PPP:/:/usr/sbin/pppd
  163.  
  164.   6.  Shells
  165.  
  166.   Often user processes are children processes of the shell mentioned in
  167.   /etc/passwd for this user. Initialization files of shells often modify
  168.   path.
  169.  
  170.   In login, the name of the shell is preceded with '-', for example bash
  171.   is called as '-bash'. This signals to the shell that it is a 'login'
  172.   shell. In this case, the shell executes the 'login' initialization
  173.   files. Otherwise some lighter initialization is performed.
  174.   Additionally, the shell checks if it is interactive - are the commands
  175.   coming from file or interactive tty. This modifies the shell
  176.   initialization so that a non-interactive non-login shell is
  177.   initialized very lightly - bash do not execute any initialization file
  178.   in this case!
  179.  
  180.   6.1.  bash
  181.  
  182.   As a normal login shell, bash 'sources' system-wide file /etc/profile,
  183.   where the system environment and path can be set for bash users.
  184.   However, it is not run when the system interprets the shell as non-
  185.   interactive. The most important case is in rsh, where remote command
  186.   is executed in the neighboring machine. The /etc/profile is not run
  187.   and the path is inherited from rsh daemon.
  188.  
  189.   bash receives command line arguments -login and -i that can be used to
  190.   set the shell as a login shell or interactive shell respectively.
  191.  
  192.   The user can overwrite values set in /etc/profile by creating a file
  193.   ~/.bash_profile, ~/.bash_login or ~/.profile. Note that just the first
  194.   one of these is executed thus differing of the logic of csh
  195.   initialization. ~/.bash_login is not executed specially for login
  196.   shells and if .bash_profile exists, it is not executed at all!
  197.  
  198.   If bash is used with name sh instead of the name bash, it emulates
  199.   original Bourne shell initialization: it sources just files
  200.   /etc/profile and ~/.profile and just for login shells.
  201.  
  202.   6.2.  tcsh
  203.  
  204.   As a login shell tcsh executes the following files in this order:
  205.  
  206.   ╖  /etc/csh.cshrc
  207.  
  208.   ╖  /etc/csh.login
  209.  
  210.   ╖  ~/.tcshrc
  211.  
  212.   ╖  ~/.cshrc (if .tcshrc is not found)
  213.  
  214.   ╖  ~/.history
  215.  
  216.   ╖  ~/.login
  217.  
  218.   ╖  ~/.cshdirs
  219.  
  220.   tcsh can be compiled to execute login scripts before cshrc scripts.
  221.   Beware!
  222.  
  223.   Non-interactive shells execute just the *cshrc scripts. *login scripts
  224.   can be used to set the path just once in the login.
  225.  
  226.   7.  Changing user ID
  227.  
  228.   7.1.  su
  229.  
  230.   Command su sets a new user id to use. If no user id is given, root is
  231.   used.
  232.  
  233.   Normally su invokes a subshell with a different user id.  With
  234.   argument '-' (more recent synonyms -l or --login) su invokes shell
  235.   like login shell.  However, it does not use login program to do this
  236.   but uses a yet another built-in path for login 'simulation' (term used
  237.   in the source code). It is:
  238.  
  239.   for normal users
  240.  
  241.        /usr/local/bin:/usr/bin:/bin:/usr/bin/X11:.
  242.  
  243.   for root user
  244.  
  245.        /sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/X11:/usr/local/sbin:/usr/local/bin
  246.  
  247.   su makes many quite subtle environment changes as well.
  248.  
  249.   7.2.  sudo
  250.  
  251.   There is a group of commands that make use of super user commands
  252.   safer.  They allow better logging, user-based restrictions and usage
  253.   of individual passwords.  Most widely used is sudo.
  254.  
  255.        $ sudo env
  256.  
  257.   executes command env as super user (if it is configured to allow it).
  258.  
  259.   sudo command has again a different approach to path handling.  It
  260.   modifies the search path so that the current directory is always the
  261.   last one.  However, it does not modify PATH environment variable.
  262.   just couple of environment variables like SUDO_USER.
  263.  
  264.   8.  Network servers
  265.  
  266.   Most network servers should not invoke subprocesses of any kind.  For
  267.   security reasons, their path should be minimal.
  268.  
  269.   An important exception is all the services that allow logging in to
  270.   the system from network.  This section describes what is the
  271.   environment in these cases.  If the command is executed in the remote
  272.   machine with rsh it gets different path than if it is executed with
  273.   ssh.  Similarly, logging in with rlogin, Telnet or ssh is different.
  274.  
  275.   8.1.  inetd
  276.  
  277.   Most network servers do not have process of their own waiting for
  278.   requests all the time.  This work is delegated to an Internet super
  279.   server called inetd.  Inetd listens for all the defined network ports
  280.   and starts the appropriate server when there is an incoming request.
  281.   This behaviour is defined in /etc/inetd.conf.
  282.  
  283.   inetd is started from system startup scripts.  It inherits just path
  284.   of init process.  It does not modify it and all the servers started
  285.   from inetd has init path.  An example of such a server is imapd, the
  286.   server of IMAP post office protocol.
  287.  
  288.   Other examples of inetd processes are telnetd, rlogind, talkd, ftp,
  289.   popd, many http servers and so on.
  290.  
  291.   Often usage of inetd is still complicated by using a separate tcpd
  292.   program to start the real server.  It is a program that makes
  293.   additional security checks before starting the real application.  It
  294.   does not affect the path (not verified).
  295.  
  296.   8.2.  rsh
  297.  
  298.   rsh daemon sets the path from _PATH_DEFPATH (/usr/include/paths.h)
  299.   that is the same path that login program uses for normal users. Root
  300.   will get the same path than the normal user.
  301.  
  302.   Actually, rshd executes the command it gets with the command line:
  303.  
  304.        shell -c command-line
  305.  
  306.   and shell is not a login shell. It is desirable that all the shells
  307.   mentioned in /etc/passwd support -c option to give on the command
  308.   line.
  309.  
  310.   8.3.  rlogin
  311.  
  312.   Rlogin is invokes login to make the real login procedure. If you login
  313.   with rlogin, you get the same path than in login. Most other ways to
  314.   log in to a Linux computer do not use login. Note the difference with
  315.   rsh.
  316.  
  317.   The login command actually used is
  318.  
  319.        login -p -h host-name user-name
  320.  
  321.   -p preserves the environment except the variables HOME, PATH, SHELL,
  322.   TERM, MAIL and LOGNAME. -h tells the remote host name for logging.
  323.  
  324.   8.4.  telnet
  325.  
  326.   Telnet is similar than rlogin. It uses the login program and the
  327.   command line to invoke it in a similar way.
  328.  
  329.   8.5.  ssh
  330.  
  331.   ssh has a path setting of it's own. It has a fixed path where it adds
  332.   the directory where ssh is. Often this means that /usr/bin is in the
  333.   path twice:
  334.  
  335.        /usr/local/bin:/usr/bin:/bin:.:/usr/bin
  336.  
  337.   The path does not contain /usr/X11/bin and shell invoked by ssh
  338.   command is not a login shell. Thus
  339.  
  340.        ssh remotehost xterm
  341.  
  342.   never works and anything in /etc/profile or /etc/csh.cshrc can change
  343.   this.  You must always use explicit path /usr/bin/X11/xterm.
  344.  
  345.   ssh searches environment variables of form VAR=VALUE from file
  346.   /etc/environment. Unfortunately this causes some problems with
  347.   XFree86.
  348.  
  349.   9.  XFree86
  350.  
  351.   9.1.  XDM
  352.  
  353.   XDM is the most common way to log in to a graphical terminal. It a bit
  354.   looks like login but it is internally totally different.
  355.  
  356.   In directory /etc/X11/xdm there are configuration files that are
  357.   executed on different login phases. Xstartup (and Xstartup_0 specially
  358.   for screen 0) contains commands to be run after the user has logged in
  359.   (commands are run as user root).
  360.  
  361.   The path that is set for users is in /etc/X11/xdm/xdm-config. There
  362.   are lines:
  363.  
  364.        DisplayManager*userPath: /usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
  365.        DisplayManager*systemPath: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
  366.  
  367.   That will be a default path for normal and root users respectively. It
  368.   is very important that /usr/bin/X11 is available for X users. If X
  369.   user logs in to another machine to start and X client application, he
  370.   should get /usr/bin/X11 to his path even he don't seem to come
  371.   directly from X terminal.
  372.  
  373.   After running Xstartup the XDM runs /etc/X11/Xsession that is run as
  374.   the final user. Local configuration is meant to be done in
  375.   /etc/environment that is sourced (included) from Xsession if available
  376.   (Xsession is run with /bin/sh and thus /etc/environment must be a sh
  377.   file). This clashes with ssh that supposes that /etc/environment is a
  378.   file that contains just lines of form VAR=VALUE.
  379.  
  380.   9.2.  xterm -ls
  381.  
  382.   By default the path for all the commands invoked from X window manager
  383.   menus is the path inherited from XDM. To use something different it
  384.   must be set explicitly. To start a terminal emulator with a path that
  385.   is "normal" some special option must be used. In xterm the option -ls
  386.   (login shell) must be used to get a login shell with path specified in
  387.   shell login initialization files.
  388.  
  389.   9.3.  Window manager menus and buttons
  390.  
  391.   Window manager inherits environment of XDM.  All the programs started
  392.   by the window manager inherit the environment of the window manager.
  393.  
  394.   User shell environment does not affect the programs that are started
  395.   from window manager buttons and menus.  For example, if program is
  396.   started from 'xterm -ls', it has the default environment of login
  397.   shell but if it is started from menu, it has just environment of the
  398.   window manager.
  399.  
  400.   10.  Delayed commands cron and at
  401.  
  402.   10.1.  cron
  403.  
  404.   Cron is a command that executes commands periodically as specified in
  405.   /etc/crontab and user-defined crontabs.  In Debian 1.3 there is a
  406.   standard mechanism to execute commands in /etc/cron.daily,
  407.   /etc/cron.weekly and /etc/cron.monthly.
  408.  
  409.   Cron is started from boot scripts but it seems to change it's PATH to
  410.   a pretty strange one:
  411.  
  412.        /usr/bin:/binn:/sbin:/bin:/usr/sbin:/usr/bin
  413.  
  414.   THIS IS LIKELY A BUG IN CRON.  This is the init path where there is
  415.   /usr/bin:/bin written over the beginning without terminating 0!  This
  416.   bug does not exist in all the systems.
  417.  
  418.   In crontab there can be PATH definition.  In Debian 1.3 there is the
  419.   following default line in the beginning of /etc/crontab:
  420.  
  421.        PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  422.  
  423.   Because of this, the PATH of crond program is never used in user
  424.   programs.  All the scripts in /etc/cron.* directories get this path by
  425.   default.  This path is used even if a program is executed as non-root.
  426.  
  427.   10.2.  at
  428.  
  429.   at is a command that can be used to run a one-time program at specific
  430.   time.
  431.  
  432.   atd is run using init path.  However, the user programs are always run
  433.   in the user environment using sh command.  Therefore the usual shell
  434.   overwrites apply.  Look the chapter on bash.
  435.  
  436.   11.  Some examples
  437.  
  438.   11.1.  magicfilter
  439.  
  440.   magicfilter is a common tool to manipulate files for printer.  It
  441.   analyzes the type of the file to be printed and invokes a filter
  442.   script to make appropriate pretty-printing.  These scripts are invoked
  443.   from lpd that is started from /etc/init.d/lpd that is started from
  444.   init. Thus, the path is that of init. That does not contain
  445.   /usr/bin/X11!
  446.  
  447.   You might want to insert printing of PDF files to magicfilter.  It is
  448.   possible to do this by using /usr/bin/X11/xpdf.  Now you must remember
  449.   to insert full directory path to the file name because magicfilter
  450.   would not find it otherwise.  Most programs used in magicfilter do not
  451.   need full path, because they are on /bin or /usr/bin.
  452.  
  453.   11.2.  Printing from X applications
  454.  
  455.   You may use PRINTER environment variable to show what is the printer
  456.   that you are using.  However, you may notice that in some cases in X
  457.   applications it is sometimes lost.
  458.  
  459.   You must remember that if the X session is started from XDM, the
  460.   window manager has never evaluated your shell login scripts.  All the
  461.   X applications that you have started from xterm have your PRINTER
  462.   variable.  However, if the same application is started from menu or
  463.   window manager button, it does not contain your PRINTER variable.
  464.  
  465.   In some cases this can be inherited to an even lower layer: for
  466.   example a Netscape helper application can have or have not your
  467.   PRINTER definition.
  468.  
  469.   12.  Security concerns
  470.  
  471.   The path is sometimes a big security problem.  It is a very common way
  472.   to hack into a system using some mistakes in path settings.  It is
  473.   easy to make Trojan horse attacks if hacker gets root or other users
  474.   to execute his versions of commands.
  475.  
  476.   A common mistake in the past (?) was to keep '.' in the root's path.
  477.   Malicious hacker makes program 'ls' in his home directory.  If root
  478.   makes
  479.  
  480.        # cd ~hacker
  481.        # ls
  482.  
  483.   he executes ls command of hacker's.
  484.  
  485.   Indirectly, this same applies to all the programs that are executed as
  486.   root.  Any of the important daemon processes should never execute
  487.   anything that some other user can write into.  In some systems,
  488.   /usr/local/bin is allowed to contain programs with less strict
  489.   security screening - it is just removed from the path of the root
  490.   user.  However, if it is known that some daemon executes 'foo' using
  491.   path '/usr/local/bin/:...', it may be possible to cheat daemon to
  492.   execute '/usr/local/bin/foo' instead of '/bin/foo'.  Likely anybody
  493.   who can write to '/usr/local/bin' is able to break into the system.
  494.  
  495.   It is very important to consider in what order the directories are in
  496.   the path.  If /usr/local/bin is before /bin, it is a security risk -
  497.   if it is after, it is not possible to overwrite command /bin/foo with
  498.   some localized modification in /usr/local/bin/foo.
  499.  
  500.   In Linux it should be remembered that the path evaluation is done in
  501.   the operating system call level.  Everywhere where an executable file
  502.   path is given you can give a short name that is searched at least from
  503.   /bin and /usr/bin - likely from many other places as well.
  504.  
  505.   13.  How to debug problems?
  506.  
  507.   The basic command to read environment is /usr/bin/env.
  508.  
  509.   It is possible to use /proc directory to find out path of any program.
  510.   First you must know the process number - use ps command to get that.
  511.   For example, if xterm is process number 1088, you can find it's
  512.   environment with command
  513.  
  514.        # more /proc/1088/environ
  515.  
  516.   This does not work with daemon processes like xdm. To access
  517.   environment of system processes or other user processes, root access
  518.   is required.
  519.  
  520.   To debug Netscape, you can create a script /tmp/test:
  521.  
  522.        $ cat > /tmp/test
  523.        #!/bin/sh
  524.        /usr/bin/env > /tmp/env
  525.        ^d
  526.        $ chmod +x /tmp/test
  527.  
  528.   Then set some helper application, for example RealAudio, audio/x-pn-
  529.   realaudio to call program "/tmp/test".  When you try to browse some
  530.   RealAudio link (something from http://www.realaudio.com/showcase),
  531.   Netscape calls the dummy program that stores environment to /tmp/env.
  532.  
  533.   14.  Some strategies to get the same path for all the users
  534.  
  535.   The most important settings is possible to set in the global shell
  536.   initialization files for login shells: /etc/csh.login for tcsh and
  537.   /etc/profile for bash.
  538.  
  539.   Exceptions that do not get the right path from these files are rsh
  540.   commands, ssh commands, menu items from X window manager that do not
  541.   explicitly start login shell, commands invoked from inittab, cron
  542.   jobs, daemons jobs like magic filters started from lprd, WWW CGI
  543.   scripts, and so on.
  544.  
  545.   If the path is set in /etc/csh.cshrc, the path is right even when rsh
  546.   or ssh execute command in remote machine with account using tcsh/csh.
  547.   However, it is not possible to set path if account uses bash/sh.
  548.   It is possible to combine path setting to one file, for example to a
  549.   file /etc/environment-common. There we write:
  550.  
  551.        ${EXPORT}PATH${EQ}/bin:/usr/bin:/sbin:/usr/sbin:/usr/bin/X11:/usr/local/bin:/usr/games:.
  552.  
  553.   This can be used from /etc/csh.login (for tcsh and csh)
  554.  
  555.        set EQ=" " set EXPORT="setenv " source /etc/environment-common
  556.  
  557.   And from /etc/profile (for bash, doesn't work for ordinary sh)
  558.  
  559.        EQ='=' EXPORT="export " . /etc/environment-common
  560.  
  561.   And from /etc/environment (for XDM)
  562.  
  563.        EQ="=" EXPORT="export " . /etc/environment-common
  564.  
  565.   This strategy works mostly but ssh will complain of the lines in
  566.   /etc/environment (and defined environment variables EQ and EXPORT).
  567.   And still, rsh commands executed with bash won't get this path.
  568.  
  569.   15.  Acknowledgements
  570.  
  571.   One reason to start writing this document was the big frustration of
  572.   Ari Mujunen.  Juha Takala gave some valuable comments.
  573.  
  574.