Linux what is login shell

What is Login Shell in Linux?

The “login shell” is a term used to define which shell (like Sh, Bash, or Zsh) will be used to load the environment variables and shell configuration files when you log into an interactive session.

Tips : Once you log into your system using a pre-defined login shell, you can switch to another shell (as a subshell) from your interactive shell.

So, you can consider it the first process that starts with your system and runs under your user ID when you interactively log in to your system (like system boot, “ su — ” command, or SSH to remote system will get you in interactive login shell).

To fully comprehend the login shell in Linux, you must be familiar with how users get authenticated during system boot, interactive and non-interactive logins, and shell configuration files.

If you want a thorough grasp of the Linux login shell, read this article all the way through.

How Users Get Authenticated on System Boot in Linux

As you know, Linux is a multi-user operating system where multiple users can login and use the system at the same time.

To protect your system from unauthorized access, the getty program (short for “get tty“) is the first process that runs when your (init or systemd) system boots up.

The role of the getty program is to prompt login screen to allow you enter your username and password and then verify the specified information from the “/etc/passwd” file.

All the users and system account information is stored in “/etc/passwd” file like username, UID, GID, comments, home directory, and your default login shell.

Linux what is login shell

As shown in the preceding image, the last column is used to define the user login shell, which is the first program that runs when a user logs in.

If the last column is empty, the “/bin/sh” shell will be used by default as the user login shell.

Interactive Login vs Non-Login Shell

Now you understood that the “/etc/passwd” file store the users and system account information including user login shell.

Next, you need to understand the difference between the interactive login and non-login shell; although we have written a detailed guide on this topic, we will still give you a small overview.

So, let’s look at the difference between an interactive and a non-interactive shell:

Interactive vs Non-interactive shell

  • Interactive shell: can be said to be when you directly interact with your shell from the command line.
  • Non-interactive shell: can be said to be when you indirectly interact with your shell, like shell script.

Next, let’s look at the difference between a login and a non-login shell:

Login vs Non-login shell

The “login shell” is the first process that runs under the user ID after the system boots or when the “ su — ” command is executed. Even if you use SSH to connect to a remote system, it will be seen as an interactive login shell.

Читайте также:  Linux домене смена пароля

Aside from that, login-shell will load some configuration files (or profiles), such as “/etc/profile” and “~/.bash_profile” for Bash and “/etc/zprofile” and “~/.zprofile” for Zsh, into your current shell environment.

For example, I’ve edited a few of the configuration files and commented out their filenames to demonstrate how these files were loaded when you launched login-shell (via SSH).

Login shell

This file will only be read and run in your current shell environment once, when your system boots up. After that, you’ll interact with a non-login shell.

The “non-login shell” is when you start more than one shell in the same environment after the system boots. For example, you could run the bash command in your terminal or open a new tab in your terminal.

Non-login shell

The non-login shell originates from the login shell, due to which it will get all the environment variables set by the login shell via the profile files. Although, the non-login can define their own environment variables through rc (resource configuration) files.

For the bash shell, it is usually “/etc/bash.bashrc” and “~/.bashrc” files, while for the zsh shell, it is “/etc/zshrc” and “~/.zshrc“.

How to Verify You Are in the Login Shell

The login shell and non-login shell can be easily identified, especially if you are using the Bash shell. So, if you are using the login shell, then the “ ” character is prepended to the shell executable (ex: “ -bash “), while for the non-login shell, it would normally be “ bash “.

Note : When you log in to graphical mode on a desktop Linux computer, a display manager controls it. This time, you won’t get a login shell; instead, you’ll get a session manager or a window manager.

Verifying the login and non-login shell

Final Tips!

Although, this article gives you a grasp on Login Shell, I also strongly recommend that you visit the referenced links in this article to learn more about them.

If you have any questions or problems while understanding any point, feel free to ask them in the comment section.

Источник

What is Login Shell in Linux?

You probably are aware of the shell in Linux. This is where you enter commands and execute programs. But what is this login shell and how is it different from the regular shell?

The login shell is the first process that is executed with your user ID when you log into an interactive session.

This may seem simple at the surface but if you dig deep, it could get confusing a bit. To understand, let’s see revisit the login process in Linux systems.

Linux is a multi-user system where multiple users can log in and use the system at the same time.

The first process in a Linux system, be it init or systemd, starts a getty program. This getty, short for ‘get tty’ (tty denotes physical or virtual terminals), is responsible for protecting the system from unauthorized access.

login process in Linux

When you try to connect to a Linux system either directly (if you have a server installed on a system locally) or via SSH (if you have the server on a remote location), getty prompts (through login program) you to enter your username and when the user name is entered, it prompts for the password.

Читайте также:  Welcome to My Website!

Your entries are verified against the information stored in /etc/passwd file. The entries in the /etc/passwd file has some information like username, userid, home directory and the first program that should be started after login succeeds.

etc passwd file in Linux

As you can see in the picture above, the first program to start when the user abhishek logs in. For standard users in Linux, it is the login shell.

There could be other values in the field as well. For example, sshd is a system user, not a real user and it can not log in to the system interactively like a regular user like abhishek.

sshd:x:110:65534::/run/sshd:/usr/sbin/nologin

If there is no entry in the column of the first program/log in shell, it is defaulted to /bin/sh, i.e. the standard shell.

I hope it was not too much information.

Login shell vs non-login shell

Alright, so you logged into a system and know that it’s a login shell. But what does it mean? Are there other shells as well? Where are they?

In Linux, when you run a shell script, it runs in its own shell (a non-interactive shell). You can start a new shell from your current shell (an interactive shell).

Changing shell in Linux

In technical terms, these are subshell but more on this topic later. Let’s focus on login shell for now.

Profile and RC files for the shell

The login shell reads environment variable and other configuration from /etc/profile and a profile file in the home directory. This allows you to have tab completion, colored output and sets other stuff such as umask etc.

You probably know that there are more than one shell available for Linux systems. Most Linux distributions use bash shell by default but you may install a different shell like zsh or Fish shell.

If your login shell is bash, it reads from /etc/profile and ~/.bash_profile file. If your login shell is zsh, it reads from /etc/zprofile and ~/.zprofile.

The files in /etc directory sets the respective shell configuration for all the users on the system. This is normally set up by the system administrator.

The files in the home directory of the user are user-specific, obviously. This allows users to create alias for frequently used commands or use a custom PATH variable for a program.

The non-login shells originates from the login shell and hence it gets all the environment set by the login shell via the profile files. In addition to that, non-login, interactive shell can define their own environment variables through rc (resource configuration) files in /etc or home directory.

For bash shell, it is usually, /etc/bash.bashrc and ~/.bash_rc files while for zsh shell, it is /etc/zshrc and ~/.zshrc.

Let me show the difference with a practical example.

Notice how the shell prompt changes to green-ish color? It’s because it reads /etc/bash.bashrc file and Ubuntu has put additional parameters in this file. Colored prompt is one of them.

  • The profile files are for interactive login shells. The rc files are for interactive non-login shells.
  • Files in /etc directory are executed first and then the files in the home directory.
  • The non-login, interactive shells benefit from both profile and rc files.
Читайте также:  Boot repair windows 10 linux

How to know if you are in a login shell?

That’s very simple actually, at least for the bash shell. For login shell, the name of the shell executable starts with — . For non-login shell, it’s just the name of the shell.

You know how to check the shell, right? You check the value of argument 0.

Checking if it is login shell

On desktop Linux, you don’t use login shell. Your login is managed by a display manager. This is why when you open a terminal in Linux desktop, you’ll see that even the first shell running in the terminal is not login shell.

I have tried to make things clear about login shell here. However, if you never wondered about these things, it may leave some unanswered questions. Feel free to ask it in the comment section and I’ll try to answer them.

If you are an expert user reading this and find something technically incorrect or some important point missing, please let me know in the comments.

Источник

What are the differences between a login shell and interactive shell?

An interactive shell is one started without non-option arguments, unless -s is specified, without specifying the -c option, and whose input and error output are both connected to terminals (as determined by isatty(3)), or one started with the -i option.

An interactive shell generally reads from and writes to a user’s terminal.

A login shell is a shell where you login. You can recognize a login shell from a ps -f listing, it will have a hyphen at the start of the program name, for example:

root 3561 3553 0 09:38 pts/0 00:00:00 -bash qa 7327 3432 0 10:46 pts/1 00:00:00 -bash 

An interactive shell is one which reads commands from its standard-input, usually a terminal.

  • if you login to bash using an xterm or terminal emulator like putty , then the session is both a login shell and an interactive one.
  • if you then type bash then you enter an interactive shell, but it is not a login shell.

If a shell script (a file containing shell commands) is run, then it is neither a login shell nor an interactive one.

Start-up files are highly tailorable in bash:

When a login bash shell is invoked, then /etc/profile is sourced (executed in the current environment). After that, three files are checked for existence. The checks for these files are done in this order, the first one that exists is run.

Once a match is found, the other files are ignored, even if they exist. The /etc/bashrc file might be used by both the ~/.bash_profile and the ~/.bashrc files. That would mean that the /etc/bashrc file is sourced on all interactive invocations of bash, whether it is a login or non-login shell.

So, the .bashrc file is also run every time you request a new interactive shell. This does not include a shell script. Normally variables, aliases or functions are placed in this file.

Bash shell scripts read a different file if suitably instructed. If the user defines (usually in their own .bash_profile ) a variable BASH_ENV which contains a filename, scripts will read this. If this variable is not set (and exported) then bash scripts will not read any startup files.

Источник

Оцените статью
Adblock
detector