linux下设置history默认历史记录数

深度链接 / 2023-12-06 21:47:55 / 107

linux下如何设置history默认历史记录数?

1、系统环境

[root@wrx ~]# uname -sir
Linux 2.6.32-696.el6.x86_64 x86_64


2、临时生效设置

#临时生效设置,重启后失效
[root@wrx ~]# history
    ……
    ……
    ……
  271  Modules/Setup
  272  cd ..
  273  ls
  274  cd -
  275  make && make install
  276  vim Modules/Setup
  277  make && make install
  278  pip3
  279  pip3 install pyinstaller
  280  cd 
  281  ls /etc/ssh/
  282  vim /etc/ssh/sshd_config 
  283  service sshd restart
  284  vim /etc/ssh/sshd_config 
  285  service iptables status
  286  vim /etc/ssh/sshd_config 
  287  service sshd restart
  288  exit
  289  uname -sir
  290  history
#设置history命令只显示最后10条
[root@wrx ~]# export HISTSIZE=10

#查看效果
[root@wrx ~]# history
  282  service sshd restart
  283  vim /etc/ssh/sshd_config 
  284  service iptables status
  285  vim /etc/ssh/sshd_config 
  286  service sshd restart
  287  exit
  288  uname -sir
  289  history
  290  export HISTSIZE=10
  291  history
[root@wrx ~]#


3、永久生效设置

#永久生效,需要将命令追加到配置文件中
[root@wrx ~]# echo "export HISTSIZE=10" >> /etc/profile

#生效配置文件
[root@wrx ~]# source /etc/profile


4、为了安全还需要进一步设置,修改保存历史记录条数

#在家目录下查看隐藏文件.bash_history
[root@wrx ~]# cat .bash_history 
vim /etc/ssh/sshd_config 
service sshd restart
exit
uname -sir
history
export HISTSIZE=10
history
echo "export HISTSIZE=10" >> /etc/profile
source /etc/profile
reboot

#调小.bash_history 文件大小
[root@wrx ~]# echo "export HISTFILESIZE=10" >> /etc/profile