如何为系统上的所有用户编辑 bash_logout [已关闭]

问题描述 投票:0回答:2

现在我知道我可以编辑 bash_logout 以在用户键入 exit 后执行命令,但仅限特定用户通过在其主目录中编辑 bash_logout 来执行。但如何为所有用户编辑此内容?

linux bash debian
2个回答
5
投票

bash
无法识别系统范围的注销脚本。*您能做的最好的事情就是创建一个世界可读的文件,例如
/etc/bash_logout
,并建议用户将
. /etc/bash_logout
添加到他们的个人
~/.bash_logout
文件中.

bash
唯一识别的系统范围配置文件是用于登录 shell 的
/etc/profile
。你可以添加

trap 'on_logout' EXIT

(其中

on_logout
是您定义的包含所需注销代码的函数的名称)到此文件,并希望用户不要重置处理程序。 (从技术上讲,
/etc/profile
在所有 POSIX 兼容 shell 之间共享,因此不要在此类处理程序中放置任何
bash
特定的代码。)


* 默认情况下。不过,有一个编译时选项可以启用

/etc/bash.bash_logout


1
投票

chepner 上面所说的……如果你想成为 BOFH,你可以相当轻松地为每个用户创建一个 .bash_logout ……

基于您的操作系统/发行版的变化可能(将会?)适用...假设所有普通用户都存在于本地(您没有提到 LDAP)并且 UID 在 [1000...50000] 范围内...

# iterate over all user accounts that are likely to be people with awk,
# print their home, loop over those homes and copy the logout file into place.
awk -F: '$3 >999 && $3 < 50000 { print $6}' /etc/passwd | while read home
do
  cp /path/to/your/logout-script $home/.bash_logout
  # if you want to be **reallY** nasty uncomment the next two lines.
  # that will stop users from deleting the file.
  # chown root $home/.bash_logout
  # chattr +i $home/.bash_logout
done

如果您以root身份运行系统上的每个人(请小心,请参阅上面的假设,确保没有系统用户受到攻击...)将获得您想要的注销文件。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.