At the end of .bashrc
I added:
touch /tmp/bash_noninteractive_test
Run:
/usr/bin/ssh -v -C [email protected] 'ls'
On the host (logged in interactively before):
% ls -l /tmp/bash_noninteractive_test
ls: cannot access /tmp/bash_noninteractive_test: No such file or directory
I thought ~./bashrc
is ALWAYS sourced in non-interactive shells, like over SSH? How do I fix that?
Systems affected:
% lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
% lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.11 (jessie)
Release: 8.11
Codename: jessie
Adding my bit to the old thread for information because the accepted answer is not the real reason for .bashrc not being read for an ssh command. I got the hint after reading this answer. The second last paragraph of the INVOCATION section in bash man page states that:
Hence although the ssh command is run as a non-interactive user, .bashrc is still loaded.
But your OS may be adding checks at the top of .bashrc similar to the following (from Ubuntu 18.04) which exits .bashrc after the first few lines for a non-interactive invocation.
To prevent the checks, open .bashrc and comment those lines and your ssh commands will read the whole .bashrc which is the result, I suppose, you are expecting.
From the INVOCATION chapter of
man bash
:So it appears that
~/.bashrc
is never sourced on non-interactive shells, unlessBASH_ENV
points to it.Depending on what you wish to actually achieve, you might consider enabling
PermitUserRC
and/orPermitUserEnvironment
in/etc/ssh/sshd_config
on your remote host, and then writing the desired environment variables in~/.ssh/environment
and/or the desired commands in~/.ssh/rc
.NOTE: if you plan to use SSH X11 forwarding together with
~/.ssh/rc
, you must include a snippet of code to explicitly feed the X11 session to thexauth -q -
command; see the example in the SSHRC chapter ofsshd(8)
man page.