使用 GUI 或命令行登录的所有用户的列表

使用 GUI 或命令行登录的所有用户的列表

我需要计算特定用户每天登录的总时间。并通过关闭系统来阻止用户使用计算机超过允许的时间。

uptime由于这是一台多用户计算机,因此无法使用

last命令无法完成这项工作,因为据我观察,它只存储运行终端的用户的数据。如果用户使用 GUI 登录并继续使用计算机而不运行终端,则会出现一个条目不是写入 /var/log/wtmp。

有没有办法找到我需要的信息?我使用的是 Ubuntu 12.04 LTS

答案1

who/var/run/utmp如果用户通过 X 登录,(使用)也应该可以工作。

然后您可以通过以下方式运行该脚本/etc/crontab 每一分钟。 它能做什么:

  • 循环遍历$u当前登录的所有用户 ()
  • 增加数字/var/log/accounting-$u——由于每分钟进行一次,因此文件存储了用户$u迄今为止登录的总时间。
  • 检查是否$u已达到限制($allowedtime),此处为 60 分钟。如果已达到,则关闭系统或采取其他措施。提前 5 分钟只会发出警告(@terdon 提供)。
  • $u最后,如果过去 24 小时内没有见到该用户,则删除/var/log/accounting-$u该用户,然后游戏可以从头开始。

正如我在评论中提到的,我不认为关闭系统是个好主意。尤其是对于这个脚本,因为如果用户在 24 小时尚未结束时再次登录,则关机操作将在不到一分钟后触发(下次启动 cron 时accounting.sh)。


会计.sh

#!/bin/bash

accountinglogprefix=/var/log/accounting-
allowedtime=60

for u in $(who | cut -d " " -f 1 | sort | uniq); do

  if [[ -e ${accountinglogprefix}${u} ]]; then
    consumed=$(cat ${accountinglogprefix}${u})
  else
    consumed=0
  fi
  echo -n $(( consumed + 1 )) > ${accountinglogprefix}${u}

  if [[ $consumed -gt $allowedtime ]]; then 
    # time is over, do whatever you want
    echo "Shutting down..."
  elif [[ $consumed -gt $(( allowedtime - 5 )) ]]; then
    # notify the user $u that his time is over in 5 minutes with a suitable command
    echo "Time's up! Shutting down in 5 minutes..."
  fi

  # check if e.g. 24h have passed since the user was last seen
  if [[ $(( $(date +%s ) - $(stat -c %Y ${accountinglogprefix}${u}) )) -gt $(( 24 * 3600 )) ]]; then
    rm ${accountinglogprefix}${u}
  fi

done

笔记:这不是一个完全成熟的脚本(即可复制和粘贴)——它只是展示一种不同的方法。

答案2

人们不需要编写自定义脚本,而是可以使用现有的、专为完成这一任务而设计的软件。

请检查是否psacct可以安装。

从:http://www.cyberciti.biz/tips/howto-log-user-activity-using-process-accounting.html

psacct 包中包含几个用于监控进程活动的实用程序,包括 ac、lastcomm、accton 和 sa。

The ac command displays statistics about how long users have been logged on.
The lastcomm command displays information about previous executed commands.
The accton command turns process accounting on or off.
The sa command summarizes information about previously executed commmands.

答案3

您还可以尝试解析输出ps

$ ps -U terdon -u terdon -o %t 
  ELAPSED
  00:39
  00:06

在此示例中,用户terdon正在运行两个进程,其中最早的进程已运行了 39 秒。因此,如果您解析此进程以获取运行时间最长的进程(可能是用户的登录 shell),则可以知道用户已登录多长时间。在此示例中,我假设您希望至少允许整整一个小时(这就是我在命令NF>=3中使用的原因gawk),但不超过 4 个小时:

ps -U terdon -u terdon -o %t | grep : | sed 's/ *//g' | 
   gawk -F"[ +:]" 'NF>=3{print $1}' | sort -g | tail -n 1

此命令将打印已过去的小时用户 terdon 运行时间最长的命令。现在您可以检查该命令是否大于或小于 4:

limit=4;
time=$(ps -U terdon -u terdon -o %t |  grep : | sed 's/ *//g' | 
        gawk -F"[ +:]" 'NF>=3{print $1}' | sort -g | tail -n 1);
 if [[ $time -gt 3 ]]; 
 then echo "Time's up! Shutting down in 5 minutes...";
      shutdown -h +5
fi

答案4

您可以审核您的安全日志。它位于:

/var/log/安全

它为您提供登录和注销信息。您还可以运行 logwatch,它会告诉您在运行期间谁登录过。

这里还有更多信息:

1. 检索系统上所有成功的登录

cat /var/log/secure* | grep Accepted > logins.txt

现在 logins.txt 将包含所有成功登录到您的 Redhat Linux 系统的信息。您可以浏览该文件并先进行手动查找。

2. 在特定时间检查用户

其他命令用于查找特定用户的上次登录详细信息最后,最后显示最后登录用户列表的命令。

最后一个程序通过扫描 /var/log/wtmp 文件来打印最近用户登录时间的详细报告。

输出包括以下详细信息:

Username
Tty device number
Login date and time
Logout time
Total working time

它还有一个选项可以搜索特定时间的登录,如下所示:last -t YYYYMMDDHHMMSS – 显示截至指定时间的登录状态。

其中使用的文件为:/etc/utmp – 这是一个二进制文件,包含每个活动 tty 线路的记录。/var/adm/wtmp – 跟踪登录和注销。

相关内容