Have you ever wondered which of the multiple files does bash run when you login to a Linux machine?
Remember, there are at least those files that are potentially run:
- /etc/profile
- /etc/bash.bashrc
- /etc/bash_completion
- .profile
- .bash_profile
- .bashrc
- .bash_completion
Depending if you login interactively (getting a shell) or just running a command,some of these files are sourced.
Assumptions
We will take the case where:
- You login through SSH
- Or you change to a user using su
- You have an Ubuntu or Debian Linux machine
I suspect that Redhat/Fedora and others are not far away from these assumptions, but I have none to check…
There are in fact 6 scenarios
First Scenario : interactive shell with SSH
You login to a remote machine using SSH, getting a shell. This is the most current use of SSH.
With:
ssh user@machine
You then get a shell.
The files that are sourced are:
- /etc/profile
- /etc/bash.bashrc
- .profile
- .bashrc
- /etc/bash_completion
- .bash_completion
That’s right only if there is no .bash_profile file in your HOME directory. Else the files sourced are:
- /etc/profile
- /etc/bash.bashrc
- .bash_profile
In fact, bash sources .bash_profile if it exists, else .bashrc
Note that
- /etc/bash.bashrc is sourced by /etc/profile if the shell is bash
- .bashrc is sourced by .profile
- /etc/bash_completion is in fact sourced by .bashrc (not bash itself directly)
So we could summarize this way.
When asking for an interactive shell, bash sources:
- /etc/profile
- .bash_profile
- Or .profile if .bash_profile is not found
Second scenario : running a command through SSH
You login to a remote machine using SSH, asking to run a command (no interactive shell)
ssh user@machine "command to run" (Ex: ssh user@machine "ls /tmp")
The files that are sourced are:
- /etc/bash.bashrc
- .bashrc
Note that .bashrc is run in place of .bash_profile in this case.
Third scenario : getting an interactive shell using su –
You are logged with root and want to get a shell as another user:
    su - user
Note that the minus sign means that you want to source the config files for the target user.
The files that are sourced, are
- /etc/profile
- .bash_profile
- Or .profile if .bash_profile is not found
Fourth scenario : running a command using su –
You are logged with root and want to get a shell as another user:
    su - user -c "touch /tmp/file"
The files that are sourced, are
- /etc/profile
- .bash_profile
- Or .profile if .bash_profile is not found
Note that in this case, .bashrc is NOT run from /etc/profile since the shell is not interactve! (PS1 is not defined)
Fifth scenario : getting an interactive shell using su (without the minus sign)
You are logged with root and want to run a shell as another user:
    su userSurprise! Even since you thought that the minus sign would source NO config files, the files that are sourced, are
- /etc/bash.bashrc
- .bashrc
Note that the sourced files are the same as running a command through SSH!
Sixth scenario : running a command using su (without the minus sign)
You are logged with root and want to run a shell as another user:
    su user -c "touch /tmp/file"And this time, no surprise, the files that are sourced, are … NONE!
That’s exactly what the minus sign means. Become <user>, but don’t source its config files.
And the bonus scenario
What happens when you run a command from a crontab?
First, the default shell when running from a CRON, is /bin/sh. You can change this to bash putting:
SHELL=/bin/bashas the first line of your crontab file (crontab -e is your friend for this one).
Then sourced config files when the command is run are … NONE also.
Conclusion
Every time I need to run remote command on a machine or run a script as a user using su, I ask myself the same questions about those config files!
And we can see above that it’s not that clear which files are run when…
So now that’s done, for the posterity, on my blog so it should last for some time.
Comments of course welcome!
Bonjour, aurez vous le courage de me dire qui est derrière cet adresse?