history记录定制
摘要
本文讲解了一些关于history的使用和配置。
- history基础用法
- 搜索历史命令
- history记录的定制化
- 一些定制化实例
history基础用法
# 显示最近操作的所有命令
history
# 只显示最后的10条历史记录,history N
history 10
# 主动保存缓冲区的历史记录
history -w
# 执行敏感命令操作,执行-c,将缓冲区内容直接删除
history -c
# 查看history命令的帮助文档
man history搜索历史命令
交互式搜索历史命令
在命令行输入 Ctrl+r 后,进入交互界面
(reverse-i-search)`':键入需要搜索的关键字,会自动查找到最后一条相关命令
(reverse-i-search)`cd': cd ..可以通过多次键入 Ctrl+r 继续向前搜索相关命令
(reverse-i-search)`cd': cd /etc/nginx/conf.d/定制history记录
一般查看历史记录输出
# history 10
579 grep -i user /etc/php/8.3/fpm/pool.d/www.conf
580 grep -i user /etc/php/8.3/fpm/pool.d/www.conf -C 2
581 nginx -V
582 cat /etc/nginx/nginx.conf
583 cat /etc/nginx/nginx.conf.bak
584 history
585 exit
586 history
587 man history
588 history 10可以通过配置相关变量,来实现记录日志行数,记录日志格式,禁止记录日志等配置
HISTSIZE 控制缓冲区历史记录的最大个数
HISTFILESIZE 控制历史记录文件中的最大个数
HISTIGNORE 设置哪些命令不记录到历史记录
HISTTIMEFORMAT 设置历史命令显示的时间格式
HISTCONTROL 扩展的控制选项
export HISTCONTROL=ignoreboth
# ignorespace: 忽略空格开头的命令
# ignoredups: 忽略连续重复命令
# ignoreboth: 表示上述两个参数都设置
# 设置追加而不是覆盖
shopt -s histappend用户的 ~/.bashrc 文件中默认会有变量配置
# grep HIST .bashrc
HISTCONTROL=ignoredups:ignorespace
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000实例
禁止记录日志
将 HISTSIZE 和 HISTFILESIZE 设置为0
HISTSIZE=0
HISTFILESIZE=0处于某种特殊环境,我们需要禁用历史记录
echo "export HISTSIZE=0" >> ~/.bash_profile
echo "export HISTFILESIZE=0" >> ~/.bash_profile
source ~/.bash_profile
# 直接把上述两个变量的值设置为 0,就实现了禁用历史记录的功能黑客必知的一个小技巧
在命令前额外多加一个空格,这样的命令是不会被记录到历史记录的。
这个技巧如果在你的系统不管用,请查看下环境变量 HISTCONTROL 是否包含 ignorespace,貌似 centos 系统默认没有设置这个值
调整日志记录行数
控制历史记录总数
# 默认情况下,Linux 系统最多存储 1000 条历史记录,可以通过 HISTSIZE 环境变量查看
echo $HISTSIZE
# 对于需要做审计的场景,1000 条历史记录可能会太少了,我们可以修改为合适的值
export HISTSIZE=10000
# 注意,HISTSIZE 变量只能控制缓冲区中的历史记录数量,如果需要控制 .bash_history 文件存储的最大记录数,可以通过 HISTFILESIZE 进行控制
# 上述命令行修改只在当前 Shell 环境生效,如果需要永久生效,需要写入配置文件
echo "export HISTSIZE=10000" >> ~/.bash_profile
echo "export HISTFILESIZE=200000" >> ~/.bash_profile
source ~/.bash_profile更改历史记录文件名
# 有时,为了方便管理和备份,需要更改历史记录文件的路径和名称。简单,同样可以通过环境变量 HISTFILE 更改它的文件名称
echo "export HISTFILE=/data/backup/chopin.bash_history" >> ~/.bash_profile
souce ~/.bash_profile调整日志记录格式
通过设置 HISTTIMEFORMAT 变量,可以记录命令执行时间,时间戳会记录在日志文件中。
export HISTTIMEFORMAT='%F %T '
查看日志格式
39 2025-02-24 10:39:37 vim /etc/profile
40 2025-02-24 10:39:43 vim .bashrc
41 2025-02-24 10:39:50 grep HIST .bashrc
42 2025-02-24 10:47:55 ls /etc/skel
43 2025-02-24 10:47:57 ls -alh /etc/skel
44 2025-02-24 11:08:43 export HISTTIMEFORMAT='%F %T '
45 2025-02-24 11:08:45 history可以看到,历史记录已经显示了时间戳。其实这些对于审计需求,还不够,可以加上更详细的信息:
export HISTTIMEFORMAT="%F %T `who -u am i 2>/dev/null| awk '{print $NF}'|sed \-e 's/[()]//g'` `whoami` "查看日志格式
38 2025-02-24 10:34:24 113.142.*.* root grep HIST .*
39 2025-02-24 10:39:37 113.142.*.* root vim /etc/profile
40 2025-02-24 10:39:43 113.142.*.* root vim .bashrc
41 2025-02-24 10:39:50 113.142.*.* root grep HIST .bashrc
42 2025-02-24 10:47:55 113.142.*.* root ls /etc/skel
43 2025-02-24 10:47:57 113.142.*.* root ls -alh /etc/skel
44 2025-02-24 11:08:43 113.142.*.* root export HISTTIMEFORMAT='%F %T '
45 2025-02-24 11:08:45 113.142.*.* root history
46 2025-02-24 11:08:54 113.142.*.* root cat .bash_history持久化配置,就需要添加到 ~/.bashrc 或者 ~/.profile 文件中
HISTTIMEFORMAT='%F %T '也可以通过创建一个脚本文件,放置在 /etc/profile.d 目录中的方式来定制日志记录,将相关定制history的设置添加进去。但是可能会被 ~/.bashrc 中的配置覆盖。
swift@cloud-test-001:~$ cat /etc/profile.d/history.sh
export HISTSIZE=10000
export HISTTIMEFORMAT="%F %T `whoami` "
swift@cloud-test-001:~$ ls -alh /etc/profile.d/history.sh
-rw-r--r-- 1 root root 62 Mar 12 2019 /etc/profile.d/history.sh
swift@cloud-test-001:~$ history
1008 2021-10-13 14:45:55 swift crontab -l
1009 2021-10-13 14:45:58 swift date
swift@cloud-test-001:~$ echo $HISTSIZE
1000
为新用户定制配置
为后续新添加的用户配置history设置,可以通过修改模板文件 /etc/skel 目录中的文件来实现,因为创建新用户的时候,会基于 /etc/skel 创建家目录
ls -alh /etc/skel
total 28K
drwxr-xr-x 2 root root 4.0K Apr 23 2024 .
drwxr-xr-x 124 root root 12K Feb 24 10:39 ..
-rw-r--r-- 1 root root 220 Mar 31 2024 .bash_logout
-rw-r--r-- 1 root root 3.7K Mar 31 2024 .bashrc
-rw-r--r-- 1 root root 807 Mar 31 2024 .profile所以可以将相关配置添加修改到 /etc/skel/.bashrc 文件中,这样后续新增用户,默认就是定制的history配置