您应该可以使用/etc/hosts.allow. 尝试类似:
sshd: ALL: (/usr/bin/echo "SSH connection from %h (%H)" | /usr/bin/mailx -s "SSH Alert" you@example.com)
Run Code Online (Sandbox Code Playgroud)
您可以从 运行的脚本中获取更多详细信息/etc/profile.d,或包含在/etc/profile. 但是,这仅在用户登录到交互式会话时才有效。
如果您不需要立即通知,该logcheck程序可以每小时通知您过去一小时内的任何访问。您需要在配置中添加适当的规则。
编辑:Ubuntu 使用不兼容的hosts_options格式来执行 shell 命令。以下规则是我实施的:
SSHD: ALL: spawn (/bin/echo "SSH connection to %H from %h[%a]" | \
/usr/bin/mailx -s "SSH Alert" me@example.com)
Run Code Online (Sandbox Code Playgroud)
注意:反斜杠符号可用于换行如上。替换字符记录在hosts.allow手册页中。