{"id":2828,"date":"2020-03-01T05:45:00","date_gmt":"2020-03-01T00:15:00","guid":{"rendered":"https:\/\/www.24x7serversupport.com\/blog\/?p=2828"},"modified":"2020-02-29T21:29:02","modified_gmt":"2020-02-29T15:59:02","slug":"how-to-monitor-files-on-your-linux-server","status":"publish","type":"post","link":"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/","title":{"rendered":"How To Monitor Files On Your Linux Server"},"content":{"rendered":"\n<p>You will\nsee&nbsp;<strong>one of the\nbasics of security<\/strong>&nbsp;for managing&nbsp;<strong>your web applications<\/strong>&nbsp;.&nbsp;The&nbsp;<strong>monitoring of changes to your files<\/strong>&nbsp;.&nbsp;Indeed\nif you have a site in production the modifications of the source files will be\nfew.&nbsp;And if there is, it will be easy for you to distinguish between your\nwork and a plugin update that you have carried out.&nbsp;You will see&nbsp;<strong>how in 5 minutes to set up this\nmonitoring<\/strong>&nbsp;.&nbsp;You will have basic surveillance, but oh\nhow effective.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What do you need ?<\/h2>\n\n\n\n<p>I reassure\nyou right away&nbsp;<strong>no\nneed to know how to program<\/strong>&nbsp;, or spend hours on your\nterminal.&nbsp;You are going to need an SSH client (&nbsp;<a href=\"http:\/\/www.putty.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">putty<\/a>&nbsp;on Windows), an\ne-mail box to receive alerts and that&#8217;s it!<\/p>\n\n\n\n<p>Yes,\nbecause&nbsp;<strong>this method\nis native to linux distributions so<\/strong>&nbsp;you will not need to\ninstall any additional library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What will surveillance be set up with?<\/h2>\n\n\n\n<p>Before\ngiving you the lines to copy \/ paste I will explain what you are going to\ndo.&nbsp;On your server you will&nbsp;<strong>create\na bash script<\/strong>&nbsp;(which is simply a file with the extension\n.sh).&nbsp;The script will contain&nbsp;<strong>the\nlinux command<\/strong>&nbsp;which will be used to list the modified\nfiles.<\/p>\n\n\n\n<p>Once the\nfile is in place, all you have to do is add a rule to your server for the&nbsp;<strong>script to be launched periodically.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ultra simple version of the script<\/h2>\n\n\n\n<p>To\norganize yourself a bit, I advise you to&nbsp;<strong>create a &#8220;scripts&#8221; directory<\/strong>&nbsp;at\nthe root of root:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir \/root\/scripts<\/pre>\n\n\n\n<p>In this\ndirectory we will create our files which will be a bash script and a txt\nfile.&nbsp;The txt file will be used for sending by e-mail.&nbsp;First, let&#8217;s\ncreate our txt file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">touch \/root\/scripts\/liste-fichiers-modifies.txt<\/pre>\n\n\n\n<p>Here we\nare done then create our script to be able to modify it and insert the\nnecessary commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nano \/root\/scripts\/monitoring_www.sh<\/pre>\n\n\n\n<p>The nano\neditor will open if you are used to using another, it does not matter.&nbsp;You\nwill therefore be able to copy the following lines into the file and save it, I\nwill explain it right after their meanings:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n #\n echo -e \"--------------------------------------- -------------------\n PHP, HTML &amp; JS\n v 2.0\u00a0file monitoring program\n contact: https:\/\/www.kanjian.fr\n -------- -------------------------------------------------- \"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Subject = \"[SRV-001] File monitoring report: php, html, js, conf, htaccess\"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">find \/var\/www\/ -name -ls -o -regex '. *. (php | htaccess | conf | html | js). *' -mtime 0 |\u00a0xargs ls -lah> modified-files-list.txt<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">mail -s \"$ Subject\" \"inazo@example.com\" &lt;modified-files-list.txt<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the main lines:<\/h3>\n\n\n\n<p><strong>#!\/bin\/bash:<br> <\/strong>This line is used by the server to find out which language is used in the script, more precisely which shell it will use.\u00a0I invite you to read\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/fr.wikipedia.org\/wiki\/Shell_Unix\" target=\"_blank\">the Wikipedia page on linux shells<\/a>\u00a0.<\/p>\n\n\n\n<p><strong>Subject\n= &#8220;[SRV-001] File monitoring report: php, html, js, conf, htaccess&#8221;:<\/strong><strong><br>\n<\/strong>This line defines the subject of the\nemail which will be sent as a report at the end of the script.<\/p>\n\n\n\n<p><strong>find \/var\/www\/ -name -ls -o -regex &#8216;. *. (php | htaccess | conf | html | js). *&#8217; -mtime 0 |\u00a0xargs ls -lah> list-files-modified.txt:<\/strong><br> This penultimate line is the most complex.\u00a0The find command will fetch all the files found in the &#8220;\/var\/www\/&#8221; directory as well as these subdirectories.\u00a0The &#8220;-regex&#8221; option of the command will specify that we only want\u00a0<strong>files that include in their names<\/strong>\u00a0one of the following elements:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>.htaccess<\/li><li>.php<\/li><li>.conf<\/li><li>.html<\/li><li>js<\/li><\/ul>\n\n\n\n<p>We then\nspecify with the option &#8220;-mtime 0&#8221; that we want the files whose&nbsp;<strong>modification dates back at the latest\n24 hours ago<\/strong>&nbsp;.&nbsp;The option &#8220;-name&#8221; requests\nthe search on the name of the files, &#8220;-ls&#8221; to escape certain annoying\ncharacters like spaces.<\/p>\n\n\n\n<p>Find will\ndisplay&nbsp;<strong>a list of\nmodified files<\/strong>&nbsp;, if it finds any, but we need to return\nthis list to our text file.&nbsp;Anything returned by the find command will be\nsent to &#8220;xargs&#8221; via the &#8220;|&#8221;.&nbsp;Xargs will execute the\ncommand &#8220;ls -lah&#8221; for each given file and return the display of\n&#8220;ls&#8221; to our text file.<\/p>\n\n\n\n<p><strong>mail\n-s &#8220;$ Subject&#8221; &#8220;inazo@example.com&#8221;\n&lt;modified-files-list.txt:<\/strong><br>\nAnd this last one will send an e-mail to inazo@example.com with as subject what\nwe have defined more top and for content that of our text file.<\/p>\n\n\n\n<p>Do not\nforget to make the script executable via the command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">chmod + x \/root\/scripts\/monitoring_www.sh<\/pre>\n\n\n\n<p>You can\nnow test it by running it with the command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sh \/root\/scripts\/monitoring_www.sh<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">For further :<\/h2>\n\n\n\n<p>What I\nadvise you if you have cache systems on your sites is to exclude them from the\nfind search.&nbsp;To do this, you must add exclusion paths, here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">!\u00a0-path \"* \/wp-content\/cache\/wp-rocket\/ *\"<\/pre>\n\n\n\n<p>This\naddition will ask not to list the files whose path contains &#8220;\/ wp-content\n\/ cache \/ wp-rocket \/&#8221;, and this regardless of the site in\nquestion.&nbsp;Which gives the modified command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">find \/var\/www\/ -name -ls -o -regex '. *. (php | htaccess | conf | html | js). *'\u00a0<strong>!\u00a0-path \"* \/ wp-content \/ cache \/ wp-rocket \/ *\"<\/strong>\u00a0-mtime 0 |\u00a0xargs ls -lah> modified-files-list.txt<\/pre>\n\n\n\n<p>You can\nof course add as many as you want.&nbsp;I often have 5 or 6 moreover depending\non the servers.<\/p>\n\n\n\n<p>Here is\nan example what you will receive by email:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-rw-r - r-- 1 user group 1177 Dec 21 09:52\u00a0\/var\/www\/wp-content\/plugins\/wordpress-seo\/admin\/import\/class-import-wpseo-hooks.php\u00a0\n-rw-r - r-- 1 user group 38 Dec 21 09:52\u00a0\/var\/www\/wp-content\/plugins\/wordpress-seo\/admin\/index.php\u00a0\n-rw-r - r-- 1 user group 1544 Dec 21 09:52\u00a0\/var\/www\/wp-content\/plugins\/wordpress-seo\/admin\/metabox\/class-metabox-add-keyword-tab.php\u00a0\n-rw-r - r-- 1 user group 903 Dec 21\u00a009:52 \/var\/www\/wp-content\/plugins\/wordpress-seo\/admin\/metabox\/class-metabox-addon-section.php <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Planning the script launch\u00a0<\/strong><\/h3>\n\n\n\n<p>You no\nlonger have to&nbsp;<strong>plan\nthe launch of the script via crontab<\/strong>&nbsp;.&nbsp;The idea is to\nlaunch it regularly, but be careful depending on the size of your server and\nthe sites installed&nbsp;<strong>it\nmay require some server resources<\/strong>&nbsp;.&nbsp;An\nexecution&nbsp;<strong>every 2\/3\nhours<\/strong>&nbsp;seems to me a good compromise and do not hesitate to\nput much less if you have a sensitive application.<\/p>\n\n\n\n<p>Besides,\nif this is the case, it is better to have&nbsp;<strong>a script dedicated to monitoring this sensitive\napplication<\/strong>&nbsp;.&nbsp;You can resume the script above with a\nmodification on the find path.<\/p>\n\n\n\n<p>Here is\nwhat you need to add in your crontab so that the script launches every 2\nhours:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0 * \/ 2 * * * sh \/root\/scripts\/monitoring_www.sh> \/ dev \/ null 2> &amp; 1 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Now you\nwill be notified&nbsp;<strong>by\ne-mail of the file modification<\/strong>&nbsp;on your server.&nbsp;For\nthe record, this piece of code that I use on some of my servers&nbsp;<strong>has saved me a number of times<\/strong>&nbsp;.&nbsp;It\nwill allow you, like me, to detect the beginning of an attack and to react\nquickly to stem it.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You will see&nbsp;one of the basics of security&nbsp;for managing&nbsp;your web applications&nbsp;.&nbsp;The&nbsp;monitoring of changes to your files&nbsp;.&nbsp;Indeed if you have a site in production the modifications of the source files will be few.&nbsp;And if there is, it will be easy for you to distinguish between your work and a plugin update that you have carried out.&nbsp;You [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[34],"tags":[232,112],"class_list":["post-2828","post","type-post","status-publish","format-standard","hentry","category-linux","tag-file-management","tag-linux"],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Monitor Files On Your Linux Server | 24x7serversupport Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Monitor Files On Your Linux Server | 24x7serversupport Blog\" \/>\n<meta property=\"og:description\" content=\"You will see&nbsp;one of the basics of security&nbsp;for managing&nbsp;your web applications&nbsp;.&nbsp;The&nbsp;monitoring of changes to your files&nbsp;.&nbsp;Indeed if you have a site in production the modifications of the source files will be few.&nbsp;And if there is, it will be easy for you to distinguish between your work and a plugin update that you have carried out.&nbsp;You [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/\" \/>\n<meta property=\"og:site_name\" content=\"24x7serversupport Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-01T00:15:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-02-29T15:59:02+00:00\" \/>\n<meta name=\"author\" content=\"24x7support\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@24x7serversuppo\" \/>\n<meta name=\"twitter:site\" content=\"@24x7serversuppo\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"24x7support\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/\",\"url\":\"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/\",\"name\":\"How To Monitor Files On Your Linux Server | 24x7serversupport Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/#website\"},\"datePublished\":\"2020-03-01T00:15:00+00:00\",\"dateModified\":\"2020-02-29T15:59:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/#\/schema\/person\/decfb5fad6bde6ac6822d4e965c6d401\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.24x7serversupport.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Monitor Files On Your Linux Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/#website\",\"url\":\"https:\/\/www.24x7serversupport.com\/blog\/\",\"name\":\"24x7serversupport Blog\",\"description\":\"Linux | CPanel | WHM | webhosting| Plesk | DirectAdmin | CentOs | Debian | Ubuntu Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.24x7serversupport.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/#\/schema\/person\/decfb5fad6bde6ac6822d4e965c6d401\",\"name\":\"24x7support\",\"url\":\"https:\/\/www.24x7serversupport.com\/blog\/author\/24x7support\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Monitor Files On Your Linux Server | 24x7serversupport Blog","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:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/","og_locale":"en_US","og_type":"article","og_title":"How To Monitor Files On Your Linux Server | 24x7serversupport Blog","og_description":"You will see&nbsp;one of the basics of security&nbsp;for managing&nbsp;your web applications&nbsp;.&nbsp;The&nbsp;monitoring of changes to your files&nbsp;.&nbsp;Indeed if you have a site in production the modifications of the source files will be few.&nbsp;And if there is, it will be easy for you to distinguish between your work and a plugin update that you have carried out.&nbsp;You [&hellip;]","og_url":"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/","og_site_name":"24x7serversupport Blog","article_published_time":"2020-03-01T00:15:00+00:00","article_modified_time":"2020-02-29T15:59:02+00:00","author":"24x7support","twitter_card":"summary_large_image","twitter_creator":"@24x7serversuppo","twitter_site":"@24x7serversuppo","twitter_misc":{"Written by":"24x7support","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/","url":"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/","name":"How To Monitor Files On Your Linux Server | 24x7serversupport Blog","isPartOf":{"@id":"https:\/\/www.24x7serversupport.com\/blog\/#website"},"datePublished":"2020-03-01T00:15:00+00:00","dateModified":"2020-02-29T15:59:02+00:00","author":{"@id":"https:\/\/www.24x7serversupport.com\/blog\/#\/schema\/person\/decfb5fad6bde6ac6822d4e965c6d401"},"breadcrumb":{"@id":"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.24x7serversupport.com\/blog\/how-to-monitor-files-on-your-linux-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.24x7serversupport.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Monitor Files On Your Linux Server"}]},{"@type":"WebSite","@id":"https:\/\/www.24x7serversupport.com\/blog\/#website","url":"https:\/\/www.24x7serversupport.com\/blog\/","name":"24x7serversupport Blog","description":"Linux | CPanel | WHM | webhosting| Plesk | DirectAdmin | CentOs | Debian | Ubuntu Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.24x7serversupport.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.24x7serversupport.com\/blog\/#\/schema\/person\/decfb5fad6bde6ac6822d4e965c6d401","name":"24x7support","url":"https:\/\/www.24x7serversupport.com\/blog\/author\/24x7support\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/posts\/2828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/comments?post=2828"}],"version-history":[{"count":1,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/posts\/2828\/revisions"}],"predecessor-version":[{"id":2833,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/posts\/2828\/revisions\/2833"}],"wp:attachment":[{"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/media?parent=2828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/categories?post=2828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/tags?post=2828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}