我如何提取login历史logging?

我需要知道特定用户的login历史logging(即login和注销时间),如何在Linux中为特定date范围提取此历史logging?

你可以试试last命令:

 last john 

它打印出用户john的login/离开历史logging。 而正在运行

 last 

打印所有用户的login/离开历史logging。

如果你需要在历史上比一个月更远的话,你可以用last命令读取/var/log/wtmp.1文件。

last -f wtmp.1 john将显示上个月用户john的login历史logging。

最后一个日志输出不是太重,也比较容易parsing,所以我可能会将输出传给grep来寻找一个特定的date模式。

last john | grep -E 'Aug (2[0-9]|30) ' last john | grep -E 'Aug (2[0-9]|30) ' 8月20日至last john | grep -E 'Aug (2[0-9]|30) '日。 或者类似的东西:

last -f /var/log/wtmp.1 john | grep -E 'Jul (1[0-9]|2[0-9]|30) ' last -f /var/log/wtmp.1 john | grep -E 'Jul (1[0-9]|2[0-9]|30) ' 7月10日至last -f /var/log/wtmp.1 john | grep -E 'Jul (1[0-9]|2[0-9]|30) '日为用户john

如何在Linux中提取特定date范围的login历史logging?

列出所有用户从25日至28日/ 8月login的示例:

 last | while read line do date=`date -d "$(echo $line | awk '{ print $5" "$6" "$7 }')" +%s` [[ $date -ge `date -d "Aug 25 00:00" +%s` && $date -le `date -d "Aug 28 00:00" +%s` ]] && echo $line done 
  • awk '{ print $5" "$6" "$7 }'last输出中提取对应列的date时间
  • +%s将date时间转换为纪元时间
  • -ge代表大于或等于
  • -le表示小于或等于

您也可以使用last <username>为特定用户执行此操作。