{"id":979,"date":"2024-02-11T16:34:33","date_gmt":"2024-02-11T16:34:33","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=979"},"modified":"2024-02-12T09:13:23","modified_gmt":"2024-02-12T09:13:23","slug":"linux-sudo-file","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=979","title":{"rendered":"Linux &#8211; sudo file"},"content":{"rendered":"<p>The sudo command allows you to run programs with the security privileges of another user. Usually this command is used to run command as a\u00a0 superuser.<\/p>\n<p>The configuration files are :<\/p>\n<ul>\n<li>\/etc\/sudoers<\/li>\n<li>\/etc\/sudoers.d\/*<\/li>\n<\/ul>\n<p>Instead of editing these files usually the command tool called <strong>visudo<\/strong> is used<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> visudo<\/p>\n<p>In the editor that opens add the below like to provide user\u00a0<strong>shiju<\/strong>.<\/p>\n<p style=\"padding-left: 30px;\">shiju\u00a0 ALL=(ALL)\u00a0 \u00a0 \u00a0 \u00a0ALL<\/p>\n<p>By doing the above the user\u00a0<strong>shiju<\/strong> can run any command which the user\u00a0<strong>root<\/strong> can run by adding the command\u00a0<strong>sudo\u00a0<\/strong>in front of the command.<\/p>\n<p style=\"padding-left: 30px;\"><strong>[shiju@centos9vm ~]$<\/strong> useradd sam<br \/>\nuseradd: Permission denied.<br \/>\nuseradd: cannot lock \/etc\/passwd; try again later.<\/p>\n<p style=\"padding-left: 30px;\"><strong>[shiju@centos9vm ~]$<\/strong> sudo useradd sam<br \/>\n[sudo] password for shiju:<br \/>\n<strong>[shiju@centos9vm ~]$<\/strong><\/p>\n<p>Now let us remove the line &#8220;<strong>shiju\u00a0 ALL=(ALL)\u00a0 \u00a0 \u00a0 \u00a0ALL<\/strong>&#8221; from using the editor\u00a0<strong>visudo<\/strong>. Once done, the the user\u00a0<strong>shiju<\/strong> will not be able to execute command that only\u00a0<strong>root<\/strong> will be able to run.<\/p>\n<p style=\"padding-left: 30px;\"><strong>[shiju@centos9vm ~]$<\/strong> sudo useradd sam1<br \/>\n[sudo] password for shiju:<br \/>\nshiju is not in the sudoers file. This incident will be reported.<br \/>\n<strong>[shiju@centos9vm ~]$<\/strong><\/p>\n<p>Let us add the below line in sudoers using\u00a0<strong>visudo<\/strong> and check the result<\/p>\n<p style=\"padding-left: 30px;\"><em>shiju\u00a0 \u00a0ALL=(ALL)\u00a0 \u00a0 \u00a0 \u00a0\/usr\/bin\/cat \/var\/log\/httpd\/error_log<\/em><\/p>\n<p>Now run the below command and verify the result<\/p>\n<p style=\"padding-left: 30px;\"><strong>[shiju@centos9vm ~]$<\/strong> sudo \/usr\/bin\/cat \/var\/log\/httpd\/error_log<br \/>\n[sudo] password for shiju:<br \/>\n[Mon Feb 12 11:20:49.358785 2024] [core:notice] [pid 1858:tid 1858] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0<\/p>\n<p style=\"padding-left: 30px;\"><strong>[shiju@centos9vm ~]$<\/strong> sudo \/usr\/bin\/cat \/var\/log\/httpd\/access_log<br \/>\nSorry, user shiju is not allowed to execute &#8216;\/usr\/bin\/cat \/var\/log\/httpd\/access_log&#8217; as root on centos9vm.<br \/>\n<strong>[shiju@centos9vm ~]$<\/strong><\/p>\n<p style=\"text-align: left;\">Adding the below line in sudoers file via the editor\u00a0<strong>visudo<\/strong> will result in system not asking for password when using the\u00a0<strong>sudo<\/strong> command<\/p>\n<p style=\"text-align: left; padding-left: 30px;\">shiju\u00a0 \u00a0ALL=(ALL)\u00a0 \u00a0 \u00a0 \u00a0NOPASSWD:ALL<\/p>\n<p style=\"text-align: left;\">Let us try executing the command used above as see the difference<\/p>\n<p style=\"text-align: left; padding-left: 30px;\"><strong>[root@centos9vm ~]#<\/strong> sudo cat \/var\/log\/httpd\/error_log<br \/>\n[Mon Feb 12 11:20:49.358785 2024] [core:notice] [pid 1858:tid 1858] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0<br \/>\n<strong>[root@centos9vm ~]#<\/strong><\/p>\n<p style=\"text-align: left;\">All these changes will be edited to the file\u00a0<strong>\/etc\/sudoers<\/strong> by the\u00a0<strong>visudo<\/strong> editor. However, one could also add similar entries in <strong>\/etc\/sudoers\/&lt;any file&gt;<\/strong>, and these entries too will be read when loading the sudo related entries.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The sudo command allows you to run programs with the security privileges of another user. Usually this command is used to run command as a\u00a0 <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=979\" title=\"Linux &#8211; sudo file\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":252,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,30],"tags":[],"class_list":["post-979","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-rhel-9"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=979"}],"version-history":[{"count":16,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/979\/revisions"}],"predecessor-version":[{"id":995,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/979\/revisions\/995"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/252"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=979"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}