{"id":201626,"date":"2020-10-10T09:17:48","date_gmt":"2020-10-10T01:17:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=201626"},"modified":"2020-09-27T09:22:44","modified_gmt":"2020-09-27T01:22:44","slug":"linux-history","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-history.html","title":{"rendered":"\u5728 Linux \u4e0a\u8c03\u6574\u547d\u4ee4\u5386\u53f2"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728 Linux \u7cfb\u7edf\u4e0a\uff0cbash shell \u7684 history \u547d\u4ee4\u53ef\u4ee5\u65b9\u4fbf\u5730\u56de\u987e\u548c\u91cd\u7528\u547d\u4ee4\uff0c\u4f46\u662f\u4f60\u8981\u63a7\u5236\u5b83\u8bb0\u4f4f\u591a\u5c11\uff0c\u5fd8\u8bb0\u591a\u5c11\uff0c\u6709\u5f88\u591a\u4e8b\u60c5\u8981\u505a\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

Linux \u7cfb\u7edf\u4e2d\u7684 bash history \u547d\u4ee4\u6709\u52a9\u4e8e\u8bb0\u4f4f\u4f60\u4ee5\u524d\u8fd0\u884c\u8fc7\u7684\u547d\u4ee4\uff0c\u5e76\u91cd\u590d\u8fd9\u4e9b\u547d\u4ee4\uff0c\u800c\u4e0d\u5fc5\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n

\u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u4f60\u80af\u5b9a\u4f1a\u5f88\u9ad8\u5174\u4e0d\u7528\u7ffb\u9605\u5341\u51e0\u9875\u7684\u624b\u518c\uff0c\u6bcf\u8fc7\u4e00\u4f1a\u518d\u6b21\u5217\u51fa\u4f60\u7684\u6587\u4ef6\uff0c\u800c\u662f\u901a\u8fc7\u8f93\u5165 history \u67e5\u770b\u4ee5\u524d\u8fd0\u884c\u7684\u547d\u4ee4\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u8ba9 history \u547d\u4ee4\u8bb0\u4f4f\u4f60\u5e0c\u671b\u5b83\u8bb0\u4f4f\u7684\u5185\u5bb9\uff0c\u5e76\u5fd8\u8bb0\u90a3\u4e9b\u53ef\u80fd\u6ca1\u6709\u4ec0\u4e48\u201c\u5386\u53f2\u4ef7\u503c\u201d\u7684\u547d\u4ee4\u3002<\/p>\n

\u67e5\u770b\u4f60\u7684\u547d\u4ee4\u5386\u53f2<\/strong><\/div>\n

\u8981\u67e5\u770b\u4ee5\u524d\u8fd0\u884c\u8fc7\u7684\u547d\u4ee4\uff0c\u4f60\u53ea\u9700\u8f93\u5165 history\u3002\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u4e00\u957f\u4e32\u547d\u4ee4\u3002\u8bb0\u5fc6\u7684\u547d\u4ee4\u6570\u91cf\u53d6\u51b3\u4e8e\u5728 ~\/.bashrc \u6587\u4ef6\u4e2d\u8bbe\u7f6e\u7684\u540d\u4e3a $HISTSIZE \u7684\u73af\u5883\u53d8\u91cf\uff0c\u4f46\u662f\u5982\u679c\u4f60\u60f3\u4fdd\u5b58\u66f4\u591a\u6216\u66f4\u5c11\u7684\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u8981\u6539\u53d8\u8fd9\u4e2a\u8bbe\u7f6e\u3002<\/p>\n

\u8981\u67e5\u770b\u5386\u53f2\u8bb0\u5f55\uff0c\u8bf7\u4f7f\u7528 history \u547d\u4ee4\uff1a<\/p>\n

$ history\r\n209 uname -v\r\n210 date\r\n211 man chage\r\n...<\/pre>\n

\u8981\u67e5\u770b\u5c06\u663e\u793a\u7684\u6700\u5927\u547d\u4ee4\u6570\u91cf\uff1a<\/p>\n

$ echo $HISTSIZE\r\n500<\/pre>\n

\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u8fd9\u6837\u7684\u547d\u4ee4\u6765\u6539\u53d8 $HISTSIZE \u5e76\u4f7f\u4e4b\u6c38\u4e45\u5316\uff1a<\/p>\n

$ export HISTSIZE=1000\r\n$ echo \"HISTSIZE=1000\" >> ~\/.bashrc<\/pre>\n

\u5728\u4e3a\u4f60\u4fdd\u7559\u591a\u5c11\u5386\u53f2\u8bb0\u5f55\u548c\u5f53\u4f60\u8f93\u5165 history \u65f6\u663e\u793a\u591a\u5c11\u5386\u53f2\u8bb0\u5f55\u4e4b\u95f4\u4e5f\u6709\u533a\u522b\u3002$HISTSIZE \u53d8\u91cf\u63a7\u5236\u663e\u793a\u591a\u5c11\u5386\u53f2\u8bb0\u5f55\uff0c\u800c $HISTFILESIZE \u53d8\u91cf\u63a7\u5236\u5728\u4f60\u7684 .bash_history \u6587\u4ef6\u4e2d\u4fdd\u7559\u591a\u5c11\u547d\u4ee4\u3002<\/p>\n

$ echo $HISTSIZE\r\n1000\r\n$ echo $HISTFILESIZE\r\n2000<\/pre>\n

\u4f60\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u5386\u53f2\u6587\u4ef6\u4e2d\u7684\u884c\u6570\u6765\u9a8c\u8bc1\u7b2c\u4e8c\u4e2a\u53d8\u91cf\uff1a<\/p>\n

$ wc -l .bash_history\r\n2000 .bash_history<\/pre>\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u767b\u5f55\u4f1a\u8bdd\u4e2d\u8f93\u5165\u7684\u547d\u4ee4\u5728\u6ce8\u9500\u524d\u4e0d\u4f1a\u88ab\u6dfb\u52a0\u5230\u4f60\u7684 .bash_history \u6587\u4ef6\u4e2d\uff0c\u5c3d\u7ba1\u5b83\u4eec\u4f1a\u7acb\u5373\u663e\u793a\u5728 history \u547d\u4ee4\u8f93\u51fa\u4e2d\u3002<\/p>\n

\u4f7f\u7528\u5386\u53f2<\/strong><\/div>\n

\u6709\u4e09\u79cd\u65b9\u6cd5\u53ef\u4ee5\u91cd\u53d1\u4f60\u5728 history \u4e2d\u53d1\u73b0\u7684\u547d\u4ee4\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5f53\u4f60\u60f3\u91cd\u7528\u7684\u547d\u4ee4\u662f\u6700\u8fd1\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u901a\u5e38\u662f\u8f93\u5165\u4e00\u4e2a ! \u540e\u9762\u8ddf\u4e0a\u547d\u4ee4\u4e2d\u8db3\u591f\u591a\u7684\u9996\u5b57\u6bcd\u6765\u552f\u4e00\u5730\u8bc6\u522b\u5b83\u3002<\/p>\n

$ !u\r\nuname -v\r\n#37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020<\/pre>\n

\u53e6\u4e00\u79cd\u7b80\u5355\u7684\u91cd\u590d\u547d\u4ee4\u7684\u65b9\u6cd5\u662f\uff0c\u53ea\u9700\u6309\u4e0a\u7bad\u5934\u952e\uff0c\u76f4\u5230\u663e\u793a\u4e86\u8be5\u547d\u4ee4\uff0c\u7136\u540e\u6309\u56de\u8f66\u952e\u3002<\/p>\n

\u53e6\u5916\uff0c\u5982\u679c\u4f60\u8fd0\u884c history \u547d\u4ee4\uff0c\u5e76\u770b\u5230\u4f60\u60f3\u91cd\u65b0\u8fd0\u884c\u7684\u547d\u4ee4\u88ab\u5217\u51fa\uff0c\u4f60\u53ef\u4ee5\u8f93\u5165\u4e00\u4e2a ! \u540e\u9762\u8ddf\u7740\u547d\u4ee4\u5de6\u8fb9\u663e\u793a\u7684\u5e8f\u53f7\u3002<\/p>\n

$ !209\r\nuname -v\r\n#37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020<\/pre>\n
\u9690\u85cf\u5386\u53f2<\/strong><\/div>\n

\u5982\u679c\u4f60\u60f3\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u505c\u6b62\u8bb0\u5f55\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u547d\u4ee4\uff1a<\/p>\n

$ set +o history<\/pre>\n

\u5f53\u4f60\u8f93\u5165 history \u65f6\uff0c\u4f60\u8f93\u5165\u7684\u547d\u4ee4\u4e0d\u4f1a\u663e\u793a\u51fa\u6765\uff0c\u5f53\u4f60\u9000\u51fa\u4f1a\u8bdd\u6216\u9000\u51fa\u7ec8\u7aef\u65f6\uff0c\u5b83\u4eec\u4e5f\u4e0d\u4f1a\u88ab\u6dfb\u52a0\u5230\u4f60\u7684 .bash_history \u6587\u4ef6\u4e2d\u3002<\/p>\n

\u8981\u53d6\u6d88\u8fd9\u4e2a\u8bbe\u7f6e\uff0c\u4f7f\u7528 set -o history\u3002<\/p>\n

\u8981\u4f7f\u5b83\u6c38\u4e45\u5316\uff0c\u4f60\u53ef\u4ee5\u628a\u5b83\u6dfb\u52a0\u5230\u4f60\u7684 .bashrc \u6587\u4ef6\u4e2d\uff0c\u5c3d\u7ba1\u4e0d\u4f7f\u7528\u547d\u4ee4\u5386\u53f2\u8bb0\u5f55\u901a\u5e38\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u3002<\/p>\n

$ echo 'set +o history' >> ~\/.bashrc<\/pre>\n

\u8981\u6682\u65f6\u6e05\u9664\u5386\u53f2\u8bb0\u5f55\uff0c\u8fd9\u6837\u5728\u8f93\u5165 history \u65f6\u53ea\u663e\u793a\u4e4b\u540e\u8f93\u5165\u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528 history -c\uff08\u6e05\u9664\uff09\u547d\u4ee4\uff1a<\/p>\n

$ history | tail -3\r\n209 uname -v\r\n210 date\r\n211 man chage\r\n$ history -c\r\n$ history\r\n1  history<\/pre>\n

\u6ce8\u610f\uff1a\u5728\u8f93\u5165 history -c \u540e\u8f93\u5165\u7684\u547d\u4ee4\u4e0d\u4f1a\u88ab\u6dfb\u52a0\u5230 .bash_history \u6587\u4ef6\u4e2d\u3002<\/p>\n

\u63a7\u5236\u5386\u53f2<\/strong><\/div>\n

\u8bb8\u591a\u7cfb\u7edf\u4e0a\u7684 history \u547d\u4ee4\u7684\u8bbe\u7f6e\u4f1a\u9ed8\u8ba4\u5305\u62ec\u4e00\u4e2a\u540d\u4e3a $HISTCONTROL \u7684\u53d8\u91cf\uff0c\u4ee5\u786e\u4fdd\u5373\u4f7f\u4f60\u8fde\u7eed\u8fd0\u884c\u540c\u4e00\u547d\u4ee4\u4e03\u6b21\uff0c\u4e5f\u53ea\u4f1a\u88ab\u8bb0\u4f4f\u4e00\u6b21\u3002\u5b83\u8fd8\u53ef\u4ee5\u786e\u4fdd\u4f60\u5728\u9996\u5148\u8f93\u5165\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u683c\u540e\u8ddf\u7740\u7684\u547d\u4ee4\u5c06\u4ece\u4f60\u7684\u547d\u4ee4\u5386\u53f2\u8bb0\u5f55\u4e2d\u5ffd\u7565\u3002<\/p>\n

$ grep HISTCONTROL .bashrc\r\nHISTCONTROL=ignoreboth<\/pre>\n

ignoreboth \u7684\u610f\u601d\u662f\u201c\u5ffd\u7565\u91cd\u590d\u7684\u547d\u4ee4\u548c\u4ee5\u7a7a\u683c\u5f00\u5934\u7684\u547d\u4ee4\u201d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8f93\u5165\u8fd9\u4e9b\u547d\u4ee4\uff1a<\/p>\n

$ echo try this\r\n$ date\r\n$ date\r\n$ date\r\n$   pwd\r\n$ history<\/pre>\n

\u4f60\u7684 history \u547d\u4ee4\u5e94\u8be5\u50cf\u8fd9\u6837\u62a5\u544a\uff1a<\/p>\n

$ history\r\n$ echo try this\r\n$ date\r\n$ history<\/pre>\n

\u8bf7\u6ce8\u610f\uff0c\u8fde\u7eed\u7684 date \u547d\u4ee4\u88ab\u7f29\u51cf\u4e3a\u4e00\u6761\uff0c\u4ee5\u7a7a\u683c\u7f29\u8fdb\u7684\u547d\u4ee4\u88ab\u7701\u7565\u3002<\/p>\n

\u5ffd\u7565\u5386\u53f2<\/strong><\/div>\n

\u8981\u5ffd\u7565\u67d0\u4e9b\u547d\u4ee4\uff0c\u4f7f\u5b83\u4eec\u5728\u4f60\u8f93\u5165 history \u65f6\u4e0d\u4f1a\u51fa\u73b0\uff0c\u4e5f\u4e0d\u4f1a\u88ab\u6dfb\u52a0\u5230\u4f60\u7684 .bash_history \u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 $HISTIGNORE \u8bbe\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n

$ export HISTIGNORE=\u201dhistory:cd:exit:ls:pwd:man\u201d<\/pre>\n

\u8fd9\u4e2a\u8bbe\u7f6e\u5c06\u5bfc\u81f4\u6240\u6709\u7684 history\u3001cd\u3001exit\u3001ls\u3001pwd \u548c man \u547d\u4ee4\u4ece\u4f60\u7684 history \u547d\u4ee4\u7684\u8f93\u51fa\u548c .bash_history \u6587\u4ef6\u4e2d\u88ab\u5ffd\u7565\u3002<\/p>\n

\u5982\u679c\u4f60\u60f3\u628a\u8fd9\u4e2a\u8bbe\u7f6e\u53d8\u6210\u6c38\u4e45\u6027\u7684\uff0c\u4f60\u5fc5\u987b\u628a\u5b83\u6dfb\u52a0\u5230\u4f60\u7684 .bashrc \u6587\u4ef6\u4e2d\u3002<\/p>\n

$ echo 'HISTIGNORE=\"history:cd:exit:ls:pwd:man\"' >> .bashrc<\/pre>\n

\u8fd9\u4e2a\u8bbe\u7f6e\u53ea\u662f\u610f\u5473\u7740\u5f53\u4f60\u56de\u770b\u4ee5\u524d\u8fd0\u884c\u7684\u547d\u4ee4\u65f6\uff0c\u5217\u8868\u4e0d\u4f1a\u88ab\u4f60\u5728\u67e5\u770b\u547d\u4ee4\u5386\u53f2\u8bb0\u5f55\u65f6\u4e0d\u60f3\u770b\u5230\u7684\u547d\u4ee4\u6240\u5e72\u6270\u3002<\/p>\n

\u8bb0\u4f4f\u3001\u5ffd\u7565\u548c\u5fd8\u8bb0\u8fc7\u53bb\u7684\u547d\u4ee4<\/strong><\/div>\n

\u547d\u4ee4\u5386\u53f2\u8bb0\u5f55\u5f88\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u4f4f\u6700\u8fd1\u4f7f\u7528\u8fc7\u7684\u547d\u4ee4\uff0c\u5e76\u63d0\u9192\u4f60\u6700\u8fd1\u6240\u505a\u7684\u66f4\u6539\u3002\u5b83\u8fd8\u53ef\u4ee5\u8ba9\u4f60\u66f4\u5bb9\u6613\u5730\u91cd\u65b0\u8fd0\u884c\u547d\u4ee4\uff0c\u7279\u522b\u662f\u90a3\u4e9b\u6709\u4e00\u4e32\u53c2\u6570\u4f46\u4f60\u4e0d\u4e00\u5b9a\u60f3\u91cd\u65b0\u521b\u5efa\u7684\u547d\u4ee4\u3002\u5b9a\u5236\u4f60\u7684\u5386\u53f2\u8bbe\u7f6e\u53ef\u4ee5\u8ba9\u4f60\u5bf9\u547d\u4ee4\u5386\u53f2\u7684\u4f7f\u7528\u53d8\u5f97\u66f4\u5bb9\u6613\uff0c\u66f4\u6709\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Linux \u7cfb\u7edf\u4e2d\u7684 bash history \u547d\u4ee4\u6709\u52a9\u4e8e\u8bb0\u4f4f\u4f60\u4ee5\u524d\u8fd0\u884c\u8fc7\u7684\u547d\u4ee4\uff0c\u5e76\u91cd\u590d\u8fd9\u4e9b\u547d\u4ee4\uff0c\u800c\u4e0d\u5fc5\u91cd\u65b0\u8f93 […]<\/p>\n","protected":false},"author":1469,"featured_media":201630,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-201626","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1469"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=201626"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201626\/revisions"}],"predecessor-version":[{"id":202042,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201626\/revisions\/202042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/201630"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=201626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=201626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=201626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}