shell的交互式和非交互式登录

工作中经常碰见环境变量加载问题,归根结底就是配置文件的加载问题。

一般会有四种模式:交互式登陆、非交互式登陆、交互式非登陆、非交互非登陆。

交互式和非交互式对环境变量的加载:

+----------------+--------+-----------+---------------+
|                | login  |interactive|non-interactive|
|                |        |non-login  |non-login      |
+----------------+--------+-----------+---------------+
|/etc/profile    |   A    |           |               |
+----------------+--------+-----------+---------------+
|/etc/bash.bashrc|        |    A      |               |
+----------------+--------+-----------+---------------+
|~/.bashrc       |        |    B      |               |
+----------------+--------+-----------+---------------+
|~/.bash_profile |   B1   |           |               |
+----------------+--------+-----------+---------------+
|~/.bash_login   |   B2   |           |               |
+----------------+--------+-----------+---------------+
|~/.profile      |   B3   |           |               |
+----------------+--------+-----------+---------------+
|BASH_ENV        |        |           |       A       |
+----------------+--------+-----------+---------------+

bash的每种模式会读取其所在列的内容,首先执行A,然后是B,C。而B1,B2和B3表示只会执行第一个存在的文件。


交互式登陆:简单示例:

      a.用户直接登陆到机器获得的第一个shell

      b.用户使用  ssh user@remote 获得的shell

非交互式登陆:

      bash -l  script.sh

交互式非登陆:

      在已有的shell中运行bash,此时不需要登陆

非交互式非登陆:

      a. bash script.sh

      b. ssh user@remote command

 

为了更好的理清这几种模式,下面我们对一些典型的启动方式各属于什么模式进行一个总结:

  • 登陆机器后的第一个shell:login + interactive
  • 新启动一个shell进程,如运行bash:non-login + interactive
  • 执行脚本,如bash script.sh:non-login + non-interactive
  • 运行头部有如#!/usr/bin/env bash的可执行文件,如./executable:non-login + non-interactive
  • 通过ssh登陆到远程主机:login + interactive
  • 远程执行脚本,如ssh user@remote script.sh:non-login + non-interactive
  • 远程执行脚本,同时请求控制台,如ssh user@remote -t 'echo $PWD':non-login + interactive
  • 在图形化界面中打开terminal:  Linux: non-login + interactive  、Mac OS X上: login + interactive

 

参考链接:http://feihu.me/blog/2014/env-problem-when-ssh-executing-command-on-remote/

posted @ 2018-10-08 15:09  imcati  阅读(1381)  评论(0编辑  收藏  举报