{"id":11341,"date":"2019-03-13T04:13:30","date_gmt":"2019-03-13T04:13:30","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11341"},"modified":"2019-03-13T04:13:30","modified_gmt":"2019-03-13T04:13:30","slug":"how-to-run-shell-scripts-with-sudo-command-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/how-to-run-shell-scripts-with-sudo-command-in-linux\/","title":{"rendered":"How to Run Shell Scripts with Sudo Command in Linux"},"content":{"rendered":"<p><strong>sudo<\/strong>\u00a0is a powerful command line tool that enables a \u201c<strong>permitted user<\/strong>\u201d to run a command as another user (the superuser by default), as defined by a security policy. On most if not all Linux systems, the security policy is driven by the\u00a0<strong>\/etc\/sudoers<\/strong><strong>\u00a0file.<\/strong><\/p>\n<p><strong>Read Also<\/strong>:\u00a0<a href=\"https:\/\/www.tecmint.com\/sudoers-configurations-for-setting-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener\">10 Useful Sudoers Configurations for Setting \u2018sudo\u2019 in Linux<\/a><\/p>\n<p>Therefore, to run a shell script or program as root, you need to use\u00a0<a href=\"https:\/\/www.tecmint.com\/run-sudo-command-without-password-linux\/\" target=\"_blank\" rel=\"noopener\">sudo command<\/a>. However,\u00a0<strong>sudo<\/strong>\u00a0only recognizes and runs commands that exist in directories specified in the\u00a0<strong>secure_path<\/strong>\u00a0in the\u00a0<strong>\/etc\/sudoers<\/strong>, unless a command is present in the\u00a0<strong>secure_path<\/strong>, you\u2019ll counter an error such as the one below.<\/p>\n<p>This will happen even if the script exists in a directory in the\u00a0<strong>PATH<\/strong>\u00a0environmental variable, because when a user invokes\u00a0<strong>sudo<\/strong>,\u00a0<strong>PATH<\/strong>\u00a0is replaced with\u00a0<strong>secure_path<\/strong>.<\/p>\n<pre>$ echo  $PATH\r\n$ ls  -l\r\n$ sudo proconport.sh 80\r\n<\/pre>\n<div id=\"attachment_27688\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Sudo-Error.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27688\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Sudo-Error.png\" alt=\"Sudo Error While Running Script\" width=\"702\" height=\"249\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Sudo Error While Running Script<\/p>\n<p>In the above scenario, the directory\u00a0<strong>\/home\/aaronkilik\/bin<\/strong>\u00a0is in the\u00a0<strong>PATH<\/strong>\u00a0environment variable and we are trying to run the script\u00a0<strong>\/home\/aaronkilik\/bin\/proconport.sh<\/strong>\u00a0(finds process listening on a port) with root privileges.<\/p>\n<p>Then we encountered the error \u201c<strong>sudo: proconport.sh: command not found<\/strong>\u201d, since\u00a0<strong>\/home\/aaronkilik\/bin<\/strong>\u00a0is not in the\u00a0<strong>sudo secure_path<\/strong>\u00a0as shown in next screenshot.<\/p>\n<div id=\"attachment_27689\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Check-Sudo-Secure-Path.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27689\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Check-Sudo-Secure-Path.png\" sizes=\"auto, (max-width: 942px) 100vw, 942px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Check-Sudo-Secure-Path.png 942w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Check-Sudo-Secure-Path-768x203.png 768w\" alt=\"Check Sudo Secure Path\" width=\"942\" height=\"249\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check Sudo Secure Path<\/p>\n<\/div>\n<p>To fix this, we need to add the directory containing our scripts in the\u00a0<strong>sudo secure_path<\/strong>\u00a0by using the\u00a0<strong>visudo command<\/strong>\u00a0by editing\u00a0<strong>\/etc\/sudoers<\/strong>\u00a0file as follows.<\/p>\n<pre>$ sudo visudo\r\n<\/pre>\n<p><strong>Attention<\/strong>: This method has serious security implications especially on servers running on the Internet. This way, we risk exposing our systems to various attacks, because an attacker who manages to gain access to an unsecure (without superuser privileges) directory that has been added to secure_path, can run a malicious script\/program with\u00a0<strong>sudo command<\/strong>.<\/p>\n<p>For security reason, do check out the following article from the sudo website explains a vulnerability relating to secure_path:\u00a0<a href=\"https:\/\/www.sudo.ws\/sudo\/alerts\/secure_path.html\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/www.sudo.ws\/sudo\/alerts\/secure_path.html<\/a><\/p>\n<p>Preferably, we can provide the absolute path to a script while running it with sudo:<\/p>\n<pre>$ sudo .\/proconport.sh 80\r\n<\/pre>\n<div id=\"attachment_27690\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Run-Script-using-Sudo-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27690\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Run-Script-using-Sudo-Command.png\" sizes=\"auto, (max-width: 962px) 100vw, 962px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Run-Script-using-Sudo-Command.png 962w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Run-Script-using-Sudo-Command-768x47.png 768w\" alt=\"Run Script Using Sudo Command\" width=\"962\" height=\"59\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Run Script Using Sudo Command<\/p>\n<\/div>\n<p>That\u2019s It! You may follow list of articles about sudo command:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/run-sudo-command-without-password-linux\/\" target=\"_blank\" rel=\"noopener\">How to Run \u2018sudo\u2019 Command Without Entering a Password in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/set-sudo-password-timeout-session-longer-linux\/\" target=\"_blank\" rel=\"noopener\">How to Keep \u2018sudo\u2019 Password Timeout Session Longer in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/fix-user-is-not-in-the-sudoers-file-the-incident-will-be-reported-ubuntu\/\" target=\"_blank\" rel=\"noopener\">How to Fix \u201cUsername is not in the sudoers file. This incident will be reported\u201d in Ubuntu<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/sudo-insult-when-enter-wrong-password\/\" target=\"_blank\" rel=\"noopener\">Let Sudo Insult You When You Enter Incorrect Password<\/a><\/li>\n<\/ol>\n<p><a style=\"font-size: 1rem;\" href=\"https:\/\/www.tecmint.com\/run-shell-scripts-with-sudo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>sudo\u00a0is a powerful command line tool that enables a \u201cpermitted user\u201d to run a command as another user (the superuser by default), as defined by a security policy. On most if not all Linux systems, the security policy is driven by the\u00a0\/etc\/sudoers\u00a0file. Read Also:\u00a010 Useful Sudoers Configurations for Setting \u2018sudo\u2019 in Linux Therefore, to run &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/how-to-run-shell-scripts-with-sudo-command-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Run Shell Scripts with Sudo Command in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11341","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11341"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11341\/revisions"}],"predecessor-version":[{"id":11342,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11341\/revisions\/11342"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}