Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

We just got our new server(s) up and we're running CentOS on them all. After successfully installing Ruby Enterprise Edition, I would now like to add the REE /bin (located at /usr/lib/ruby-enterprise/bin) directory to make it the default Ruby interpreter on the server.

I have tried the following, which only adds it to the current shell session:

export PATH=/usr/lib/ruby-enterprise/bin:$PATH

What would be the correct approach to permanently adding this directory to $PATH for all users. I am currently logged in as root.

Thanks in advance!

share|improve this question
add comment

4 Answers 4

up vote 42 down vote accepted

It's not a good idea to edit /etc/profile for things like this, because you'll lose all your changes whenever CentOS publishes an update for this file. This is exactly what /etc/profile.d is for:

# echo 'pathmunge /usr/lib/ruby-enterprise/bin' > /etc/profile.d/ree.sh
# chmod +x /etc/profile.d/ree.sh

Log back in and enjoy your (safely) updated PATH:

# echo $PATH
/usr/lib/ruby-enterprise/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# which ruby
/usr/lib/ruby-enterprise/bin/ruby

Instead of logging back in, you could reload the profile:

# . /etc/profile

This will update the $PATH variable.

share|improve this answer
    
~/.profile is another valid option too –  Zypher Aug 22 '11 at 16:34
    
Yes, for a single user. But the question was about altering PATH for all users. –  Mike Conigliaro Aug 22 '11 at 17:46
2  
whoops, missed that tiny little word ... please move along, nothing to see here –  Zypher Aug 22 '11 at 17:50
1  
I know this is kind of old, but I just wanted to comment on it and say thanks. Helped me out. :) +1 –  Rob Feb 15 '12 at 5:22
    
@Mike What is this pathmunge command? –  Nickolai Leschov Apr 3 at 0:25
show 1 more comment

After following fmonk's advice I checked out /etc/bashrc, where I noticed it said that "Environment stuff goes in /etc/profile." I proceeded to look in /etc/profile, I saw this:

pathmunge () {
    if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
       if [ "$2" = "after" ] ; then
          PATH=$PATH:$1
       else
          PATH=$1:$PATH
       fi
    fi
}

[...]

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
fi

To solve my problem, I simply added pathmunge /usr/lib/ruby-enterprise/bin underneath the if statement. This solved my issue.

share|improve this answer
    
Could someone explain what the "$EUID" = "0" means in this context? –  Eli May 11 '11 at 14:41
    
EUID 0 means that user is root. –  bbaja42 Jun 5 '11 at 18:55
2  
You should have used /etc/profile.d. See my answer below. –  Mike Conigliaro Aug 22 '11 at 16:30
    
Please consider accepting @MikeConigliaro answer as the correct. His way is the correct one. It's developed to work that way. Take a look at the files in /etc/profile.d/ folder and you'll notice it. Also, as he said, a system update could undo your solution. –  CaioToOn Oct 3 '12 at 13:02
add comment

"An interactive login shell is started after a successful login, using /bin/login, by reading the /etc/passwd file. This shell invocation normally reads /etc/profile and its private equivalent ~/.bash_profile upon startup.

An interactive non-login shell is normally started at the command-line using a shell program (e.g., [prompt]$/bin/bash) or by the /bin/su command. An interactive non-login shell is also started with a terminal program such as xterm or konsole from within a graphical environment. This type of shell invocation normally copies the parent environment and then reads the user's ~/.bashrc file for additional startup configuration instructions." http://www.linuxfromscratch.org/blfs/view/6.3/postlfs/profile.html

Therefore I would not put environment variables in bashrc, because it is not only against common convention, but you will also miss your bashrc varialbles when invoking a terminal from a graphical Desktop environment.

On Redhat in the /etc/profile I found this comment:

"System wide aliases and functions should go in /etc/bashrc. Personal environment variables and startup programs should go into ~/.bash_profile. Personal aliases and functions should go into ~/.bashrc."

So if you want to set environment variables on a user basis, do it in the user's .bash_profile file.

Heading over to the .bash_profile I read:

"Personal environment variables and startup programs.

Personal aliases and functions should go in ~/.bashrc. System wide environment variables and startup programs are in /etc/profile. System wide aliases and functions are in /etc/bashrc."

Conclusion
If you want only root to see programs residing, for instance in /sbin I would add that path to root's .bash_profile file. But if you want every user to see what root specific programs are installed on your box I would put /sbin into /etc/.profile. Now every user can use tab completion to look for root specific programs and elevate rights if necessary.

Special Case: SSH
When ssh is started with a commandline, an interactive login shell is started. But in this case /etc/profile is not read. When I defined environment variables in the .bash_profile file of each user it worked with ssh.

share|improve this answer
add comment

You can set environment variables in a .rc file; for bash shells (I believe the most common, and default in CentOS) each user has a file called .bashrc in his home directory.

Add the command PATH=/usr/lib/ruby-enterprise/bin:$PATH to this file to set it for any one particular user.

To set it for all users (as you mention), change it in /etc/bashrc (the default .bashrc in each user's home directory should source this file, but you should doublecheck that).

share|improve this answer
add comment

Your Answer

 
discard

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.