Sequence of scripts sourced upon login
Solution 1
This is kind of complex. First of all, the details depend on what kind of shell you are running. To plagiarize myself:
When you open a terminal emulator (
gnome-terminal
for example), you are executing what is known as an interactive, non-login shell.When you log into your machine from the command line, or run a command such as
su - username
, you are running an interactive login shell.When you log in graphically, you are running something completely different. The details will depend on your system and graphical environment but in general, it is the graphical shell that deals with your login. While many graphical shells (including the Ubuntu default) will read
/etc/profile
and~/.profile
not all of them do.Finally, when you run a shell script, it is run in a non-interactive, non-login shell.
The files that bash will read when launched depend on the type of shell it is running as. The following is an excerpt of the INVOCATION section of man bash
(emphasis mine):
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
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.
Those are the initialization files. You also have /etc/environment
where you can set global environmental variables but that's read rather than sourced (commands inside it are not executed but variable definitions are set).
Now, the greeting you see is something else again. That is set in /etc/motd
and is displayed through pam_motd
. As explained in man motd
:
The contents of /etc/motd are displayed by pam_motd(8) after a successful login but just before it executes the login shell.
The abbreviation "motd" stands for "message of the day", and this file has been traditionally used for exactly that (it requires much less disk space than mail to all users).
On Debian GNU/Linux, the content of /run/motd.dynamic is also displayed. This file is generated by /etc/init.d/motd at boot.
To remove the message just empty the /etc/motd
file and make sure that nothing is being generated by /etc/init.d/motd
if present.
Anyway, based on the output you show, you seem to be logging in via ssh
which means you're running an interactive login shell, see above for what that means.
So, in summary, the things you care about that are sourced when you log in are (and in this order):
- The SSH daemon, via the
pam_motd
module of the PAM library, displays the contents of/etc/motd
. Via thepam_env
module, it sets the environment variables from/etc/environment
and~/.pam_environment
. - A login shell is started, and the following files are read in order:
/etc/profile
-
/etc/bash.bashrc
(the default Ubuntu/etc/profile
sources/etc/bash.bashrc
). -
~/.bash_profile
. The other files that could have been read here (~/.profile
and~/.bash_login
) are ignored because~/.bash_profile
exists.
Solution 2
The info you are seeing when you login via ssh is created before /etc/profile is ever even looked at. Take a look at the sequence of files in /etc/update-motd.d. That will show you where the messages you are seeing are generated by various scripts.
Related videos on Youtube
![amphibient](https://i.stack.imgur.com/ORKi0.jpg?s=256&g=1)
Comments
-
amphibient almost 2 years
I would like to concentrate all my login config in my
~/.bash_profile
. There was a~/.bashrc
there by default but I replaced it with a~/.bash_profile
.However, when I log in, something before my
~/.bash_profile
gets sourced and displays the following:Linux ubnt10-dev1 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux Ubuntu 10.04.4 LTS Welcome to the Ubuntu Server! * Documentation: http://www.ubuntu.com/server/doc System information as of Fri May 9 12:17:39 EDT 2014 System load: 0.01 Processes: 74 Usage of /: 5.5% of 18.58GB Users logged in: 0 Memory usage: 4% IP address for eth0: 123.x.x.x Swap usage: 0% Graph this data and manage this system at https://landscape.canonical.com/ New release 'precise' available. Run 'do-release-upgrade' to upgrade to it. Last login: Fri May 9 12:11:52 2014 from 123.x.x.x
I would like to remove that so that only my own splash greeting defined in my
~/.bash_profile
gets displays but I am not sure about the logic/sequence of scripts sourced/executed when a user logs in. Can you enlighten me?As a bonus, I would also like to know the sequence of automated sourcings/execs when the system starts, not just when a user starts a session by logging in, albeit this is a concern of lesser importance to me right now.
-
Rmano about 10 yearsA nice trick to self-discover this things is putting sentinel variables around. I mean, I put
export SET_IN_ETC_PROFILE=yes
in/etc/profile
,export SET_IN_HOME_ZSHRC=yes
in~/.zhshrc
(I am a zsh user), etc... Then reboot and a niceenv|grep SET_IN
will tell you all the story. To see which variables are seen by the desktop applications (the ones you launch from the dash or the WM, without using a terminal), see askubuntu.com/a/356973/16395 -
amphibient about 10 yearsso when you ssh, that would be an interactive shell, correct?
-
Penghe Geng over 9 years@Rmano I upvoted your comments. But moments later found the order in
env
has no relationship with the order of execution. -
terdon over 9 years@xiaobai of course not, the point is that
SET_IN
will show you which files have been read, not in which order they have been read. The order is standard, it is what is shown in the man page excerpts I have in my answer. -
wjandrea over 6 yearsThis is already covered in the top answer, but I appreciate your brevity. +1 :)
-
David Tonhofer over 5 yearsPlus the files in
/etc/profile.d
seem to be sourced in "sorted as usual" order (256.., a..., b..., x....). Good to have if you want a specific fragment to be executed last. -
vastlysuperiorman over 4 years@Rmano an alternative to this that also preserves order is to create a
SET_IN
variable in the same manner you add toPATH
. In each file,export SET_IN="$SET_IN:$BASH_SOURCE"
. This will result in a variableSET_IN
that contains all files in the order they were sourced.