ssh登录挂钩?

ssh登录挂钩?

有没有办法在用户每次使用 ssh 连接时运行脚本/命令?是否可以全局配置(即运行脚本时任何用户登录)?

我在 Identica 上遇到了这个问题,但还没有答案,无论如何我想知道它。

答案1

对于所有用户,还是特定用户?对于单个用户,将其设置在他们的.bashrc文件中;对于所有用户,请查看pam_exec

如果用户来自sshd,您需要将以下行添加到/etc/pam.d/sshd;其他文件取决于其来源:

session optional pam_exec.so seteuid  /path/to/my/hook.sh

出于测试目的,该模块包含为optional,以便在失败时您仍然可以登录。确定其有效后,您可以更改optionalrequired.然后,除非您的钩子脚本执行成功,否则将无法登录。

笔记:与往常一样,当您更改登录配置时,请在后台打开备份 shell,并从新终端测试登录。

答案2

还有另一种方法只影响使用 ssh 的用户而不影响本地用户(在紧急情况下可能会更好)

请参阅下面 ssh 手册页中的片段。

在这种情况下,用户通常可以自己修改文件(有点像 .bashrc)

 ~/.ssh/rc
         Commands in this file are executed by ssh when the user logs in, just before the user's shell (or command) is started.  See the sshd(8) manual page for more information.

这是全局的,普通用户无法修改

 /etc/sshrc
         Commands in this file are executed by ssh when the user logs in, just before the user's shell (or command) is started.  See the sshd(8) manual page for more information.

相关内容