Home > Backend Development > PHP Tutorial > Linux/Unix login script

Linux/Unix login script

伊谢尔伦
Release: 2023-03-05 11:12:02
Original
1379 people have browsed it

I don’t know if you have ever encountered such a scenario, when you need to set an environment variable, or run a program to set your shell or desktop environment, but you don’t know where is the most convenient place to set it.

There are some common situations, such as from Debian's package management program to Iaas management, many tasks need to set environment variables to run properly.

Sometimes, a program usually only needs to be run once on first login, such as the xrandr command.

Additionally, programs are occasionally injected into the shell, such as rbenv, rvn, or SitePoint’s own envswith program.

Let’s take a look at some common options that appear in Debian GNU/Linux Jessie installations and try to make sense of it all.

/etc/profile

By default, Debian provides the /etc/profile file. This file is used to set the $PATH variable ($PATH is usually used to declare the search path of the command). You can Effective immediately. The code below is part of /etc/profile.

if [ "`id -u`" -eq 0 ]; then
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
    PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH
Copy after login

For convenience, the path of the root user (ID is 0) is different from that of any other user. This is because the location of the system binary directory (sbin directory) is traditionally reserved for system management programs or programs that must be run as root. The games path is omitted for the root user, because it is impossible to use the root user to run the game program unless necessary.

Next, /etc/profile handles the setting of the $PS1 variable. The $PS1 variable is used to set the main prompt string (that is, the characters displayed when the user logs in). Except that the system shell is Bash, the system $PS1 variable is set to $ by default (the default for root users is #). If the system's shell uses Bash, the /etc/bash.bashrc file will replace the $PS variable to process the main prompt string (except in special circumstances). We will briefly talk about /etc/bash.bashrc later.

So from this point, we can infer that /etc/profile will be read by all shells during login (for example, using the login command). /etc/profile calls the id command to read the user ID, rather than using the more efficient Bash built-in variable ${UID}. Bash uses source-specific configurations rather than defining a fancy shell prompt, because Bash supports backslash-escaped special characters such as \u (username) and \h (hostname), which many other shells do not Support this definition. /etc/profile should attempt to be POSIX compliant so as to be compatible with any shells users may install themselves.

Debian GNU/linux usually comes pre-installed with Dash, a basic shell designed only to implement POSIX (and some Berkeley) extensions. If we modify /etc/profile (back up before modifying) to set a different value for the PS1='$ ' line, and then simulate a Dash login (through the dash -l command), we can see that Dash will use our customized hint. However, if we call the dash command without the -l parameter, dash will not read /etc/profile. At this time Dash will use the default value (which means that the value of PS1 at this time is the value before we modified it).

The last interesting thing related to /etc/profile is the following code snippet:

if [ -d /etc/profile.d ]; then
    for i in /etc/profile.d/*.sh; do
        if [ -r $i ]; then
            . $i
        fi
    done
    unset i
fi
Copy after login

In other words, anything readable that matches /etc/profile.d/*.sh will used as a variable source. This is very important because it shows that editing /etc/profile directly is never actually necessary (so restore your previous backup). Any variables defined above can be configured in a separate file and then override the settings in /etc/profile. The advantage of this is that it allows the corresponding changes to be automatically added to the /etc/profile file when the system is upgraded. Because Debian's Apt package management system usually does not modify the default configuration file.

~/.bash_profile, ~/.bash_login, and ~/.profile

One potential problem with /etc/profile is that it is located in a system-wide path. This means that modifying it will affect all users on this system. On a personal computer, this might not be too much of an issue, but modifying it also requires root privileges. For these reasons, each individual Bash user account can create any of the files ~/.bash_profile, ~/.bash_login and ~/.profil as a source of Bash configuration files. The first file found in the order listed will be used as the configuration file, the rest will be ignored.

Other shells, such as Dash, support something similar, but will only look for ~/.profile files. This allows users to configure separate .bash_profile files for Bash-specific use cases, if at some point they need to switch to Dash or another shell as the login shell (e.g. via the chsh -s dash command). ~/.profile can be retained as the configuration file for these shells.

需要牢记的一点是,默认的Debian框架目录(/etc/skel,用于存放要复制到新用户账户主目录的文件和目录)包含.profile文件,但不包含.bash_profile和.bash_login文件。此外Debian使用Bash作为默认的shell,因此,许多Debian用户习惯于将他们的Bash 登录shell设置放在.profile文件中。

我曾经看到过一些项目的安装说明,例如RVN,这个项目建议用户创建一个.bash_profile文件,但是这样做是非常危险的,根据上面提到的知识我们知道,这个会改变用户的shell环境。即使用户没有修改.profile文件,它也可能利用默认~/.profile功能,将~/bin添加到$PATH环境变量。一个可能提高安全性的选项是,在创建用户的账户之前,将.bash_profile作为.bash_rc的符号链接文件,放到/etc/skel目录中。

如果我们查看Debian Jessie的默认.profile脚本,我们可以看到下面的代码片段:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi
Copy after login

这和我们在/etc/profile里面看到的相似,如果shell是Bash,且发现了/etc/bash.bashrc文件,/etc/bash.bashrc文件就被当作Bash的配置文件。这一点的意义将在下一节讨论。

/etc/bash.bashrc 和 ~/.bashrc

启动的时候,Bash会同时读取/etc/bash.bashrc和~/.bashrc,但是只有在Bash Shell作为交互式Shell而不是登录Shell启动时(意味着通过xtem启动),会依照这种顺序,这是Bash Shell的标准行为。然而,Debian分别从 /etc/profile和~/.profile登录脚本中获取配置文件。这会显著地改变行为,使得/etc/bash.bashrc和.bashrc(如果它们存在)总是在Bash启动时调用,而不管是不是登录Shell。不要期待这种情况在不同地发行版中是一样的。

.bashrc是一个添加命令别名的好地方,实际上,一些用户拥有太多的别名,以至于他们宁愿将别名都放在一个单独的文件中去。Debian的默认.bashrc会查找.bash_alias,如果这个文件存在的话,会将它作为别名配置来源。所以你可以在这个文件中随意保存所有的Bash别名。如果用户愿意的话,.bashrc文件也是用户重写shell变量,例如$PS1或者$HISTSIZE的绝佳位置。Debian的默认.bashrc有超过100行,但是仍然可以非常清晰地阅读,且有良好地注释。见名知意,.bashrc不是其他非Bash shell的配置文件来源。

~/.xsession 和 ~/.xsessionrc

如果你是一个GNU/Linux桌面用户,通过显示管理器本地登录(而不是通过getty登录程序),则/etc/profile和~/.profile不会像预期的那样工作。一些显示管理器会直接将这些文件视为错误地配置文件,例如Gnome显示管理器。但一些其他的显示管理器,例如LightDm不会这样。幸运的是,你还有一些其他的选项。

当启动X Window系统会话时(不管是用显示管理或从虚拟终端启动startx),将会执行/etc/X11/Xsessionshell脚本。这基本上相当于登录shell调用/etc/profile。这个只对X Window生效,并且不是将其作为源配置文件,而是直接执行。但是它也相当复杂,类似于/etc/profile怎么从/etc/profile.d目录中的脚本读取配置,怎么从/etc/X11/Xsession.d/目录下的/etc/X11/Xsessions脚本中读取配置。在/etc/X11/Xsession.d目录下的所有脚本名称都以数字开头,因此所有的脚本都会按照数字顺序来读取。

Debian Jessie包含一个名叫40×11-common_xsessionrc的文件,这个文件做的工作就是检查~/.xsessionrc是不是可读的,如果是就用它作为配置文件的来源。这就使得~/.xsessions是一个加载环境变量或者运行一个一次性使用程序(例如xrandr或xmodmap)的完美位置(仅适用于X会话)。如果你希望的话,你同样可以将/etc/profile或~/.profile作为来源。那么任何指定的环境变量也都会被你的会话管理器继承(如果还没有继承的话)。请注意,默认情况下.xsessionrc是不存在的,需要你自己创建这个文件。

如果我们继续浏览/etc/X11/Xsession中的文件, 我们会发现50×11-common_determine-startup会决定加载哪个会话管理器。如果~/.xsessions文件存在而且是可执行的,它会被保存并且随后作为99×11-common_start的一部分执行,当~/.xsession用于运行会话管理器,X会话将会被注销。并且当这个脚本终止时,你会返回到显示管理器登录界面。

和~/.xsessionrc相似,~/.xsession默认也是不存在的,在你需要的时候你可以创建一个。你可能会创建一个类似下面给的简单的.xsession脚本

# Start our session manager of choice.
#
exec x-session-manager
Copy after login

其中x-session-manager默认设置为通过update-alternatives命令配置的任何内容,这样,你可以轻松地更改系统范围默认地会话管理器,只需要将x-session-manager替换为/usr/bin/startfce4(切换到XFCE),其他的用户账户将完全不受影响。

当然,许多显示管理器提供从登录界面直接选择公共会话管理器的能力,所以这个文件通常是不必要的。然而.xsession提供了更多地灵活性,你可以用任何程序调用这个文件,而不仅仅是会话管理器。例如,在这里你可以在while循环中调用chromium或者iceweasel,而不是执行基本的kiosk模式设置。

~/.bash_logout

我们前面介绍了当用户运行交互式Bash登录shell时读取的文件,但是如果你想在注销以后仍然运行程序该怎么办?对于这个用例,~/.bash_logout文件就非常方便了。在Debian中默认的配置仅用于清除屏幕(我认为从安全角度来说很重要),但是可以轻微地想象以下就知道能用于其他目的,例如,在你离开你的机器之前显示一个几秒钟的提醒。

主要的限制因素在于.bash_logout仅在注销交互式shell时读取,并且并不能假定它在注销X会话时会被加载。

其他选项

上面那些已经为你介绍了大部分的通用选项。其他的选项可能会存在,取决于你的安装环境(例如/etc/environment),但是我不认为他们可能在其他的平台上存在,并且极少有需要去接触它们。

示例

那么你应该在哪放置你的系统范围环境变量?如果你希望一个环境变量可以影响所有用户,/etc/profiled./someifle.sh会是一个好的选择。但是,这假设你是使用一个登录管理器以/etc/profile作为配置来源。如果不是这样,你可以(作为一个管理员)添加一个脚本到/etc/X11/Xsession.d/来替代/etc/profile作为配置来源。

如果你希望一个脚本可以找到一个私人目录路径,并且添加它到你的PATH中,你需要考虑这个目录是不是会移动很多东西,如果你向.profile添加代码来实现,用户需要注销然后再登录来更改用户会话期间的PATH。如果你将代码添加到.bashrc中,这意味着代码将在用户每次打开xterm时执行,如果执行大约半秒以上可能就不太理想。所以这是一个权衡取舍的问题。

如果你仅仅是为了你个人登录会话时的一个环境变量,且它只关心X会话,你可以将它添加到~/.xsessionrc中。这样做的优点是,它通常将可用于通过X会话管理器启动的所有程序,因为它在启动X会话管理器之前被设置,并且被继承。例如,某些图形驱动程序可以通过运行

export vblank_mode=0
Copy after login

来禁用vsync。 所以位于.xsessionrc中的变量会影响到所有的程序。

然而如果这一行被添加到.bashrc中,则只有通过xterm登录的程序会被影响。通过一个窗口管理器启动的程序照常运行。你可以把它添加到.profile,并且从.xessionrc作为.profile的来源。但是之后,当你的X服务没有在运行的时候,你就不需要导出环境变量。

希望你现在可以更好地了解了登录和注销脚本在Debian GNU/Linux系统上的工作原理。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template