login shell vs non-login shell

今天遇到的问题,通过su命令切换用户并没有进入该用户的shell环境。这是为什么?

先介绍login shell和non-login shell概念:

  • login shell:去的bash时需要完整的登录流程。就是说通过输入账号和密码登录系统,此时取得的shell称为login shell
  • non-login shell:取得sbash接口的方法不需要重复登录的举动。如以X Window登录登录linux后,再以X的图形界面启动终端机,此时那个终端机并没有需要输入账号和密码,那个bash环境就是non-login shell。在原本的bash环境下再次执行bash命令,同样也没有输入账号密码就进入新的bash环境(前一个bash的子进程),新的bash也是non-login shell。

说了半天,那这两个shell到底有什么区别?

  • login shell:此种方式登录时,shell会重新读取/etc/profile和~/.bash_profile来应用新的环境变量。
  • non-login shell:此时shell不会读取/etc/profile和~/.bash_profile,而是读取~/.bashrc来应用新的环境变量。

话说标题是区别与影响,影响的东西自然源于区别。回到文章开头的问题,su切换用户没有获取用户的环境(当时用的命令是"su xxoo",这里xxoo为要切换的用户,以root身份执行的此命令),看来问题就在这倆login方式,根据现象我们可以判断su xxoo执行了non-login shell。

为了验证上面的判断,我们查看了su命令的简单帮助:

1
2
3
4
5
      
      
[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

很明显,我的判断是正确的,默认情况下是执行的non-login shell,因为要执行login shell需要在su后面添加参数"-"或"-l"或"--login"。所以要在su之后获取用户的环境变量,需要执行类似"su - xxoo"之类的命令。

自此,了解了login shell和non-login shell的区别。也解决了文章开头的问题。本文按理该结束了,但我还想说:PP更精彩!

  • PS1:bash在读取/etc/profile后会读取各账户的个人配置文件,所读取的个人配置文件主要有三个:~/.bash_profile,~/.bash_login,~/.profile。其实bash在读取的时候只会读取的上述三个文件的其中一个,而读取的顺序则就是上面的顺序。也就是说bash会依次查找上述三个个人配置文件,且找到一个后,后续的文件便不再读取
  • PS2:当以non-login shell登录时,如果希望获取用户的环境变量,执行"source ~/.bash_profile"即可(.bash_profile根据情况也可能是.bash_login或.profile),而其中source可以用小数点替换,即"source ~/.bash_profile"等价于". ~/.bash_profile"。source命令的解释可以自行搜索。
  • PS3:其实有的linux发行版的login shell也会读取.bashrc文件,如Fedora11中执行 "vim ~/.bash_profile"会发现 "if [ -f ~/.bashrc ]; then . ~/.bashrc fi"。这里就使用了PS2的提到的用小数点替换source特性,即在./bash_profile会读取执行.bashrc文件。


Difference between login shell and non-login shell

Understanding the distinction between your shell types, profile files and shell rc files is important because when you apply modifications to your system you need to know where  to set variables in order to make sure that they can be initialized as expected.

Let’s give a quick answer for this topic which covers the exact difference between the two shells.

When you login your system and see the command line prompt, it’s a login shell, and it executes these files in order:

  • /etc/profile
  • ~/.bash_profile
  • ~/.bashrc
  • /etc/bashrc

A non-login shell will only execute the two files in order:

  • /etc/bashrc
  • ~/.bashrc

So what’s a non-login shell?
When issuing this command:

$ ssh joseph@howto.admon.org ‘uptime’

you will gain a non-login shell at the remote side howto.admon.org. when you type bash after login, your new shell is also a non-login shell.

Now, you may have a clear answer to a common system administration question: How can I properly apply a new system variable like JAVA_HOME?

It’s better to set it in /etc/bashrc other than /etc/profile. Addtionally, there’re better choice. If you prefer to make your system clean and tidy, it’s suggested to create single config files in /etc/profile.d, the point is all these file would be initialized by both login shell and non-login shell:

$ grep profile /etc/profile /etc/bashrc
/etc/profile:# /etc/profile
/etc/profile:for i in /etc/profile.d/*.sh ; do
/etc/bashrc:# Environment stuff goes in /etc/profile
/etc/bashrc:        for i in /etc/profile.d/*.sh; do


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值