1.编辑脚本
sudo vim /usr/local/bin/login-notify.sh
例如 login-notify.sh
#!/bin/bash# 检查是否有一个原始SSH命令,并执行它
if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; thenecho "SSH_ORIGINAL_COMMAND: $SSH_ORIGINAL_COMMAND" >> /var/log/ssh-login-notify.log#exec $SSH_ORIGINAL_COMMAND BUG 有空格不会执行eval $SSH_ORIGINAL_COMMAND
else# 记录登录信息MESSAGE="localnews服务器:用户 $(whoami) 从 ${SSH_CLIENT%% *} 登录于
$(date)"echo $MESSAGE >> /var/log/ssh-login-notify.log# 执行用户的原始 shellexec $SHELL
fi
- sudo chmod +x /usr/local/bin/login-notify.sh
2.配置sshd
sudo vim /etc/ssh/sshd_config
新增
ForceCommand /usr/local/bin/ssh-login-notify.sh
重启
sudo systemctl restart sshd