From bash manual

Invoked by remote shell daemon

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, if that file exists and is 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.

Is a shell provided by ssh username@server login or nonlogin?

If it is a login shell, why doesn't it executes commands from ~/.profile, but from ~/.bashrc according to the bash manual?

Thanks.

My OS is Ubuntu 16.04, but the bash manual isn't OS specific.

closed as unclear what you're asking by Stephen Kitt, Jeff Schaller, G-Man, Kiwy, Christopher Apr 5 at 14:04

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Specs of the system you are logging in to? – blissini Apr 4 at 21:33
  • specs of the remote OS, not yours. – blissini Apr 4 at 21:39
  • can be Ubuntu, or any other Linux. The bash manual is not server-specific either. – Tim Apr 4 at 21:43
  • Put alias hewoo='echo "bashrc got invoked"' in ~/.bashrc on the remote system, then ssh to it and do hewoo. That should clarify it for you. :3 – Mioriin Apr 4 at 21:47
  • 1
    What is your login shell on the remote system? ssh myuser@host 'getent passwd myuser' – glenn jackman Apr 4 at 22:12
up vote 3 down vote accepted

SSH starts a login shell, as alluded to in its manpage:

If a command is specified, it is executed on the remote host instead of a login shell.

You can verify this within Bash with

shopt login_shell

which will show whether it is running as a login shell.

Bash’s behaviour when started remotely, whether as a login shell or otherwise, is as documented in the section you quote. The behaviour you’re comparing it with is that of interactive shells, and a remote shell adds the .bashrc processing on top of the interactive login shell behaviour.

Note that Ubuntu systems typically have a .bash_profile script, which takes priority over .profile, and they typically have .bash_profile source .bashrc in any case...

  • "Bash’s behaviour when started remotely, whether as a login shell or otherwise, " Does it matter that the remoate bash shell is interactive or non-interactive? For example ssh me@example.com 'some-command.sh' provides a noninteractive login bash shell. – Tim Apr 4 at 22:03
  • It matters a great deal: the ~/.bashrc only gets sourced if you have an interactive shell – glenn jackman Apr 4 at 22:06
  • If you specify a command to run, that’s a different ballgame; I thought you were asking about shells you get with plain ssh me@example.com. In the 'some-command.sh' case, the shell is non-interactive, as you’d expect; in the plain SSH case, it’s interactive (echo $- shows i among other flags). – Stephen Kitt Apr 4 at 22:09
  • In case of ssh me@example.com 'some-command.sh', which startup file(s) is executed, ~/.bash_profile or ~/.bashrc or something else? – Tim Apr 4 at 22:13
  • If you run ssh me@example.com some-command.sh, the remote shell won’t execute any startup script, unless you set BASH_ENV; see the section describing non-interactive shells. – Stephen Kitt Apr 4 at 22:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.