Problems related to login shell and non-Login Shell

Source: Internet
Author: User
Problem: Switching a user through the su command does not enter the user's shell environment. Why? To solve this problem, we must be clear about the difference between using Login Shell and non-login shell.
  • Login Shell:Complete logon process is required for Bash. That is to say, by entering the account and password to log on to the system, the obtained shell is called login shell.
  • Non-login shell:The method for obtaining the sbash interface does not require repeated logon. For example, after logging on to Linux with X Window, start the terminal with the graphical interface of X. At this time, the terminal does not need to enter the account and password, and the bash environment is non-login shell. Execute the bash command again in the original bash environment, and enter the new bash environment without entering the account password (the previous bash sub-process ), the new Bash is also a non-login shell.

So what is the difference between the two?

Login Shell: In this mode, the shell will re-read/etc/profile and ~ /. Bash_profile to apply new environment variables.
Non-login shell: At this time, shell will not read/etc/profile and ~ /. Bash_profile, but read ~ /. Bashrc to apply new environment variables. The title is the difference and influence, and the impact naturally comes from the difference. Back to the question at the beginning of the article, the su switching user did not get the user's environment (the command was "su xxoo" at the time, here xxoo is the user to switch, run this command as root). It seems that the problem lies in the login Login method. Based on the symptom, we can determine that Su xxoo has executed the non-login shell.

To verify the above judgment, we have checked the simple help of the su command:

[[Email protected] ~] # Su -- Help
Usage: su [Option]... [-] [user [Arg]...]
Change the valid tive user ID and group ID to that of user.
 
-,-L, -- login make the shell a login shell


Obviously, my judgment is correct. By default, the non-login shell is executed, to execute the login shell, you need to add the parameter "-" or "-l" or "-- login" After Su ". Therefore, to obtain the user's environment variables after Su, you need to execute commands such as "Su-xxoo.

There are also three tips:

 

PS1:Bash reads the personal configuration files of each account after reading/etc/profile. The Personal Configuration Files Read mainly include three :~ /. Bash_profile ,~ /. Bash_login ,~ /. Profile. In fact, bash only reads one of the above three files, and the reading order is the above Order. That is to say, bash searches for the above three individual configuration files in sequence, and finds one, and the subsequent files are no longer read.

PS2:When you log on with a non-Login Shell, if you want to obtain the user's environment variables, Run "source ~ /. Bash_profile "(. bash_profile may also be. bash_login or. profile), where source can be replaced by the decimal point, that is," Source ~ /. Bash_profile "is equivalent ".~ /. Bash_profile ". You can search for the explanations of the source command.

PS3:In fact, some Linux releases of login shell will also read the. bashrc file, for example, execute "Vim ~ /. Bash_profile "will find" If [-f ~ /. Bashrc]; then .~ /. Bashrc fi ". Here the source feature is replaced by the decimal point mentioned in PS2, that is, the. bashrc file will be read in./bash_profile.

From: http://blog.sina.com.cn/s/blog_59c0a56201016im6.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.