history 常见优化配置

文章目录

一、写在哪个文件生效?(关键)

✅ Bash 环境下生效位置(最常见)

1️⃣ 全局生效(所有用户)

✅ 推荐方式(最规范)

bash 复制代码
/etc/profile.d/history_optimize.sh

内容示例:

bash 复制代码
cat >> /etc/profile.d/history_optimize.sh << EOF
#当前 shell 会话中保存的命令条数
export HISTSIZE=10000
#历史文件中最多保存多少条命令
export HISTFILESIZE=50000
#时间戳(审计必备)
export HISTTIMEFORMAT="%F %T "
#空格开通连续重复不记录:整段历史中去重
export HISTCONTROL=ignoreboth:erasedups
#忽略指定命令
export HISTIGNORE="history*:exit:pwd:clear"
#启用追加模式(⭐多窗口必备),防止多终端 history 互相覆盖
shopt -s histappend
#实时写入历史
PROMPT_COMMAND='history -a'
EOF
chmod +x /etc/profile.d/history_optimize.sh
source /etc/profile.d/history_optimize.sh

📌 优点:

  • 系统级
  • 自动加载
  • 不影响用户自定义配置

2️⃣ 全局兜底(老系统)

bash 复制代码
/etc/profile

⚠️ 不推荐大量修改,容易被系统升级覆盖


3️⃣ 当前用户生效

bash 复制代码
~/.bashrc

或:

bash 复制代码
~/.bash_profile

📌 适用于:

  • 个人运维账号
  • 跳板机用户

✅ 各文件加载顺序(很重要)

text 复制代码
登录 shell:
/etc/profile
  ↓
/etc/profile.d/*.sh
  ↓
~/.bash_profile
  ↓
~/.bashrc

📌 结论:

  • 登录服务器 → 看 /etc/profile.d/
  • 非登录 shell(SSH 再 su) → 看 ~/.bashrc

二、不同场景推荐配置位置

场景 推荐文件
生产服务器所有用户 /etc/profile.d/history_optimize.sh
个人运维账号 ~/.bashrc
跳板机 /etc/profile.d/
容器基础镜像 /etc/profile.d/

三、验证是否生效

bash 复制代码
echo $HISTSIZE
echo $HISTFILESIZE
echo $HISTTIMEFORMAT
shopt histappend

查看历史:

bash 复制代码
history | head

四、一句话总结(运维必记)

history 优化配置 = 数量 + 时间 + 去重 + 实时写入 + 正确文件

相关推荐
GeW5 小时前
超详细!RHCE10版通用备考策略,内含评分细则解读
linux
用户041328465136 小时前
Linux 服务开机自启动怎么配置?systemctl enable 原理详解
linux
草莓熊Lotso15 小时前
【Redis 初阶】Set 类型深度解析:去重集合的运算能力与实战场景
linux·网络·数据库·windows·redis·tcp/ip·缓存
张文君15 小时前
ubuntu26.04坏道坏块分区隔离急速版260831-V0.12
linux·python
贝锐1 天前
从国产化信创设备到商用安卓终端,向日葵如何帮助企业实现统一运维管理?
linux·运维·远程控制
Julien20041 天前
调查和解决 SELinux 问题
linux·运维·服务器·网络·学习方法
小张同学a.1 天前
Docker 容器实战 1—— docker 基础与镜像构建
linux·运维·docker·容器
Best-Wishes1 天前
BurpSuite Pro教育版在linux系统中配置
linux·运维·服务器·网络安全·burpsuite
wuminyu1 天前
深入剖析 Panama Off-heap 的性能损耗与开销
java·linux·c语言·jvm·c++
虎王物联1 天前
RK3568嵌入式Linux跑Docker:内核配置排查到AIoT容器化的完整路径
linux·运维·docker·rk3568·aiot·嵌入式linux