<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[实践OK] history命令追查登录的用户和时间，使用HISTTIMEFORMAT 显示history命令的时间戳。-bash: HISTTIMEFORMAT: readonly variable]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Tue, 27 Jun 2017 15:02:51 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	echo $HISTFILE<br/>/root/.bash_history<br/><br/>less /root/.bash_history<br/>-----------------------------------------------------------------------------------------------------------------------------------------<br/>vi /etc/profile <br/><br/>export HISTTIMEFORMAT=&quot;%F %T `who -u am i 2&gt;/dev/null&#124; awk &#039;&#123;print $NF&#125;&#039;&#124;sed -e &#039;s/[()]//g&#039;` &quot;<br/>sudo su -<br/>-bash: HISTTIMEFORMAT: readonly variable<br/><br/>cd /etc/profile.d/<br/>grep -r &quot;HISTTIMEFORMAT&quot; ./<br/>./hq.sh:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;export HISTTIMEFORMAT=&quot;%Y.%m.%d-%H:%M:%S &quot;<br/>./hq.sh:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readonly HISTTIMEFORMAT HISTSIZE HISTFILE ORIGNALUSER USERIP IPLIST PROMPT_COMMAND<br/><br/>在这儿原来 vi /etc/profile.d/hq.sh：<br/><br/>#EXPORTS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;export HISTTIMEFORMAT=&quot;%Y.%m.%d-%H:%M:%S &quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;export HISTSIZE=4096<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;export HISTFILE=&quot;$histDir/$&#123;username&#125;/$&#123;userIP&#125;.hist.$DT&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;export ORIGNALUSER=`$ECHO $username`<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;export USERIP=`$ECHO $userIP`<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;export IPLIST=$(/sbin/ip ad sh &#124;$GREP inet &#124;$GREP -v inet6&#124;awk -F&#039; &#039; &#039;&#123;print $2&#125;&#039;&#124;awk -F&#039;/&#039; &#039;&#123;print $1&#125;&#039; &#124;<br/>$GREP -v &#039;127.0.0.1&#039;&#124;tr -s &quot;&#92;n&quot; &quot;,&quot; &#124;sed &quot;s/&#124;$//g&quot;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;export PROMPT_COMMAND=&#039;&#123; ec=$?; read seq time cmd &lt; &lt;(history 1); [ -z &quot;$time&quot; ] &#124;&#124; &#123; &#039;$ECHO&#039; &quot;$time&#124;$IPLI<br/>ST&#124;$&#123;ORIGNALUSER&#125;,$(whoami)&#124;$(pwd)&#124;$cmd&#124;$ec&#124;$USERIP&quot; &gt;&gt; &#039;$histDir&#039;/`&#039;$DATE&#039; +%Y.%m.%d`_history; &#039;$LOGGER&#039; -t &quot;audi<br/>t.hist&quot; -p 7 &quot;$USERIP&#124;$&#123;ORIGNALUSER&#125;&#124;$(whoami)&#124;$(pwd)&#124;$ec&#124;$cmd&quot;; &#125; &#125;&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readonly HISTTIMEFORMAT HISTSIZE HISTFILE ORIGNALUSER USERIP IPLIST PROMPT_COMMAND<br/><br/><br/>http://awesomeops.blogspot.com/2014/10/there-are-lot-of-possibilities-to-track.html<br/><br/>——————<br/>后面遇到这种类似的，直接修改，这估计是开发加的：/etc/profile <br/>把里的：<br/><textarea name="code" class="php" rows="15" cols="100">
export HISTTIMEFORMAT=&quot;%F %T `who -u am i 2&gt;/dev/null&#124; awk &#039;&#123;print $NF&#125;&#039;&#124;sed -e &#039;s/[()]//g&#039;` &quot; 
</textarea><br/>注释掉。<br/>这里也有，文件在： /etc/profile.d/history.sh&nbsp;&nbsp;#黄冶给想把命令加到ELK里，又不妨碍安全，于是得：rpm -qf `which chattr` ，yum install e2fsprogs -y ， chattr -i&nbsp;&nbsp;/etc/profile.d/hq.sh&nbsp;&nbsp;，把那个i的权限给去掉。<br/><textarea name="code" class="php" rows="15" cols="100">
function log2file
&#123;
#export HISTTIMEFORMAT=&quot;[%Y-%m-%d %H:%M:%S] &quot;
#export PROMPT_COMMAND=&#039;&#92;
#&nbsp;&nbsp;if [ -z &quot;$OLD_PWD&quot; ];then
#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;export OLD_PWD=$(pwd);
#&nbsp;&nbsp;fi;
#&nbsp;&nbsp;if [ ! -z &quot;$LAST_CMD&quot; ] &amp;&amp; [ &quot;$(history 1)&quot; != &quot;$LAST_CMD&quot; ]; then
#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&nbsp;`whoami`_shell_cmd [$IPLIST]&nbsp;&nbsp;&quot;[$OLD_PWD]$(history 1)&quot; &gt;&gt;/data/logs/.syshistory/bash_history.log;
#&nbsp;&nbsp;fi;
#export LAST_CMD=&quot;$(history 1)&quot;;
#export OLD_PWD=$(pwd);&#039;
&#125;
</textarea><br/><br/>为何出现readonly的原因：<br/><br/>对于只读变量而言，若用户对其值进行修改，则会立即报错。例如，使用该指令定义一个只读变量&quot;test&quot;，并且将其值初始化为&quot;ok&quot;，输入如下命令：<br/>readonly test=&#039;ok&#039;<br/>那么当用户直接修改该只读变量时，就会被报错，如下所示：<br/>[root@localhost ~]# test=&#039;my&#039; #试图修改只读变量的值 -bash: test: readonly variable<br/>当用户试图修改只读变量的值时，会被提示该变量为只读变量。<br/><br/><br/>实践：<br/>readonly test=&#039;ok&#039;&nbsp;&nbsp;<br/>test=&#039;my&#039;<br/>-bash: test: 只读变量<br/><br/><br/><br/>Linux查看History记录加时间，这个对于系统管理员还是很有帮助的，原因不解释，你懂得！<br/>ora11g$ history&nbsp;&nbsp;&#124;&nbsp;&nbsp;more&nbsp;&nbsp;<br/>1&nbsp;&nbsp;&nbsp;&nbsp;rlwrap sqlplus / as sysdba<br/>2&nbsp;&nbsp;&nbsp;&nbsp;rlwrap sqlplus / as sysdba<br/>3&nbsp;&nbsp;&nbsp;&nbsp;date<br/>4&nbsp;&nbsp;&nbsp;&nbsp;rlwrap sqlplus / as sysdba<br/>&nbsp;&nbsp;但是这里只显示了命令，并没有显示执行命令的时间，因为保存历史命令的~/.bash_history里并没有保存时间。&nbsp;&nbsp;<br/>通过设置环境变量 export HISTTIMEFORMAT=&quot;%F %T `whoami` &quot; 给history加上时间戳&nbsp;&nbsp;<br/>ora11g$ export HISTTIMEFORMAT=&quot;%F %T `whoami` &quot;&nbsp;&nbsp;<br/>ora11g$ history <br/>1&nbsp;&nbsp;2014-03-25 21:29:53 oracle rlwrap sqlplus / as sysdba<br/>2&nbsp;&nbsp;2014-03-25 21:29:59 oracle rlwrap sqlplus / as sysdba&nbsp;&nbsp; <br/>3 2014-03-25 21:31:23 oracle date<br/>4 2014-03-25 21:31:43oracle rlwrap sqlplus / as sysdba<br/>可以看到，历史命令的时间,用户已经加上了，但是.bash_history里并没有加上这个时间。<br/>其实这个时间记录是保存在当前shell进程内存里的，如果你logout并且重新登录的话会发现你上次登录时执行的那些命令的时间戳都为同一个值，即当时logout时的时间。&nbsp;&nbsp;<br/><br/>尽管如此，对于加上screen的bash来说，这个时间戳仍然可以长时间有效的，毕竟只要你的server不重启，screen就不会退出，因而这些时间就能长时间保留。<br/>你在root用户下也可以使用echo &#039;export HISTTIMEFORMAT=&quot;%F %T `whoami` &quot;&#039; &gt;&gt; /etc/profile 然后source一下就OK <br/><br/><br/>===<br/>当你从命令行执行 history 命令后，通常只会显示已执行命令的序号和命令本身。如果你想要查看命令历史的时间戳，那么可以执行： <br/><br/><br/># export HISTTIMEFORMAT=&#039;%F %T &#039; <br/># history &#124; more <br/>1 2008-08-05 19:02:39 service network restart <br/>2 2008-08-05 19:02:39 exit <br/>3 2008-08-05 19:02:39 id <br/>4 2008-08-05 19:02:39 cat /etc/redhat-release <br/><br/>注意：这个功能只能用在当 HISTTIMEFORMAT 这个环境变量被设置之后，之后的那些新执行的 bash 命令才会被打上正确的时间戳。在此之前的所有命令，都将会显示成设置 HISTTIMEFORMAT 变量的时间。<br/><br/>来自：<br/>http://blog.csdn.net/needle2/article/details/7490583<br/>http://blog.itpub.net/27144762/viewspace-1129567/
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK] history命令追查登录的用户和时间，使用HISTTIMEFORMAT 显示history命令的时间戳。-bash: HISTTIMEFORMAT: readonly variable]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>