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.
2 Answers
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 withssh localhost) - simulating an initial login shell with
bash -l(orsh -l) - simulating an initial
rootlogin shell withsudo -i- or
sudo -u username -ifor another non-rootuser
- or
- authenticating as another non-
rootuser withsu - username(and their password) - using the
sudo logincommand to switch user
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:
- Running shell with
-lor--loginargument assuming it knows it (I do not know any shells which do not know-l, but--loginis only supported by a few shells). - Running shell with
argv[0]set to-{some_string}(i.e. with HYPHEN-MINUS prepended to usualargv[0]or to some other string). This is what ssh and su do: su just runs executable with-suasargv[0](hello to everybody thinkingargv[0]has something to do with currently running executable name), ssh runs zsh with-zshwhen user has set/bin/zshas 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).