0

I know /etc/profile and ~/.profile are among the start-up scripts used when ssh is used to login to a server – i. e. using ssh with server argument. However, the scenario where I am just using ssh with a server+command argument does not seem to pick up the same start-up scripts.

Assume I have bash as default shell. If .bashrc exists, I know it is picked up in this scenario. But if .bashrc does not exist and no other personal profiles exist, what else runs?

Is there another global script that provides the environment variables?

1
  • For once, there's /etc/environment. Apr 7, 2015 at 0:37

2 Answers 2

1

This page has more than you probably want to know about it, but it's a great concise resource:

Basically, when you ssh to a computer, you are not running a 'login' shell, so different files are sourced. In short, if your default shell is /bin/bash (default on Ubuntu), you will be sourcing a ~/.bashrc in your home directory. Check the FILES section of the bash man page (man bash), near the end, for other files that come into play.

Also, make sure you are on the lookout in your .bashrc for lines that exit if not using an 'interactive' shell. A good way to figure this stuff out is to put in prints in your .bashrc and keep experimenting until you figure it out.

Like this:

# ~/.bashrc:

echo "HERE I WAS!"

# Exit here unless an interactive session.
[ -z "$PS1" ] && return

echo "HERE I AM!"
2
  • So if assuming there are no personal profiles involved, is there anything else that actually runs in this scenario? Do you know? Thanks.
    – user55570
    Apr 7, 2015 at 1:57
  • Looks like @muru has a good answer here for other options.
    – dpb
    Apr 7, 2015 at 2:14
1

Without a command, SSH runs a login shell. For bash, that involves sourcing .profile (which, on Ubuntu, sources .bashrc) (and /etc/profile, which sources /etc/bash.bashrc). There are other files which could be sourced instead, such as .bash_profile, but a default Ubuntu setup has only .profile.

$ grep bashrc /etc/profile .profile
/etc/profile:    # The file bash.bashrc already sets the default PS1.
/etc/profile:    if [ -f /etc/bash.bashrc ]; then
/etc/profile:      . /etc/bash.bashrc
.profile:    # include .bashrc if it exists
.profile:    if [ -f "$HOME/.bashrc" ]; then
.profile:   . "$HOME/.bashrc

When run with a command, SSH doesn't run a login shell, so, according to man bash (section INVOCATION):

When an interactive shell that is not a login shell  is  started,  bash
reads  and  executes  commands  from /etc/bash.bashrc and ~/.bashrc, if
these files exist.  This may be inhibited by using the  --norc  option.
The  --rcfile  file option will force bash to read and execute commands
from file instead of /etc/bash.bashrc and ~/.bashrc.

However, with a command, bash is not being run interactively. Then why is .bashrc sourced? Again, from man bash:

Bash attempts to determine when it is being run with its standard input
connected to a network connection, as when executed by the remote shell
daemon, usually rshd,  or  the  secure  shell  daemon  sshd.   If  bash
determines  it  is  being  run  in  this fashion, it reads and executes
commands from ~/.bashrc and ~/.bashrc, if these  files  exist  and  are
readable.  It will not do this if invoked as sh.  The --norc option may
be used to inhibit this behavior, and the --rcfile option may  be  used
to  force  another file to be read, but neither rshd nor sshd generally
invoke the shell with those options or allow them to be specified.

Other files could be read by SSH (from man ssh, section FILES):

~/.ssh/rc
     Commands in this file are executed by ssh when the user logs in,
     just before the user's shell (or command) is started.  See the
     sshd(8) manual page for more information.
/etc/ssh/sshrc
     Commands in this file are executed by ssh when the user logs in,
     just before the user's shell (or command) is started.  See the
     sshd(8) manual page for more information.

For environment variables, (from man ssh, section ENVIRONMENT):

Additionally, ssh reads ~/.ssh/environment, and adds lines of the format
“VARNAME=value” to the environment if the file exists and users are
allowed to change their environment.  For more information, see the
PermitUserEnvironment option in sshd_config(5).

The pam_env module is enabled for SSH:

$ grep pam_env /etc/pam.d/sshd 
# /etc/security/pam_env.conf.
session    required     pam_env.so # [1]
session    required     pam_env.so user_readenv=1 envfile=/etc/default/locale

Therefore, variables in /etc/environment and ~/.pam_environment are also set (and /etc/default/locale, since envfile is set). However, these files are not sourced the way .profile is, so you cannot use shell commands here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .