我正在尝试跟踪用户上次使用某个密钥登录 Linux 服务器的时间。用户secure
有 5 个密钥~/.ssh/authorized_keys
,我如何跟踪每个密钥的使用时间以及使用哪个 IP 访问服务器?
理想情况下,我可以看到密钥 1 最后一次使用是在 5 天前,来自 127.0.0.1;密钥 2 最后一次使用是在 10 分钟前,来自 10.0.0.5;密钥 3 和 4 从未使用过;密钥 5 最后一次使用是在 2 周前,来自 8.8.8.8。
答案1
您需要增加LogLevel
in/etc/ssh/sshd_config
并VERBOSE
重新启动 sshd。这将导致 sshd 将正在使用的密钥的指纹记录到您的 sshd 配置为使用的日志文件中,例如
Jan 7 22:46:17 host.lan sshd[5998]: Connection from 192.168.254.187 port 57062
Jan 7 22:46:17 host.lan sshd[5998]: Found matching RSA key: 54:d2:06:cf:85:ec:89:96:3c:a8:73:c7:a1:30:c2:8b
Jan 7 22:46:17 host.lan sshd[5998]: Found matching RSA key: 54:d2:06:cf:85:ec:89:96:3c:a8:73:c7:a1:30:c2:8b
Jan 7 22:46:17 host.lan sshd[5998]: Accepted publickey for user from 192.168.254.187 port 57062 ssh2
将密钥指纹与密钥匹配并执行您想要的操作应该相当简单。