注销用户,如果他们打破了bashrc命令

当用户通过SSH访问我的Linux主机时,我正在运行一个小脚本。 该脚本应validation和/或设置用户的Google Authenticator MFA访问权限。

现在它可以像预期的一样工作 – 在MFAconfiguration过程中的任何时候,如果用户(即) CTRL + C ,设置向导被中断,但SSH会话继续。 我需要它注销用户试图访问。

我怎样才能做到这一点?

这就是我在.bashrc文件底部添加的内容(请注意,这对我来说是非常新的,对于我当前的尝试,我很乐于批评/改进)。

 # MFA validation/configuration if [[ -n $SSH_CONNECTION ]] ; then echo "SSH connection to remote host successful." echo "testing if MFA is configured..." # is this test enough? file="$HOME/.google_authenticator" if [ -f "$file" ] ; then printf "MFA configured, you may proceed.\n" else printf "MFA not configured; running setup wizard.\n" # the command runs, but the else bit is never reached if google-authenticator ; then # I reach this point if I go to the end of the wizard echo "MFA setup successful" else # this point is never reached echo "MFA setup failed - logging you out" exit fi fi fi 

你可以添加行

 trap '' 2 

在你开始的脚本中禁用CTRL + C

 trap 2 

最后启用CTRL + Cfunction。

这将防止用户制动脚本执行。

请注意,这应该被添加到/ etc / bashrc,使得它系统宽而不是用户可修改的

https://www.cyberciti.biz/faq/unix-linux-shell-scripting-disable-controlc/