The difference between the linux login shell and a non-login shell

First think about a problem before introduction: We sometimes through suthe switch user command, but found not to enter the user's shell environment. Why is that?

  • shell the Login : need to complete the login process when acquired bash. That is by entering the account number and password to log into the system , shell acquired at the time called the login shell

  • the Login shell-non : way to get sbash interface need not be repeated logon actions of. Such as the X Window Log in to linux, and then to start the graphical interface X terminal, then the terminal does not need to enter the account number and password, the bash environment that is non-login shell. In the original execution environment bash bash command again, also did not enter the account password to enter a new bash environment (the child of a bash before), a new bash also non-login shell.

So, in the end where the difference between the two?

login shell: When this log, shell will re-read /etc/profileand ~/.bash_profileto apply the new environment variable.

non-login shell: shell does not read at this time /etc/profileand ~/.bash_profile, but read ~/.bashrcto apply the new environment variable. The title is the difference between saying and influence things from the effects of natural differences. Back to the beginning of the article the problem, suswitch the user does not get the user's environment (command was using a "su xxoo", here xxoo for users to switch, this command is executed as root), it appears that maybe the problem lies login way, according to the phenomenon we can determine su xxoo performed non-login shell.

In order to verify the above judgment, we see a simple help su command:

[root@isayme ~]# su --help
Usage: su [OPTION]... [-] [USER [ARG]...]
Change the effective user id and group id to that of USER.
 
-, -l, --login               make the shell a login shell 

Obviously, my judgment is correct, default is a non-login shell executed , because to perform login shell need to add the parameter after su"-"或"-l"或"--login" . So to get the user's environment variables after su, you need to perform similar - command "su xxoo" and the like.

Profile load case

Here Insert Picture Description
Here Insert Picture Description

.bash_profile and .bashrc difference:

/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置.
/etc/bashrc:  为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.
~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.
~/.bashrc: 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取.
~/.bash_logout: 当每次退出系统(退出bash shell)时,执行该文件.
 
另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系.
Published 115 original articles · won praise 4 · Views 4571

Guess you like

Origin blog.csdn.net/weixin_43999327/article/details/104556059