What are login and non-login shells?

69,939

Solution 1

Simply put:

  • If you open a shell or terminal (or switch to one), and it asks you to log in (Username? Password?) before it gives you a prompt, it's a login shell.
  • If it doesn't (like gnome-terminal), and lets you use it straight away, it's a non-login shell.

If you are a normal user of Ubuntu Desktop, the only login shell is...your desktop (you type a password to get in, right ;)? Well, technically it's a login shell that starts a GUI, but that's getting in to jargon. And yes, it will read the settings in .profile

The only time you (a normal user) will probably see a login shell that looks like a login shell is if you are having some problem with your desktop and you switch to a virtual terminal with the Ctrl+Alt+F1 shortcut.


The other general cases for having a login shell include:

  • accessing your computer remotely via ssh (or connecting locally with ssh localhost)
  • simulating an initial login shell with bash -l (or sh -l)
  • simulating an initial root login shell with sudo -i
    • or sudo -u username -i for another non-root user
  • authenticating as another non-root user with su - username (and their password)
  • using the sudo login command to switch user

Solution 2

I do not think that correct answer can be given without “technical jargon”. Since this question is the first one popping up in Google for the query “what is a login shell” I am providing a more correct answer below:

Login shell is simply a shell that was told to be a login shell. It does not mean shell that pops up after you login, though usually application that logs you in is telling shell it launches to be a login shell. There exists the following ways to tell shell it should be a login one:

  1. Running shell with -l or --login argument assuming it knows it (I do not know any shells which do not know -l, but --login is only supported by a few shells).
  2. Running shell with argv[0] set to -{some_string} (i.e. with HYPHEN-MINUS prepended to usual argv[0] or to some other string). This is what ssh and su do: su just runs executable with -su as argv[0] (hello to everybody thinking argv[0] has something to do with currently running executable name), ssh runs zsh with -zsh when user has set /bin/zsh as his shell.

Loginess of the shell has absolutely nothing to do with anybody asking you a password or performing some other authenication procedure. Some programs like ssh or login (or some terminal emulators like urxvt) run shells as a login ones using argv[0] that starts with a HYPHEN-MINUS. Some like su or sudo (or zsh: see - precommand modifier described in section PRECOMMAND MODIFIERS in man zshmisc) do not do this by default, but can be told so. Some have the only option of telling shell to be the login one using its argument (i.e. bash -l): ssh with a command argument (that explicitly tells ssh what to run on the remote end).

Generally it is better to first consult the documentation of the program used to invoke the shell to determine whether shell will be the login one and second perform some tests to determine whether app will launch a login shell (e.g. by adding echo to .profile).

Share:
69,939

Related videos on Youtube

DUKE
Author by

DUKE

I am an independent Java EE developer.

Updated on September 18, 2022

Comments

  • DUKE
    DUKE almost 2 years

    It is said that settings for non login shell to go into .bashrc file and login shell settings to go into .profile file.

    What is really meant by login and non-login shells?

    Please explain without using technical jargon as far as possible.

  • DUKE
    DUKE about 12 years
    If I start my Eclipse IDE from terminal, it opens as expected, but if I try to open it by clicking the Eclipse icon, it is unable to recognize Java location (unless otherwise PATH for Java is set in .profile file). That means clicking Eclipse icon needs a login shell, why?
  • ish
    ish about 12 years
    @DUKE, No, it means that environment variables need to be set differently when you are using a desktop/GUI versus a true command-line-only console system. Put your PATH, etc. in ~/.pam_environment (variables only, no bash commands in there!), logout, login, and watch everything magically appear in the desktop as well as in gnome-terminal!
  • xuhdev
    xuhdev almost 10 years
    Login via ssh does not invoke a login shell. It doesn't load /etc/profile, /etc/profile.d or ~/.profile.
  • xuhdev
    xuhdev almost 10 years
  • MichaelZ
    MichaelZ about 9 years
    @xuhdev Login via ssh does invoke a login shell and it does load /etc/profile, /etc/profile.d and ~/.bash_profile.
  • xuhdev
    xuhdev about 9 years
    @MichaelZ See here, may be you have loaded those files somewhere in bashrc?
  • VeryHardCoder
    VeryHardCoder over 8 years
    I'm interested in the second option: how may I actually change argv[0] before (or after) invoking bash? Can it be done from the command line?
  • VeryHardCoder
    VeryHardCoder over 8 years
    Ok, got it! Actually, it could be done even in a simpler way: (exec -l bash)...
  • rancho
    rancho about 8 years
    @izx Please clarify me the difference between your answer and as ZyX claims that his answer is the correct one
  • Alexey
    Alexey over 7 years
    What is the functional difference between the two? Is this simply a boolean flag?
  • ZyX
    ZyX over 7 years
    @Alexey Main functional difference is a set of configuration files used at startup. $0, and, maybe also something else (variable, setting, etc) is set so that loginess of the shell may be detected in the configuration file, but who bothers actually detecting this so it will make any difference? That’s all I know.
  • Honey
    Honey over 3 years
    I just opened my terminal on my mac. It doesn't prompt me with a login. Based on what you said If it doesn't (like gnome-terminal), and lets you use it straight away, it's a non-login shell I think it's a non-login shell. Then when I try this shopt -q login_shell && echo 'login' || echo 'not-login' it tells me that I'm on a login shell. Can you please elaborate which one is it?