Linux中如何限制Shell命令记录的数量

默认情况下,bash会在文件$HOME/.bash_history中存放多达500条命令记录。有时根据具体的系统不同,默认记录条数不同。系统中每个用户的主目录下都有一个这样的文件。

示例如下:

[root@localhost ithov]# ls -la

总用量&">nbsp;164  drwx------. 28 ithov ithov 4096 6月  18 16:18 .  drwxr-xr-x.  5 root  root  4096 6月  11 11:50 ..  drwxrwxr-x.  2 ithov   500 4096 6月   8 16:34 .abrt  -rw-------.  1 ithov ithov  106 6月  18 10:46 .bash_history  -rw-r--r--.  1 ithov   500   18 2月  22 05:05 .bash_logout  -rw-r--r--.  1 ithov   500  176 2月  22 05:05 .bash_profile  -rw-r--r--.  1 ithov   500  124 2月  22 05:05 .bashrc  drwxr-xr-x.  3 ithov   500 4096 6月   8 16:34 .cache  drwxr-xr-x.  5 ithov   500 4096 6月   8 16:34 .config  drwx------.  3 ithov   500 4096 6月   8 16:34 .dbus  -rw-r--r--.  1 ithov ithov   31 6月  18 14:40 .dmrc  -rw-------.  1 ithov   500   16 6月   8 16:34 .esd_auth  drwx------.  4 ithov   500 4096 6月  18 14:40 .gconf  drwx------.  2 ithov   500 4096 6月  18 16:32 .gconfd  drwxr-xr-x.  6 ithov   500 4096 6月   8 16:43 .
gnome2  drwx------.  2 ithov ithov 4096 6月   8 16:43 .gnome2_private  drwxrwxr-x.  3 ithov   500 4096 6月   8 16:34 .gnote  drwx------.  2 ithov   500 4096 6月  18 14:40 .gnupg  drwxrwxr-x.  2 ithov   500 4096 6月   8 16:34 .gstreamer-0.10  -rw-rw-r--.  1 ithov ithov  190 6月  18 14:40 .gtk-bookmarks  drwx------.  2 ithov   500 4096 6月   8 16:34 .gvfs  -rw-------.  1 ithov   500 2484 6月  18 14:40 .ICEauthority  -rw-r--r--.  1 ithov ithov  871 6月  18 16:32 .imsettings.log  drwxr-xr-x.  3 ithov   500 4096 6月   8 16:34 .local  drwxr-xr-x.  5 ithov   500 4096 6月   8 16:43 .
mozilla  drwxr-xr-x.  2 ithov   500 4096 6月   8 16:34 .nautilus  drwx------.  2 ithov ithov 4096 6月   8 16:34 .pulse  -rw-------.  1 ithov   500  256 6月   8 16:34 .pulse-cookie  -rw-------.  1 ithov ithov  218 6月  10 11:13 .recently-used.xbel  drwx------.  2 ithov   500 4096 6月   8 16:34 .ssh  -rw-------.  1 ithov ithov 2318 6月  18 16:32 .xsession-errors  -rw-------.  1 ithov ithov 2133 6月  18 10:46 .xsession-errors.old  drwxr-xr-x.  2 ithov   500 4096 6月   8 16:34 公共的  drwxr-xr-x.  2 ithov   500 4096 6月   8 16:34 模板  drwxr-xr-x.  2 ithov   500 4096 6月   8 16:34 视频  drwxr-xr-x.  2 ithov   500 4096 6月   8 16:34 图片  drwxr-xr-x.  2 ithov   500 4096 6月   8 16:34 文档  drwxr-xr-x.  2 ithov   500 4096 6月   8 16:34 下载  drwxr-xr-x.  2 ithov   500 4096 6月   8 16:34 音乐  drwxr-xr-x.  2 ithov   500 4096 6月   8 16:44 桌面 

为了系统的安全,在此强烈建议用户限制该文件的大小。用户可以编辑/etc/profile文件,修改其中的选项如下:

HISTFILESIZE=30 或 HISTSIZE=30

这样就将记录的命令条数减少到30条

示例:

[root@localhost ithov]# cat /etc/profile

# /etc/profile   # System wide environment and startup programs, for login setup  # Functions and aliases go in /etc/bashrc   # It's NOT a good idea to change this file unless you know what you  # are doing. It's much better to create a custom.sh shell script in  # /etc/profile.d/ to make custom changes to your environment, as this  # will prevent the need for merging in future updates.   pathmunge () {      case ":${PATH}:" in          *:"$1":*)              ;;          *)              if [ "$2" = "after" ] ; then                  PATH=$PATH:$1              else                  PATH=$1:$PATH              fi      esac  }    if [ -x /usr/bin/id ]; then      if [ -z "$EUID" ]; then          # ksh workaround          EUID=`id -u`          UID=`id -ru`      fi      USER="`id -un`"     LOGNAME=$USER      MAIL="/var/spool/mail/$USER" fi   # Path manipulation  if [ "$EUID" = "0" ]; then      pathmunge /sbin      pathmunge /usr/sbin      pathmunge /usr/local/sbin  else      pathmunge /usr/local/sbin after      pathmunge /usr/sbin after      pathmunge /sbin after  fi   HOSTNAME=`/bin/hostname 2>/dev/null`  HISTSIZE=1000 if [ "$HISTCONTROL" = "ignorespace" ] ; then      
export HISTCONTROL=ignoreboth else      export HISTCONTROL=ignoredups fi   export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL   # By default, we want umask to get set. This sets it for login shell  # Current threshold for system reserved uid/gids is 200  # You could check uidgid reservation validity in  # /usr/share/doc/setup-*/uidgid file  if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then      umask 002  else      umask 022  fi   for i in /etc/profile.d/*.sh ; do      if [ -r "$i" ]; then          if [ "${-#*i}" != "$-" ]; then              . "$i"          else              . "$i" >/dev/null 2>&1          fi      fi  done   unset i  unset -f pathmunge

时间: 2024-07-31 22:23:04

Linux中如何限制Shell命令记录的数量的相关文章

Linux下常用的shell命令记录

本文章记录我在linux系统下常用或有用的系统级命令,包括软硬件查看.修改命令,有CPU.内存.硬盘.网络.系统管理等命令.但本文不打算介绍生僻命令,也不介绍各个linux发行版下的特有命令,且以后会持续更新. 说明,我是在一个Centos 6.4 64位的虚拟机系统进行测试.本文介绍的命令都会在此Centos下运行验证(也有部分命令会在我的suse/ubuntu系统里测试的,会做特明说明),但运行结果就不再列出了. 硬件篇 CPU相关 lscpu                   #查看的是

Linux中rsh远程shell命令的使用技巧解析

  rsh有两种使用模式: rsh $host : 远程登录,启动交互式进程. rsh $host $command :远程执行命令,并显示输出. rsh hosthostcommand rsh $host $command的作用是: 1.在远程机器上执行命令$command 2.通过网络连接(socket)重定向当前进程和远端进程的标准输入和标准输出 3.远端rsh进程在远端进程结束后结束 4.本地rsh进程读取远端进程的标准输出直到结束(EOF) 深刻理解这个执行过程有助于理解各种"奇怪&q

如何在linux上分享你shell命令的输出

如何在linux上分享你shell命令的输出 前段时间我发布了一篇关于shelr.tv这个网站的文章,它提供一个服务允许你从网站上直接分享你的终端记录. 现在shelr.tv这个网站似乎关闭了,然后我四处寻找是否有类似的网站,于是我发现了commands.com. 从它的主页上来看,它的服务和其他网站提供的服务是类似的,因此让我们来测试它. 步骤 1 – 在网站上注册 只需要注册一个新的 用户名/密码,或者直接使用你的github账户快速登录. 步骤 2 – 下载安装monitor程序 Moni

【日常小记】linux中强大且常用命令:find、grep

在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 find命令的常用选项及实例 find与xargs grep命令 grep命令的一般形式 grep正则表达式元字符集(基本集) grep命令的常用选项及实例 1.find命令 find命令是一个无处不在命令,是linux中最有用的命令之一.find命令用于:在一个目录(及子目录)中搜索文件,你可以

Linux中强大且常用命令:find、grep

在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 find命令的常用选项及实例 find与xargs grep命令 grep命令的一般形式 grep正则表达式元字符集(基本集) grep命令的常用选项及实例 1.find命令 find命令是一个无处不在命令,是linux中最有用的命令之一.find命令用于:在一个目录(及子目录)中搜索文件,你可以

linux 中强大且常用命令:find、grep

在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,它哥俩可以算是必会的linux命令,我几乎每天都要用到它们.   1.find命令 find命令是一个无处不在命令,是linux中最有用的命令之一.find命令用于:在一个目录(及子目录)中搜索文件,你可以指定一些匹配条件,如按文件名.文件类型.用户甚至是时间戳查找文件.下面就通过实例来体验下find命令的强大. 1.1.find命令的一般形式 man文档中给出的find命令的一般形式为: find [-H]

利用ganymed-ssh2远程执行其它Linux机器上的shell命令

实际应用中,有时候需要从web管理界面上,远程去启动其它linux主机上的程序,利用ssh协议可以方便的满足这一需求.事实上hadoop架构中,从nn上启动dn时,就是利用了免密码ssh登录.ganymed-ssh2是一个实现了ssh协议的开源项目,项目地址为:http://ganymed-ssh-2.googlecode.com/ (下载源码要翻强,众所周知的原因),如果只是使用的话,pom.xml添加以下依赖项就行了: 1 <dependency> 2 <groupId>ch.

【日常小记】linux中强大且常用命令:find、grep【转】

转自:http://www.cnblogs.com/skynet/archive/2010/12/25/1916873.html 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 find命令的常用选项及实例 find与xargs grep命令 grep命令的一般形式 grep正则表达式元字符集(基本集) grep命令的常用选项及实例 1.fin

android开发-在APK中通过adb shell命令启动UIautomator失败

问题描述 在APK中通过adb shell命令启动UIautomator失败 源码如下: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); Button stButton = (Button)findViewById(R.id.start_test); stButton.setOnClick