CentOS SSH日志如何查看和分析

在 CentOS 系统中,SSH 日志文件通常位于 /var/log/secure。要查看和分析 SSH 日志,你可以使用以下命令:

  1. 查看所有 SSH 登录尝试:
sudo cat /var/log/secure | grep sshd
  1. 查看特定时间段的 SSH 登录尝试:
sudo grep 'sshd' /var/log/secure --since "2021-09-01 00:00:00" --until "2021-09-30 23:59:59"

将日期和时间替换为你感兴趣的时间段。

  1. 分析失败的 SSH 登录尝试:
sudo grep 'Failed password' /var/log/secure
  1. 分析成功的 SSH 登录尝试:
sudo grep 'Accepted password' /var/log/secure
  1. 分析 SSH 登录尝试次数(按 IP 地址排序):
sudo grep 'sshd' /var/log/secure | awk '{print $11}' | cut -d':' -f1 | sort | uniq -c | sort -nr
  1. 分析 SSH 登录尝试次数(按用户排序):
sudo grep 'sshd' /var/log/secure | awk '{print $1}' | sort | uniq -c | sort -nr

这些命令可以帮助你查看和分析 SSH 日志。你可以根据需要修改它们以满足你的需求。在分析日志时,请注意异常行为,例如多次失败的登录尝试、来自未知 IP 地址的登录尝试等。这些可能是潜在的安全威胁,需要进一步调查和处理。