When I ssh into a server (4.3.3-2-ARCH), my .bashrc is not loaded (however, I can load it manually by sourcing it).

I don't have any other files in my home folder, like .profile. The shell is also correct

$ echo $SHELL
/bin/bash

When I login using ssh -t myserver /bin/bash , my .bashrc IS loaded correctly.

In SSH verbose mode, I do see that /etc/bash.bashrc and ~/.bashrc are loaded using the last command. When using the default ssh command, none of them is being loaded...

What should I configure to have .bashrc (and /etc/bash.bashrc) loaded when using the default ssh command?

up vote 10 down vote accepted

the ~/.bashrc file is loaded for interactive non-login shells, that's why running /bin/bash from ssh works as you expect.

For login shells (amongst others) ~/.bash_profile is read. Typically your ~/.bash_profile contains something like

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

Which ensures that ~/.bashrc is loaded if your shell is a login shell.

If your ~/.bash_profile contains the above and ~/.bashrc is not being loaded you can debug the scripts by using set -x as usual.

  • I do not have a ~/.bash_profile ... So, I do need to create it with the above lines in it? – intrixius Jan 7 '16 at 9:22
  • 2
  • I created the ~/.bash_profile file and it works... I have installed multiple servers in the past, and I cannot remember that I had to create that to get bashrc working... – intrixius Jan 7 '16 at 9:30

Normally when a user is created it will copy a bunch of files to your new homedirectory (if you chose to create one).

You can check the /etc/skel directory for those files. There should be the .bash_profile that you expected.

  • /etc/skel does indeed include .bash_profile . So i don't know why I did not have it. I just created my user with useradd username ... strange – intrixius Jan 7 '16 at 10:43
  • were the contents of the file as exspected tho ? – Koen van der Rijt Jan 8 '16 at 14:10

Bash reads group of files depending on the invocation of the shell:

  • Login shell: Basically a shell that it is attached to a tty and comes from an authentication process. This kind of shell reads /etc/profile, ~/.bash_profile, ~/.bash_login and ~/.profile
  • Non login - interactive shell: Interactive shells that opens without an authentication process. This kind of shell reads /etc/bash.bashrc and ~/.bashrc

You can find more information on: man bash , section INVOCATION

As none of the previous, it seemd that the user didn't have a shell assigned, so it just show the character $ as prompt.
I had to assign a shell to the user by running the command:

sudo usermod --shell /bin/bash username

After that, the user automatically logged in with a bash shell.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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