418
votes

When I ssh into my ubuntu-box running Hardy 8.04, the environment variables in my .bashrc are not set.

If I do a source .bashrc, the variables are properly set, and all is well.

How come .bashrc isn't run at login?

4
How on earth is this "off topic"? - Jonah
I'm not strict like this, but my guess is that this belongs in serverfault.com, superuser.com, or askubuntu.com - Michael Butler
@MichaelButler Agreed. Wonder why they don't move it instead of just close it down... - Luc
@Luc - Questions can only be moved within 60 days of being created. This question wasn't closed as off topic until 3 and a half years after it was created. I believe the 60 days rule has something to do with when the question databases are backed up or something... it becomes more difficult to migrate after that backup occurs. - ArtOfWarfare
I thought it was a pretty useful question. Encountered this issue when I had to ssh into machine A, in order to ssh into machine B (only accessible via A's local network). Taught me a practical difference between .bashrc and .bash_profile ! - information_interchange

4 Answers

703
votes

.bashrc is not sourced when you log in using SSH. You need to source it in your .bash_profile like this:

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi
88
votes

I had similar situation like Hobhouse. I wanted to use the command

ssh myhost.com 'some_command'

where some_command exists in /var/some_location.

I tried to append /var/some_location to the PATH environment variable by editing $HOME/.bashrc but that wasn't working. Because per default, .bashrc (on Ubuntu 10.4 LTS) exits early due to this piece of code:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

Meaning if you want to change the environment for the ssh non-login shell, you should add code above that line.

34
votes

For an excellent resource on how bash invocation works, what dotfiles do what, and how you should use/configure them, read this:

31
votes

If ayman's solution doesn't work, try naming your file .profile instead of .bash_profile. That worked for me.