Linux (Bash):是否可以使用带有退出条件或 Exec 命令的登录脚本?

Linux (Bash):是否可以使用带有退出条件或 Exec 命令的登录脚本?

我想为所有用户运行一个登录脚本,检查用户是否是管理员,如果是,则提供主机登录选项,或启动systemd-nspawn默认容器 - 这样所有用户都在容器中启动,除非他们是管理员,他们主动选择直接登录主机以修复问题。

#! /bin/bash


# Break on error (log out)

set -e



# Prevent breakout from script

set -o ignoreeof

trap "" 1 2 3 6 9 15 17 18 19 20 21 22 23 24 26 27



# Trap exit conditions

trap "echo -e '\n\nDropping to CLI\n'" EXIT



# Suppress cursor

tput civis



#  Get list of groups to which user belongs

   groupList=$(id -G)



#  Remove user's primary group from head of list

   groupList=${groupList#* }



   for i in $groupList ; do


       if  [[ $i -gt 0 ]] && [[ $i -lt 1000 ]] ; then : # Ignore non-root GIDs lower than 1,000


       else

           case $i in 
                     0|1000|2000|30000) # If user is an (root|org|domain|host) administrator

                                          echo -e '\n'

                                          while true; do # Offer bare metal login

                                                        echo -en "Administer Host Machine [y/N] ? \r"

                                                        read -s -n 1 yesno

                                                        case $yesno in 
                                                                      [Yy]* ) # Exit to host system

                                                                                tput cnorm # Restore cursor

                                                                                exit

                                                                           ;;

                                                                      [Nn]* ) # Pass control to guest machine login

                                                                                tput cnorm # Resore cursor

                                                                                exec '/usr/local/sbin/scripts/system/defaultLogin'
                                                                           ;;
                                                        esac
                                          done
                                     ;;



                                     *) # Otherwise, pass control to guest machine login

                                          exec '/usr/local/sbin/scripts/system/defaultLogin'
                                     ;;

           esac
       fi
   done

然而,尽我所能,我找不到一种方法来阻止它自动注销用户。

我尝试过抑制set -eEXIT陷阱。

我尝试将条件exit中的替换[Yy]. '/usr/local/sbin/scripts/system/return-error',获取一个仅包含以下内容的脚本

#! /bin/bash

return 1

...强制set -e中断并下降到 CLI。

/etc/profile.d如果我将脚本从另一个位置移至另一个位置并从用户的位置获取脚本,这并不重要.bashrc- 因此,它甚至不适用于个人用户。

没关系,如果我删除除exec- 以外的所有内容,也会导致自动注销。

有什么办法可以做到这一点,还是这是一个失败的原因?

如果可能的话,我真的宁愿不必维护.bashrc文件 - 除了管理方面的麻烦之外,这要么意味着用户可以通过编辑它来搞砸事情,要么我必须更改所有权,这意味着他们无法调整他们的文件自己的配置(这也不好)。因此,系统范围的解决方案是更好的选择。

提前致谢。

答案1

我想问题是你的source脚本确实不应该被获取。脚本的目的不是修改正在运行的 shell。

您可以将脚本设置为/etc/passwd.您可能还需要将其添加到/etc/shells

或者您不直接调用它,/etc/profile.d而是放一个包装器在那里,它只调用您的脚本:

[login-script-wrapper.sh]
#! /bin/sh
/path/to/your/script

相关内容