由于自己实际的工作机是一台Linux,更准确的说是Debian 12

多数时间就是通过WindTerm ssh登录到这台机器上,然后开多个窗口,开始干活

那么查看历史命令就变得很重要了,需要全部记录下来,随时进行查看

如果是公司的服务器,也可以加上这个,统一把命令历史发送到统一的一台日志服务器上,进行保管和审计

Any,都很重要,记录一下:

一、编辑 /etc/profile,新增三句

export HISTTIMEFORMAT="%Y-%m-%d %H:%M "
export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$(whoami) [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//") [$RETRN_VAL]"'
export PROMPT_COMMAND="history -a;history -c;history -r;$PROMPT_COMMAND"

第一句是为了使用 history 的时候更好看一些

image-20251230100755555

二三句是为了把历史命令发到日志服务器去

二、安装并设置rsyslog

apt install rsyslog

vi  /etc/rsyslog.d/00-my-format.conf
# 定义一个简洁的时间格式模板:2025-12-30 09:50
$template MyPreciseFormat,"%TIMESTAMP:1:10:date-rfc3339% %TIMESTAMP:12:16:date-rfc3339% %HOSTNAME% %syslogtag%%msg%\n"

# 将这个模板设置为所有日志文件的默认格式
$ActionFileDefaultTemplate MyPreciseFormat

vi /etc/rsyslog.conf
...
auth,authpriv.*                 /var/log/auth.log
cron.*                          -/var/log/cron.log
kern.*                          -/var/log/kern.log
mail.*                          -/var/log/mail.log
user.*                          -/var/log/user.log

local6.*                        /var/log/commands.log
...

systemctl enable rsyslog
systemctl restart rsyslog

如上,就会把命令都发送到 /var/log/commands.log

如果要发送到统一的服务器,那就在 /etc/rsyslog.conf,把所有日志发到远程去

*.* @@10.10.10.1:514

如上就好了,重新登录,就会有记录了,cat /var/log/commands.log

image-20251230101213471