解决CentOS 7 history命令不显示操作记录的时间和用户身份问题

CentOS 6 中history命令显示操作命令的时间和用户身份
[root@node02 ~]# history
  294  2017-01-06 16:46:48  root clear
  295  2017-01-06 16:46:50  root ll
  296  2017-01-06 16:46:52  root cat hostname.sh 
  297  2017-01-06 16:46:56  root cat nginxlog_cut.sh 
  298  2017-01-06 16:47:29  root clear
  299  2017-01-06 16:47:34  root history
[root@node02 ~]# cat /etc/RedHat-release 
CentOS release 6.6 (Final)
[root@node02 ~]#

而CentOS 7中,history命令中不显示操作命令的时间和用户身份
[root@localhost data]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@localhost data]# history -n 10
[root@localhost data]# history 10
 1268  \
 1269  history
 1270  cat /etc/redhat-release
 1271  clear
 1272  cat /etc/redhat-release
 1273  history
 1274  clear
 1275  cat /etc/redhat-release
 1276  history -n 10
 1277  history 10
[root@localhost data]#

解决该问题只需要在/etc/profile中添加如下变量即可:
export HISTTIMEFORMAT=”%Y-%m-%d %H:%M:%S  `whoami` “
然后运行source /etc/profile命令即可,注意引号中的空格
[root@localhost data]# history 10
 1273  2017-01-05 19:40:18  root history
 1274  2017-01-05 19:40:27  root clear
 1275  2017-01-05 19:40:29  root cat /etc/redhat-release
 1276  2017-01-05 19:40:35  root history -n 10
 1277  2017-01-05 19:40:39  root history 10
 1278  2017-01-05 19:41:12  root cat /etc/profile
 1279  2017-01-05 19:42:16  root vim  /etc/profile
 1280  2017-01-05 19:42:26  root source  /etc/profile
 1281  2017-01-05 19:42:28  root history
 1282  2017-01-05 19:42:42  root history 10
[root@localhost data]#

至此history命令输出结果格式完美解决,如果要清除历史记录,可以运用history -c,具体history用法如下:
history命令的用法及参数usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg…]
参数:
n :数字,要列出最近的若干命令列表
-c :将目前的 shell 中的所有 history 内容全部消除
-a :将目前新增的 history 指令新增入 histfiles 中,若没有加 histfiles ,则预设写入 ~/.bash_history
-r :将 histfiles 的内容读到目前这个 shell 的 history 记忆中
-w :将目前的 history 记忆内容写入 histfiles

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/214632.html<

(0)
运维的头像运维
上一篇2025-04-11 21:22
下一篇 2025-04-11 21:24

相关推荐

  • hypervmart美国是什么?hypervmart美国官网入口

    2026 年 Hypervmart 美国站已全面接入 AI 智能选品与跨境物流自动化系统,成为中小卖家切入北美市场的首选低成本独立站解决方案,其核心优势在于“零库存 + 高转化”的闭环模式,随着 2026 年全球跨境电商进入“精细化运营”深水区,Hypervmart 美国站凭借其对北美消费者行为的深度洞察,在独……

    2026-05-02
    0
  • hypervmart是什么,hypervmart官网入口

    hypervmart 在 2026 年是否值得投资?核心结论:是,但需精准匹配供应链场景hypervmart 作为 2026 年跨境零售与 B2B 融合的新兴平台,其核心价值在于利用 AI 驱动的供应链优化技术,为中小卖家提供低于行业平均 15% 的履约成本,但成功与否高度依赖卖家对“跨境物流时效”与“海外仓选……

    2026-05-02
    0
  • RAKsmart独立服务器2026年测评,CN2 GIA实测数据与性能表现,CN2 GIA服务器到底怎么样,CN2 GIA独立服务器推荐

    RAKsmart 独立服务器在 2026 年已确立为连接中国内地与全球的高性能网络枢纽,其 CN2 GIA 线路实测延迟稳定在 35ms 以内,吞吐量突破 900Mbps,是跨境业务场景下兼顾稳定性与性价比的优选方案,核心网络性能深度解析2026 年 CN2 GIA 线路实测数据在 2026 年的网络架构中,R……

    2026-05-02
    0
  • hosteonsVPS测评,实测体验,hosteonsVPS怎么样?

    Hosteons VPS 在 2026 年的实测表现显示,其依托全球 BGP 线路优化与 NVMe 全闪存架构,在亚洲至北美跨洋延迟控制上表现优异,是追求高性价比与稳定性的中小型企业首选,但需注意其部分机房在晚高峰期的波动风险,核心性能与网络架构深度解析在 2026 年云计算基础设施全面向 AI 算力与边缘计算……

    2026-05-02
    0
  • BaCloud独立服务器测评不限流量实测表现,BaCloud独立服务器不限流量怎么样

    2026 年实测结论:BaCloud 独立服务器在不限流量场景下表现优异,特别适合高并发视频流媒体与大数据传输业务,其性价比与稳定性在同类竞品中处于第一梯队,但需关注其节点覆盖密度,在 2026 年云计算市场进入存量博弈与精细化运营并存的阶段,企业用户对于“不限流量”的诉求已从单纯的带宽大小转向实际吞吐能力与计……

    2026-05-02
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注