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?
.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
.bashrc
out-of-the-box - but it does it from .profile
, not .bash_profile
.
Commented
Oct 18, 2015 at 14:06
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.
bash # If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac
man ssh
: " If a command is specified, it is executed on the remote host instead of a login shell." For me on Fedora Core 29 it doesn't call neither .bashrc nor .bash_profile when running commands (and calls them without commands).
Commented
Feb 25, 2020 at 10:57
If ayman's solution doesn't work, try naming your file .profile
instead of .bash_profile
. That worked for me.
.profile
is to be executed by any login shell, regardless of whether said shell intends to spawn a GUI. Your comment totally contradicts the question and this answer, which clearly indicate that .profile
is invoked on SSHing in - a distinctly non-GUI method.
Commented
Oct 18, 2015 at 14:05
For an excellent resource on how bash invocation works, what dotfiles do what, and how you should use/configure them, read this:
Now let's take the second-simplest example: an ssh login. This is extremely similar to the text console login, except that instead of using getty and login to handle the initial greeting and password authentication, sshd(8) handles it. sshd in Debian is also linked with PAM, and it will read the /etc/pam.d/ssh file (instead of /etc/pam.d/login). Otherwise, the handling is the same. Once sshd has run through the PAM steps (if applicable to your system), it "execs" bash as a login shell, which causes it to read /etc/profile and then one of .bash_profile or .bash_login or .profile.
I know its an old issue, but I was facing the same issue on Ubuntu 22.04.
have two identical servers, one of them source ~/.bashrc
properly, I see colors once I login by ssh, the other was not
both servers had the exact same ~/.bashrc
file
in my case the issue was when I installed golang using one liner on one of these two, it added new file ~/.bash_profile
that file only had three export path settings, so for some reason when I ssh this file was being source and not ~/.bashrc
removing .bash_profile
and only keeping .bashrc
solved the issue for me, or you can follow @swaz suggestion and keep both files
Similar as @Loïc Wolff , Added below in my $HOME/.bash_profile
Ubuntu 16
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
echo "Executed .bash_profile , calling .bashrc"
. "$HOME/.bashrc"
fi
fi
Just want to add that except what described above also one additional step is required - to comment out the following lines in .bashrc
otherwise it still doesnt work
case $- in
*i*) ;;
*) return;;
esac
From the other hand , it happens that there is no need "to activate" reading bash (in non-interactive mode) file .bashrc
Instead as written here just use the following ssh+bash invoke syntax
ssh -t thinkpad bash -i -c CMD
.bashrc
and.bash_profile
!