当前位置: 首页>>技术问答>>正文


登录时获取的脚本序列

, , , ,

问题描述

我想将所有登录配置集中在我的~/.bash_profile中。默认情况下有一个~/.bashrc,但我用~/.bash_profile替换它。

但是,当我登录时,我的~/.bash_profile之前会收到一些内容并显示以下信息:

Linux ubnt10-dev1 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
Ubuntu 10.04.4 LTS

Welcome to the Ubuntu Server!
 * Documentation:  http://www.ubuntu.com/server/doc

  System information as of Fri May  9 12:17:39 EDT 2014

  System load:  0.01              Processes:           74
  Usage of /:   5.5% of 18.58GB   Users logged in:     0
  Memory usage: 4%                IP address for eth0: 123.x.x.x
  Swap usage:   0%

  Graph this data and manage this system at https://landscape.canonical.com/

New release 'precise' available.
Run 'do-release-upgrade' to upgrade to it.

Last login: Fri May  9 12:11:52 2014 from 123.x.x.x

我想删除它,以便我的~/.bash_profile中定义的只有我自己的启动问候语显示,但我不确定用户登录时源/执行的脚本的逻辑/顺序。你能启发我吗?

作为奖励,我还想知道系统启动时自动化源/执行的顺序,而不仅仅是当用户通过登录启动会话时,尽管这对我来说是一个不太重要的问题。

最佳解决方法

这有点复杂。首先,细节取决于您运行的shell类型。到plagiarize myself

  • 当您打开终端仿真器(例如gnome-terminal)时,您正在执行所谓的交互式non-login shell。

  • 从命令行登录计算机或运行命令(如su - username)时,您正在运行交互式登录shell。

  • 以图形方式登录时,您运行的是完全不同的东西。详细信息取决于您的系统和图形环境,但一般来说,它是处理您的登录的图形 shell 。虽然许多图形shell(包括Ubuntu默认)将读取/etc/profile~/.profile,但并非所有图形shell都可以。

  • 最后,当您运行shell脚本时,它将在non-interactive,non-login shell中运行。

bash在启动时将读取的文件取决于它运行的shell的类型。以下是man bash(强调我的)INVOCATION部分的摘录:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the –login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The –noprofile option may be used when the shell is started to inhibit this behavior.

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the –norc option. The –rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

那些是初始化文件。您还可以在/etc/environment中设置全局环境变量,但这些变量是读取而不是源(其中的命令不会执行,但会设置变量定义)。

现在,你看到的问候又是另一回事。这是在/etc/motd中设置的,并通过pam_motd显示。如man motd中所述:

The contents of /etc/motd are displayed by pam_motd(8) after a successful login but just before it executes the login shell.

The abbreviation “motd” stands for “message of the day”, and this file has been traditionally used for exactly that (it requires much less disk space than mail to all users).

On Debian GNU/Linux, the content of /run/motd.dynamic is also displayed. This file is generated by /etc/init.d/motd at boot.

要删除该消息,只需清空/etc/motd文件,并确保/etc/init.d/motd(如果存在)不生成任何内容。


无论如何,根据您显示的输出,您似乎通过ssh登录,这意味着您正在运行交互式登录shell,请参阅上面的含义。因此,总而言之,您登录时所关注的事项是(并按此顺序):

  1. SSH守护程序通过PAM库的pam_motd模块显示/etc/motd的内容。通过pam_env模块,它从/etc/environment~/.pam_environment设置环境变量。

  2. 启动登录shell,并按顺序读取以下文件:

    1. /etc/profile

    2. /etc/bash.bashrc(默认的Ubuntu /etc/profile来源/etc/bash.bashrc)。

    3. ~/.bash_profile。此处可以读取的其他文件(~/.profile~/.bash_login)将被忽略,因为存在~/.bash_profile

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/6252.html,未经允许,请勿转载。