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):

  1. The SSH daemon, via the pam_motd module of the PAM library, displays the contents of /etc/motd. Via the pam_env module, it sets the environment variables from /etc/environment and ~/.pam_environment.
  2. A login shell is started, and the following files are read in order:
    1. /etc/profile
    2. /etc/bash.bashrc (the default Ubuntu /etc/profile sources /etc/bash.bashrc).
    3. ~/.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.