linux非root用户记录命令执行时间
-
Linux的root用户拥有系统的最高权限,可以随意执行任何命令。但在一些安全要求较高的环境中,我们需要限制root用户的使用,尽量降低系统被滥用的风险。而普通用户在Linux系统中只拥有自己的一部分权限,无法执行一些需要特殊权限的命令。
为了更好地监控普通用户的行为和提高系统的安全性,我们可以记录普通用户执行命令的时间。这样一来,如果系统出现异常情况,我们可以根据记录的时间信息追踪到是哪个用户在什么时间执行了哪些命令。
要实现非root用户记录命令执行时间,我们可以借助Linux的Audit子系统。Audit子系统可以跟踪系统的各种操作,例如登录、文件访问、命令执行等。我们可以使用auditd服务来启动和配置Audit子系统。
首先,我们要确保审核系统已安装。我们可以使用以下命令检查是否已安装:
“`shell
yum list installed | grep audit
“`如果没有安装,我们可以使用以下命令安装auditd服务:
“`shell
yum install -y audit
“`安装完成后,我们需要编辑Audit的配置文件`/etc/audit/audit.cfg`,将`auditd`服务的启动方式设为启动时自动启动。默认情况下,配置文件的内容应该是类似下面这样的:
“`shell
# This file controls the configuration of the audit daemon
# Commented out below are examples of how to set realtime notifications via
# shell and netlink.# First possible realtime configuration scenario
# LOCAL_PERIOD=100
# NUM_SLOTS=600# Second possible realtime configuration scenario
# LOCAL_PERIOD=1000
# NUM_SLOTS=60# This space separated list of 16bit hex number should determine which event
# classes are being dropped during receiving of signal.
# For example, FF00 would drop all event classes through execve and mmaps and
# MM00 would drop all user-space events, but keep almost everything else.
# EVENT_DROP=0000# This field tells auditd how many bits to use in the inode field
# in correlated logs. This is useful for operators who have systems
# where the inode will never exceed a certain amount. A setting of 0
# turns off inode recording. INODE_RECORD_SIZE=256# Interleave is how many unlabeled records are allowed before labeling
# begins again when audits are turned off. The default is 48 unbabeled
# records, so at 56 records the audit buffers are flushed. BUFFERS_UNLAABLED=48# The -f and -F flags tell speculation reduction code where to start the
# file and filesystem recording, if at all. A default of 4 means
# filesystem and file recording skips the first 3 changes of audited
# objects. 0 means every single one will be recorded. -1 will mean
# no recording is done. FILES_GROUP=-1
# DIRS_GROUP=-1# This is the maximum number of operations queues per port_fd.
# The performance optimalization of assumption-based queue with
# no ordering support lays on the fact, it doesn’t use any locks on
# the saveitian operation. It means all authorizations are in il and
# and no locking order like device->inode->object exists. By using
# multiple queues per port we are able to minimize the blocking
# when accessing device. QUALITY_OF_SERVICE=1# Maximum number of file watches MAX_LOG_SIZE=”5″
# Maximum time in minutes for file watches MAX_LOG_TIME=”5″# Set this to no if you want to make use of the new file tagging
# facilities in the kernel. Controlling this setting via augenrules(8)
# is better. FILE_TAGGING=”yes”# Set number of keypairs to allocate in queue. More means less grow
# works during boot, while explode will take even more memory. For
# big hardware settings use SPLIT_ME=1000. To not use collapse feature
# use SPLIT_ME=0 SPLIT_ME=200# Default file permissions for log files
# LOG_FILE_PERM=0640# Default logfile auid,xppe Separator
# LOG_SEP=x# Action for failed logins, allowed values are single, pair, nonzero
# SINGLE_USERNAME=”single”
# PAIR_USERNAME=”pair”
# NONZERO_EXIT=”nonzero”# Change this to make audit operate as an unprivileged user and group
# AUDITD_USER=”root”
# AUDITD_GROUP=”root”
“`我们需要将其中的`AUDITD_USER`和`AUDITD_GROUP`两行更改为我们希望的用户名和用户组。例如,我们可以将其改为如下所示:
“`shell
AUDITD_USER=nobody
AUDITD_GROUP=nobody
“`然后,我们需要编辑`/etc/audit/audit.rules`文件,配置Audit日志记录的规则。我们可以添加以下规则来记录用户的命令执行时间:
“`shell
-a exit,always -F arch=b64 -S execve -k command_execution
-a exit,always -F arch=b32 -S execve -k command_execution
“`这两行规则会在用户执行execve系统调用时生成命令执行的Audit事件,并将事件关键字设置为command_execution。我们可以根据关键字来搜索和分析Audit日志。
编辑完成后,我们需要重新加载Audit配置并启动auditd服务。我们可以使用以下命令来重新加载Audit配置:
“`shell
auditctl -R /etc/audit/audit.rules
“`然后使用以下命令来启动auditd服务并设置为开机自启动:
“`shell
systemctl start auditd
systemctl enable auditd
“`auditd服务启动后,就会开始记录普通用户的命令执行时间。我们可以使用以下命令来查看并搜索Audit日志:
“`shell
ausearch -k command_execution
“`这条命令将搜索关键字为command_execution的Audit事件并显示相关信息。
通过以上步骤,我们可以实现非root用户记录命令执行时间的功能。这样一来,我们可以更好地监控普通用户的行为,并提高系统的安全性。
2年前 -
在Linux系统中,通常只有root用户有权限记录命令的执行时间。但是,可以使用一些工具和技巧来让非root用户也能记录命令的执行时间。下面是一些方法:
1. script命令
使用script命令可以记录终端会话的所有内容,包括输入和输出。非root用户可以使用这个命令来记录他们执行的命令以及命令的执行时间。只需在终端中运行`script`命令,然后执行需要记录的命令。结束后,使用`exit`命令退出终端会话,并在相应的目录下找到生成的记录文件。2. GNU time命令
GNU time命令是一个统计程序执行时间的工具,可以用于记录命令的执行时间。非root用户可以通过在命令前面加上`/usr/bin/time -v`来使用该命令。例如,要记录`ls`命令的执行时间,可以输入`/usr/bin/time -v ls`。执行完命令后,会输出命令的执行时间和其他统计信息。3. 自定义bash函数
非root用户可以在.bashrc文件中定义一个自定义的bash函数来记录命令的执行时间。可以使用以下代码:“`bash
function record_command_time {
start_time=$(date +%s.%N)
$@
end_time=$(date +%s.%N)
execution_time=$(echo “$end_time – $start_time” | bc)
echo “Execution time: $execution_time seconds”
}alias record=’record_command_time’
“`只需将以上代码添加到非root用户的.bashrc文件中,然后重新登录终端或执行`source ~/.bashrc`以使其生效。然后,使用`record`命令来记录想要统计执行时间的命令。
4. 使用time命令和tee命令结合
非root用户可以使用time命令和tee命令结合的方法来记录命令的执行时间。只需将以下代码作为命令行输入:“`bash
(time command_to_execute) 2>&1 | tee execution_time.log
“`将`command_to_execute`替换为想要统计执行时间的命令。执行完命令后,执行时间将会保存在execution_time.log文件中。
5. 使用process accounting
如果有root权限的用户可以启用Linux系统的process accounting功能来记录所有用户的命令执行时间。可以使用`accton`命令来启用process accounting功能,例如`accton /var/log/pacct`。然后,可以使用`sa`命令来查看用户的命令执行时间记录,例如`sa -u`。注意,使用process accounting功能需要root权限。这些方法可以让非root用户记录命令的执行时间,提供了一些灵活的选择。根据实际情况选择合适的方法来满足需求。
2年前 -
在Linux系统中,非root用户默认是没有权限记录命令执行时间的。然而,我们可以通过使用auditd工具,来实现非root用户记录命令执行时间的功能。
Auditd是Linux上用于记录系统活动的工具,可以监控文件的访问、系统调用以及命令执行等活动。下面是详细的操作流程:
1. 安装auditd工具。
在大多数Linux发行版中,auditd工具已经包含在默认的软件仓库中。以Ubuntu为例,可以使用以下命令安装auditd:
“`
sudo apt-get update
sudo apt-get install auditd
“`2. 配置auditd。
打开audit的配置文件/etc/audit/auditd.conf,找到如下两行,确保其为以下设置:
“`
write_logs = yes
log_file = /var/log/audit/audit.log
“`3. 设置audit规则。
创建一个文件/etc/audit/rules.d/execute.rules,可以使用以下命令创建:
“`
sudo touch /etc/audit/rules.d/execute.rules
“`打开该文件,并添加以下内容:
“`
-a exit,always -F arch=b64 -F euid=1000 -S execve -k execute-events
-a exit,always -F arch=b32 -F euid=1000 -S execve -k execute-events
“`其中,-F选项用于指定规则的过滤条件,-a选项用于添加规则。上述规则表示只记录用户ID为1000的用户执行的命令。
4. 重新加载audit规则。
使用以下命令重新加载audit规则:
“`
sudo service auditd restart
“`5. 查看记录的命令执行时间。
记录的命令执行时间存储在/var/log/audit/audit.log文件中,你可以使用以下命令查看:
“`
sudo cat /var/log/audit/audit.log | grep “type=EXECVE”
“`注意:上述命令只会显示执行的命令,如果你想要获取更详细的信息,可以使用其他的grep选项进行过滤和解析。
因为auditd记录的日志是以二进制格式存储的,所以有可能会比较难以阅读。你可以使用ausearch和aureport工具来解析和分析日志,从而获得更友好的输出。
以上就是在Linux中非root用户记录命令执行时间的方法和操作流程。通过配置auditd工具,我们可以轻松地实现对非root用户命令执行时间的记录和追踪。
2年前