{"id":25998,"date":"2016-04-05T21:14:59","date_gmt":"2016-04-05T19:14:59","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=25998"},"modified":"2019-02-25T12:13:59","modified_gmt":"2019-02-25T10:13:59","slug":"absolute-stupidity-of-include-directive-in-etc-sudoers-and-microsoft-azure","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2016\/04\/05\/absolute-stupidity-of-include-directive-in-etc-sudoers-and-microsoft-azure\/","title":{"rendered":"Absolute stupidity of include directive in \/etc\/sudoers, and Microsoft Azure"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>I&#8217;ve just spent three hours (!!!) trying to troubleshoot why sudo was misbehaving on a brand new CentOS 7 server. &nbsp;I was doing the setup of two identical servers in parallel (for two different clients). &nbsp; One server worked as expected, the other one didn&#8217;t.<\/p>\n<p>The thing I was trying to do was trivial &#8211; allow users in the wheel group execution of sudo commands without password. I&#8217;ve done it a gadzillion times in the past, and probably at least a dozen times just this week alone. &nbsp;Here&#8217;s what&#8217;s needed:<\/p>\n<ol>\n<li>Add user to the <em>wheel<\/em> group.<\/li>\n<li>Edit <em>\/etc\/sudoers<\/em> file to uncommen tthe line (as in: remove the hash comment character from the beginning of the file):&nbsp;<em># %wheel ALL=(ALL) NOPASSWD: ALL<\/em><\/li>\n<li>Enjoy!<\/li>\n<\/ol>\n<p>Imagine my surprise when it only worked on one server and not on the other. &nbsp;I&#8217;ve dug deep and wide. &nbsp;Took a break. And dug again. &nbsp;Then, I&#8217;ve summoned the great troubleshooting powers of <a href=\"http:\/\/alex.mamchenkov.net\">my brother<\/a>. &nbsp;But even those didn&#8217;t help.<\/p>\n<p>Lots of logging, diff-ing, strace-ing, swearing and hair pulling later, the problem was found and fixed. &nbsp;The issue was due to two separate reasons.<\/p>\n<p><strong>Reason 1<\/strong>: <em>\/etc\/sudoers<\/em> syntax uses the hash character (#) for two different purposes.<\/p>\n<ol>\n<li>For comments, which there are plenty of in the file.<\/li>\n<li>For the &#8220;<em>#include<\/em>&#8221; and &#8220;<em>#includedir<\/em>&#8221; directives, which include other files into the configuration.<\/li>\n<\/ol>\n<p>The default <em>\/etc\/sudoers<\/em> file is full of lengthy comments. &nbsp;Just to give you and idea:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\n(root@host ~)# wc -l \/etc\/sudoers\n118 \/etc\/sudoers\n(root@host ~)# grep -v '^#' \/etc\/sudoers | grep -v '^$' | wc -l\n12\n<\/pre>\n<p>Yup. &nbsp;<strong>118 lines<\/strong> in total vs. <strong>12 lines<\/strong> of configuration (comments and empty lines removed). Like with <a href=\"https:\/\/en.wikipedia.org\/wiki\/Banner_blindness\">banner blindness<\/a>, this causes comment blindness. &nbsp;Especially towards the end of the file. &nbsp;Especially if you&#8217;ve seen this file a billion times before.<\/p>\n<p>And that&#8217;s where the problem starts. &nbsp;Right at the bottom of the file, there are these two lines:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\n##Read drop-in files from \/etc\/sudoers.d (the # here does not mean a comment)\n#includedir \/etc\/sudoers.d\n<\/pre>\n<p>Interesting, right? Usually there is nothing in the <em>\/etc\/sudoers.d\/<\/em> folder on the brand new CentOS box. But even if there was something, by now you&#8217;d assume that the include of the folder is commented out. Much like that <em>wheel<\/em> group configuration I mentioned earlier. I found it by accident, while reading <em>sudoers(5)<\/em> manual page, trying to find out if there are any other locations or defaults for included configurations. About 600 lines into the manual, there is this:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\nTo include \/etc\/sudoers.local from within \/etc\/sudoers we\nwould use the following line in \/etc\/sudoers:\n\n#include \/etc\/sudoers.local\n\nWhen sudo reaches this line it will suspend processing of\nthe current file (\/etc\/sudoers) and switch to\n\/etc\/sudoers.local.\n<\/pre>\n<p>So that comment is not a comment at all, but an include of the folder. &nbsp;That&#8217;s the first part of the problem.<\/p>\n<p><strong>Reason #2<\/strong>: Windows Azure Linux Agent<\/p>\n<p>As I mentioned above, the servers aren&#8217;t part of my infrastructure &#8211; they were provided by the clients. &nbsp;I was basically given an IP address, a username and a password for each server &#8211; which is usually all I need. &nbsp;In most cases I don&#8217;t really care where the server is hosted and what&#8217;s the hosting company in use. &nbsp;Turns out, I should.<\/p>\n<p>The server with the problem was hosted on the Microsoft Azure cloud infrastructure. &nbsp;I assumed I was working off a brand new vanilla CentOS 7 box, but in fact I wasn&#8217;t. &nbsp;Microsoft adds packages to the default install. &nbsp;On of the packages that it adds is the Windows Azure Linux Agent, which &#8220;<em>rpm -qi WALinuxAgent<\/em>&#8221; describes as following:<\/p>\n<blockquote><p>The Windows Azure Linux Agent supports the provisioning and running of Linux&nbsp;VMs in the Microsoft Azure cloud. This package should be installed on Linux disk&nbsp;images that are built to run in the Microsoft Azure environment.<\/p><\/blockquote>\n<p>Harmless, right? Well, not so much. &nbsp;What I found in the <em>\/etc\/sudoers.d\/<\/em> folder was a little file, called <em>waagent<\/em>, which included the different sudo configuration for the user which I had a problem with.<\/p>\n<p>During the troubleshooting process, I&#8217;ve created a new test user, added the account to the wheel group and found out that it was working fine. &nbsp;From there, I needed to find the differences between the two users.<\/p>\n<p>I guess, the user that I was using initially was created by the client&#8217;s system administrator using Microsoft Azure web interface. &nbsp;A quick Google search brings <a href=\"https:\/\/azure.microsoft.com\/en-gb\/documentation\/articles\/virtual-machines-linux-use-root-privileges\/\">this page<\/a>&nbsp;from the Azure documentation:<\/p>\n<blockquote><p>By default, the <code>root<\/code> user is disabled on Linux virtual machines in Azure. Users can run commands with elevated privileges by using the <code>sudo<\/code> command. However, the experience may vary depending on how the system was provisioned.<\/p>\n<ol>\n<li><strong>SSH key and password OR password only<\/strong> &#8211; the virtual machine was provisioned with either a certificate (<code>.CER<\/code> file) or SSH key as well as a password, or just a user name and password. In this case <code>sudo<\/code> will prompt for the user&#8217;s password before executing the command.<\/li>\n<li><strong>SSH key only<\/strong> &#8211; the virtual machine was provisioned with a certificate (<code>.cer<\/code>, <code>.pem<\/code>, or <code>.pub<\/code>file) or SSH key, but no password. In this case <code>sudo<\/code> <strong>will not<\/strong> prompt for the user&#8217;s password before executing the command.<\/li>\n<\/ol>\n<\/blockquote>\n<p>I checked the user&#8217;s home folder and found no keys in there, so I think it was provisioned using the first option, with password only.<\/p>\n<p>I think Microsoft should make it much more obvious that the system behavior might be different. &nbsp;<a href=\"https:\/\/aws.amazon.com\">Amazon AWS<\/a> provides a good example to follow. &nbsp;When you login into Amazon AMI instance, you see a message of the day (motd) banner, which looks like this:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\n$ ssh server.example.com\nLast login: Tue Apr  5 17:25:38 2016 from 127.0.0.1\n\n__|  __|_  )\n_|  (     \/   Amazon Linux AMI\n___|\\___|___|\n\nhttps:\/\/aws.amazon.com\/amazon-linux-ami\/2016.03-release-notes\/\n\n(user@server.example.com)$\n<\/pre>\n<p>It&#8217;s dead obvious that you are now on the <a href=\"https:\/\/aws.amazon.com\/ec2\/\">Amazon EC2<\/a> machine and you should adjust your <del>expectations<\/del> assumptions&nbsp;accordingly.<\/p>\n<p>Deleting the file immediately solved the problem. &nbsp;To avoid similar issues in the future, #includedir directive can be moved further up in the file, and surrounded by more visible comments. &nbsp;Like, maybe, an <a href=\"http:\/\/www.asciiworld.com\/-Death-Co-.html\">ASCII art skull<\/a>, or something.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/04\/ascii-skull.png?ssl=1\" rel=\"attachment wp-att-25999\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25999\" data-permalink=\"https:\/\/mamchenkov.net\/wordpress\/2016\/04\/05\/absolute-stupidity-of-include-directive-in-etc-sudoers-and-microsoft-azure\/ascii-skull\/\" data-orig-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/04\/ascii-skull.png?fit=249%2C289&amp;ssl=1\" data-orig-size=\"249,289\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ASCII skull\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/04\/ascii-skull.png?fit=249%2C289&amp;ssl=1\" class=\"aligncenter size-full wp-image-25999\" src=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/04\/ascii-skull.png?resize=249%2C289&#038;ssl=1\" alt=\"ASCII skull\" width=\"249\" height=\"289\"><\/a><\/p>\n<p>With that, I am off to heavy drinking and&nbsp;recovery&#8230; Stay sane!<\/p>\n<p>&nbsp;<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>I&#8217;ve just spent three hours (!!!) trying to troubleshoot why sudo was misbehaving on a brand new CentOS 7 server. &nbsp;I was doing the setup of two identical servers in parallel (for two different clients). &nbsp; One server worked as expected, the other one didn&#8217;t. The thing I was trying to do was trivial &#8211; &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2016\/04\/05\/absolute-stupidity-of-include-directive-in-etc-sudoers-and-microsoft-azure\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Absolute stupidity of include directive in \/etc\/sudoers, and Microsoft Azure<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Absolute stupidity of include directive in \/etc\/sudoers, and Microsoft Azure #SysAdmin #DevOps #Linux #Microsoft #Azure #security","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,6,133,62,1334],"tags":[3246,2366,3414,200,2289],"keyring_services":[],"class_list":["post-25998","post","type-post","status-publish","format-standard","hentry","category-general","category-linux","category-sysadmin","category-technology","category-web-work","tag-centos-linux","tag-cloud-computing","tag-microsoft-azure","tag-security","tag-web-hosting"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":17949,"url":"https:\/\/mamchenkov.net\/wordpress\/2013\/04\/05\/accessing-current-username-sudo-scripts-centos\/","url_meta":{"origin":25998,"position":0},"title":"Accessing current username in sudo scripts on CentOS","author":"Leonid Mamchenkov","date":"April 5, 2013","format":false,"excerpt":"I got a bit of a puzzle at work today. \u00a0I had a script that was executed as another user via sudo, but I wanted to access the original username in the script, to know who was executing it. \u00a0Sudoers manual suggest working with \"Defaults env_keep\". \u00a0Looking into the \/etc\/sudoers,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":16335,"url":"https:\/\/mamchenkov.net\/wordpress\/2012\/06\/07\/closing-the-microsoft-vs-linux-chapter\/","url_meta":{"origin":25998,"position":1},"title":"Closing the Microsoft vs. Linux chapter","author":"Leonid Mamchenkov","date":"June 7, 2012","format":false,"excerpt":"Slashdot reports: After years of battling Linux as a competitive threat,\u00a0Microsoft is now offering Linux-based operating systems on its Windows Azure cloud service. The Linux services will go live on Azure at 4 a.m. EDT on Thursday. At that time, the Azure portal will offer a number of Linux distributions,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26227,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/07\/07\/forcing-amazon-linux-ami-compatibility-with-centos-in-ansible\/","url_meta":{"origin":25998,"position":2},"title":"Forcing Amazon Linux AMI compatibility with CentOS in Ansible","author":"Leonid Mamchenkov","date":"July 7, 2016","format":false,"excerpt":"One of the things that makes Ansible so awesome is a huge collection of shared roles over at Ansible Galaxy. \u00a0These bring you best practices, flexible configurations and in general save hours and hours of hardcore swearing and hair pulling. Each role usually supports multiple versions of multiple Linux distributions.\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26910,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/11\/20\/automate-openvpn-client-on-centos-7\/","url_meta":{"origin":25998,"position":3},"title":"Automate OpenVPN client on CentOS 7","author":"Leonid Mamchenkov","date":"November 20, 2016","format":false,"excerpt":"I need to setup OpenVPN client to start automatically on a CentOS 7 server for one of our recent projects at work. \u00a0I'm not well versed in VPN technology, but the majority of the time was spent on something that I didn't expect. I go the VPN configuration and all\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9112,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/06\/19\/watching-over-logs-in-kde\/","url_meta":{"origin":25998,"position":4},"title":"Watching over logs in KDE","author":"Leonid Mamchenkov","date":"June 19, 2005","format":false,"excerpt":"I know of a lot of people who enjoy having a terminal window with scrolling logs on their desktop. Setting one up was never a challange, but there are some nice KDE options that one could use that not so many people know about. At least I didn't know until\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26208,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/06\/27\/lets-encrypt-on-centos-7-and-amazon-ami\/","url_meta":{"origin":25998,"position":5},"title":"Let&#8217;s Encrypt on CentOS 7 and Amazon AMI","author":"Leonid Mamchenkov","date":"June 27, 2016","format":false,"excerpt":"The last few weeks were super busy at work, so I accidentally let a few SSL certificates expire. \u00a0Renewing them is always annoying and time consuming, so I was pushing it until the last minute, and then some. Instead of going the usual way for the renewal, I decided to\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/25998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=25998"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/25998\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=25998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=25998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=25998"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=25998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}