Linux在history上添加时间和用户等参数的方法
时间:2023-08-31 11:44:15
众所周知,Linux系统下可以使用history命令来查看指令的历史记录。为了提高history命令的效率,还可以在history命令加上一些参数,比如时间、用户和IP等等参数。那么如何在history上添加这些参数呢?想必很多用户都想知道,下面跟随小编一起来看看Linux在history上添加时间和用户等参数的方法。
Linux系统
1.设置显示时间和用户:
echo ‘export HISTTIMEFORMAT=“%F %T `whoami` ”’ 》》 /etc/profile
执行source生效:
source /etc/profile
查看结果:
history
1852 2016-01-11 16:24:52 xubo hs
1853 2016-01-11 16:24:53 xubo ls
1854 2016-01-11 16:24:56 xubo history
1855 2016-01-11 16:25:02 xubo history --help
1856 2016-01-11 16:26:35 xubo vi /etc/profile
1857 2016-01-11 16:27:27 xubo echo ‘export HISTTIMEFORMAT=“%F %T `whoami` ”’ 》》 /etc/profile
1858 2016-01-11 16:27:29 xubo vi /etc/profile
1859 2016-01-11 16:27:42 xubo source /etc/profile
1860 2016-01-11 16:27:46 xubo history
1861 2016-01-11 16:27:59 xubo ls
1862 2016-01-11 16:28:01 xubo history
1863 2016-01-11 16:31:00 xubo clear
1864 2016-01-11 16:31:02 xubo history
2.只看到自己的操作记录(时间,用户,ip名):
USER_IP=`who -u am i 2》/dev/null| awk ‘{print $NF}’|sed -e ‘s/[()]//g’`
xport HISTTIMEFORMAT=“[%F %T][`whoami`][${USER_IP}] ”
执行source生效:
source /etc/profile
查看结果:
hadoop@Master:~$ history
1002 [2016-01-11 16:47:37][hadoop][host-e-150.ustcsz.edu.cn] cd 。。/
1003 [2016-01-11 16:47:37][hadoop][host-e-150.ustcsz.edu.cn] du -h --max-depth=1
1004 [2016-01-11 16:47:37][hadoop][host-e-150.ustcsz.edu.cn] du -h --max-depth=2
1005 [2016-01-11 16:47:37][hadoop][host-e-150.ustcsz.edu.cn] sudo find /* -name pokec-relationships
1006 [2016-01-11 16:47:37][hadoop][host-e-150.ustcsz.edu.cn] sudo find /* -name pokec
1007 [2016-01-11 16:47:37][hadoop][host-e-150.ustcsz.edu.cn] sudo find /* -name kddcup
1008 [2016-01-11 16:47:37][hadoop][host-e-150.ustcsz.edu.cn] sudo find /* -name kddcup*
1009 [2016-01-11 16:47:37][hadoop][host-e-150.ustcsz.edu.cn] sudo find /* -name *kddcup*
3.将history记录到log:
#export HISTTIMEFORMAT=“%F %T $USER_IP:`whoami` ”
#export HISTTIMEFORMAT=“%F %T $USER_IP:`whoami` ”
USER_IP=`who -u am i 2》/dev/null| awk ‘{print $NF}’|sed -e ‘s/[()]//g’`
export HISTTIMEFORMAT=“[%F %T][`whoami`][${USER_IP}] ”
#history
LOGIP=`who -u am i 2》/dev/null| awk ‘{print $NF}’|sed -e ‘s/[()]//g’`
LOG_DIR=/var/log/history
if [ -z $LOGIP ]
then
LOGIP=`hostname`
fi
if [ ! -d $LOG_DIR ]
then
mkdir -p $LOG_DIR
chmod 777 $LOG_DIR
fi
if [ ! -d $LOG_DIR/${LOGNAME} ]
then
mkdir -p $LOG_DIR/${LOGNAME}
chmod 777 $LOG_DIR/${LOGNAME}
fi
export HISTSIZE=4096
LOGTM=`date +“%Y%m%d_%H%M%S”`
export HISTFILE=“$LOG_DIR/${LOGNAME}/${LOGIP}-$LOGTM”
chmod 777 $LOG_DIR/${LOGNAME}/*-* 2》/dev/null
运行结果:
root@xubo:/media/xubo/My Passport/gene_data/1000genomes# ls /var/log/history/root/
host-e-150.ustcsz.edu.cn-20160111_171845 host-e-150.ustcsz.edu.cn-20160111_172719 host-e-213.ustcsz.edu.cn-20160111_202047
root@xubo:/media/xubo/My Passport/gene_data/1000genomes# vi /var/log/history/root/host-e-213.ustcsz.edu.cn-20160111_202047
#1452514808
ls root/
#1452514818
history
#1452514847
source /etc/profile
#1452514848
history
#1452514860
ls root/
#1452514861
ls
#1452514863
cd root/
#1452514864
ls
#1452514866
exit
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
excel 日期加减实例 出差登记表住宿天数和补助计算
![](https://img.aspxhome.com/file/2023/2/a162362_0s.jpg)
Excel使用技巧
小编教您win10显卡驱动怎么更新
![](https://img.aspxhome.com/file/2023/3/a291985_0s.png)
访问工具选的好,效率快人不止~
![](https://img.aspxhome.com/file/2023/8/a209408_0s.jpeg)
网上文档再好,也怕这些Word问题
![](https://img.aspxhome.com/file/2023/2/24152_0s.gif)
Win10专业版C盘满了要怎么处理?
![](https://img.aspxhome.com/file/2023/26/a222393_0s.png)
如何在Word “快速访问工具栏”中添加自定义命令
![](https://img.aspxhome.com/file/2023/5/33905_0s.jpg)
苹果推送 macOS 12 Monterey 首个公测版Beta更新
![](https://img.aspxhome.com/file/2023/3/a205783_0s.png)
Cloud Outliner 2 Pro for Mac(笔记软件)6大功能介绍
![](https://img.aspxhome.com/file/2023/4/a212784_0s.png)
Excel制作“专业符号”工具栏基础教程
WPS表格固定指定行和列
![](https://img.aspxhome.com/file/2023/2/a165312_0s.jpg)
Win10操作中心开关是灰色怎么办?Win10操作中心开关是灰色的解决方法
![](https://img.aspxhome.com/file/2023/5/52375_0s.jpg)
Win10专业版软件打不开闪退怎么办?
![](https://img.aspxhome.com/file/2023/0/50540_0s.png)
XP系统连接提示错误633:调制解调器已在使用的解决方法图文教程
![](https://img.aspxhome.com/file/2023/29/a260913_0s.jpg)
WPS 快速合并多个文件实现教程
![](https://img.aspxhome.com/file/2023/2/a186712_0s.gif)
谷歌浏览器如何清除浏览器文本框候选项
![](https://img.aspxhome.com/file/2023/1/a275206_0s.jpg)
第一弹怎么收藏资源?第一弹收藏资源的方法
![](https://img.aspxhome.com/file/2023/8/a333376_0s.jpg)
Win10系统关闭CPU降频的方法
![](https://img.aspxhome.com/file/2023/1/a282411_0s.jpg)
WPS表格无法粘贴信息怎么办
Win10系统SysMain磁盘占用率高怎么办?SysMain磁盘占用率高解决方法
![](https://img.aspxhome.com/file/2023/25/a217504_0s.jpg)