{"id":104,"date":"2011-09-29T00:30:30","date_gmt":"2011-09-28T19:00:30","guid":{"rendered":"http:\/\/askadmin.com\/?p=104"},"modified":"2015-04-07T14:32:13","modified_gmt":"2015-04-07T09:02:13","slug":"send-linux-command-history-to-mail-automatically","status":"publish","type":"post","link":"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/","title":{"rendered":"Receive Linux command history to mail automatically"},"content":{"rendered":"<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>&nbsp;<\/p>\n<p>Basically i am a tech-enthusiastic.<\/p>\n<p>When we seek for any support, Linux admins used to connect and work remotely on our servers which are hosted in a remote location.<\/p>\n<p>I was so interested to know how they fix the issues , So i came up with this idea of getting the history of terminal commands they typed in to terminal into my mailbox automatically.<\/p>\n<p>I tried this small script and it worked well.<\/p>\n<p>I first created a script which will send a mail with the history contents.<\/p>\n<p>#<em><span style=\"color: #99cc00;\">cd \/tmp<\/span><\/em><\/p>\n<p>#<em><span style=\"color: #99cc00;\">vi hist.sh<\/span><\/em><\/p>\n<p>and typed the below lines.<\/p>\n<p><span style=\"color: #99cc00;\"><em>#!\/bin\/bash<br \/>\nNOW=$(date)<br \/>\nmail -s &#8220;Log $HOSTNAME @ $NOW&#8221; balusreekanth@gmail.com &lt;$HOME\/.bash_history<\/em><\/span><\/p>\n<p>entered <em><span style=\"color: #99cc00;\">!wq<\/span><\/em> to save and quit from vi editor and to make it executable<\/p>\n<p><span style=\"color: #99cc00;\"><span style=\"color: #000000;\">#<\/span>chmod -x hist.sh<\/span><\/p>\n<p>Now the script is ready to send email with the bash history.<\/p>\n<p>We can test it by executing the script #<em><span style=\"color: #99cc00;\">.\/hist.sh<\/span><\/em><\/p>\n<p>Now We can add the script to crontab and schedule it to send email periodically .<\/p>\n<p>I wanted make it to send the command history automatically whenever\u00a0 the .history file is modified whichmeans ,<\/p>\n<p>whenever someone connected to terminal ,I should receive an mail with a copy of the terminal commands history.<\/p>\n<p>To achive this I used incrontab . Incrontab is just like cron scheduler , but with additional capabilities<\/p>\n<p>-which allows us to configure like file ,directory watch etc.,.<\/p>\n<p>&nbsp;<\/p>\n<p>I downloaded incron rpm from <a href=\"http:\/\/pkgs.repoforge.org\/incron\/_buildlogs\/\">http:\/\/pkgs.repoforge.org\/incron\/_buildlogs\/<\/a> and<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/screen-shot-2011-09-29-at-5-09-21-am\/\" rel=\"attachment wp-att-129\"><img loading=\"lazy\" class=\"alignleft size-full wp-image-129\" title=\"Screen shot 2011-09-29 at 5.09.21 AM\" src=\"https:\/\/askadmin.com\/aa\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-5.09.21-AM.png\" alt=\"\" width=\"543\" height=\"194\" srcset=\"https:\/\/askadmin.com\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-5.09.21-AM.png 543w, https:\/\/askadmin.com\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-5.09.21-AM-500x178.png 500w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>After adding root user to <em><span style=\"color: #99cc00;\">\/etc\/incron.allow<\/span><\/em> , I typed <span style=\"color: #99cc00;\"><em>incrontab -l<\/em><\/span>\u00a0 which returnded <span style=\"color: #ff0000;\">&#8220;no table for root&#8217;<\/span>\u00a0 ,because I dont have any jobs configured yet.<\/p>\n<p>I typed <span style=\"color: #99cc00;\"><em>#incrontab -e<\/em><\/span> to create a user table and added this line <span style=\"color: #99cc00;\"><em>~\/.bash_history IN_MODIFY \/tmp\/.\/hist.sh<\/em><\/span><\/p>\n<p>( assuming we have the script file in temp directory) .<\/p>\n<p>&nbsp;<\/p>\n<p>Voila thats all ! I just received an email<\/p>\n<p><a href=\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/screen-shot-2011-09-29-at-6-11-35-am\/\" rel=\"attachment wp-att-144\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-144\" title=\"Screen shot 2011-09-29 at 6.11.35 AM\" src=\"https:\/\/askadmin.com\/aa\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-6.11.35-AM.png\" alt=\"\" width=\"785\" height=\"178\" srcset=\"https:\/\/askadmin.com\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-6.11.35-AM.png 785w, https:\/\/askadmin.com\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-6.11.35-AM-500x113.png 500w\" sizes=\"(max-width: 785px) 100vw, 785px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; &nbsp; Basically i am a tech-enthusiastic. When we seek for any support, Linux admins used to connect and work remotely on our servers which are hosted in a remote location. I was so interested to know how they fix the issues , So i came up with this idea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[7],"tags":[32,34,33,35,121,31,37,36],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>how to send linux command history to email<\/title>\n<meta name=\"description\" content=\"send linux command history to email\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"how to send linux command history to email\" \/>\n<meta property=\"og:description\" content=\"send linux command history to email\" \/>\n<meta property=\"og:url\" content=\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/\" \/>\n<meta property=\"og:site_name\" content=\"Ask Admin - A Lifelong Learner\" \/>\n<meta property=\"article:published_time\" content=\"2011-09-28T19:00:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-04-07T09:02:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/askadmin.com\/aa\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-5.09.21-AM.png\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"balu sreekanth\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/askadmin.com\/aa\/#website\",\"url\":\"https:\/\/askadmin.com\/aa\/\",\"name\":\"Ask Admin - A Lifelong Learner\",\"description\":\"Mobile App Development | IP Telephony | Linux\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/askadmin.com\/aa\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/askadmin.com\/aa\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-5.09.21-AM.png\",\"contentUrl\":\"https:\/\/askadmin.com\/aa\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-5.09.21-AM.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#webpage\",\"url\":\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/\",\"name\":\"how to send linux command history to email\",\"isPartOf\":{\"@id\":\"https:\/\/askadmin.com\/aa\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#primaryimage\"},\"datePublished\":\"2011-09-28T19:00:30+00:00\",\"dateModified\":\"2015-04-07T09:02:13+00:00\",\"author\":{\"@id\":\"https:\/\/askadmin.com\/aa\/#\/schema\/person\/e1ff95d4a91ea001e25af3273533a345\"},\"description\":\"send linux command history to email\",\"breadcrumb\":{\"@id\":\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/askadmin.com\/aa\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Receive Linux command history to mail automatically\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/askadmin.com\/aa\/#\/schema\/person\/e1ff95d4a91ea001e25af3273533a345\",\"name\":\"balu sreekanth\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/askadmin.com\/aa\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6aebf95935573dc70f536f1003a2fb8c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6aebf95935573dc70f536f1003a2fb8c?s=96&d=mm&r=g\",\"caption\":\"balu sreekanth\"},\"sameAs\":[\"http:\/\/askadmin.com\"],\"url\":\"https:\/\/askadmin.com\/aa\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"how to send linux command history to email","description":"send linux command history to email","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/","og_locale":"en_US","og_type":"article","og_title":"how to send linux command history to email","og_description":"send linux command history to email","og_url":"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/","og_site_name":"Ask Admin - A Lifelong Learner","article_published_time":"2011-09-28T19:00:30+00:00","article_modified_time":"2015-04-07T09:02:13+00:00","og_image":[{"url":"https:\/\/askadmin.com\/aa\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-5.09.21-AM.png"}],"twitter_misc":{"Written by":"balu sreekanth","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/askadmin.com\/aa\/#website","url":"https:\/\/askadmin.com\/aa\/","name":"Ask Admin - A Lifelong Learner","description":"Mobile App Development | IP Telephony | Linux","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/askadmin.com\/aa\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#primaryimage","inLanguage":"en-US","url":"https:\/\/askadmin.com\/aa\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-5.09.21-AM.png","contentUrl":"https:\/\/askadmin.com\/aa\/aa\/wp-content\/uploads\/2011\/09\/Screen-shot-2011-09-29-at-5.09.21-AM.png"},{"@type":"WebPage","@id":"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#webpage","url":"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/","name":"how to send linux command history to email","isPartOf":{"@id":"https:\/\/askadmin.com\/aa\/#website"},"primaryImageOfPage":{"@id":"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#primaryimage"},"datePublished":"2011-09-28T19:00:30+00:00","dateModified":"2015-04-07T09:02:13+00:00","author":{"@id":"https:\/\/askadmin.com\/aa\/#\/schema\/person\/e1ff95d4a91ea001e25af3273533a345"},"description":"send linux command history to email","breadcrumb":{"@id":"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/askadmin.com\/aa\/send-linux-command-history-to-mail-automatically\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/askadmin.com\/aa\/"},{"@type":"ListItem","position":2,"name":"Receive Linux command history to mail automatically"}]},{"@type":"Person","@id":"https:\/\/askadmin.com\/aa\/#\/schema\/person\/e1ff95d4a91ea001e25af3273533a345","name":"balu sreekanth","image":{"@type":"ImageObject","@id":"https:\/\/askadmin.com\/aa\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/6aebf95935573dc70f536f1003a2fb8c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6aebf95935573dc70f536f1003a2fb8c?s=96&d=mm&r=g","caption":"balu sreekanth"},"sameAs":["http:\/\/askadmin.com"],"url":"https:\/\/askadmin.com\/aa\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/posts\/104"}],"collection":[{"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":29,"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":507,"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/posts\/104\/revisions\/507"}],"wp:attachment":[{"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/askadmin.com\/aa\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}