ssh 登录钩子?

phu*_*ehe 16 ssh hook

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

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

Gle*_*rry 13

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

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

session optional pam_exec.so seteuid  /path/to/my/hook.sh
Run Code Online (Sandbox Code Playgroud)

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

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


Mar*_*l G 6

还有另一种方法只影响使用 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.
Run Code Online (Sandbox Code Playgroud)

这是全局的,普通用户不能修改

 /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.
Run Code Online (Sandbox Code Playgroud)

  • 但是/etc/sshrc只有在用户没有自己的~/.ssh/rc时才会执行,所以这个用户可以绕过 (2认同)
  • @isevcik,仅当在`/etc/ssh/sshd_config` 中将 PermitUserRC 设置为“yes”(有关详细信息,请参阅 `man sshd_config`) (2认同)