title: As a root user can I execute commands in BASH as another user without requiring a password?
body: <p>I have a root user account on an RHeL server. On that server I have a simple script called <code>user.sh</code> that just returns the current user:</p>
  
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  echo $USER
  </code></pre>
  
  <p>when run from my root account the output is</p>
  
  <pre><code>bash user.sh
  &gt;&gt;&gt;root
  </code></pre>
  
  <p>From another script I would like to be able to temporarily switch between users without entering a password, <a href="https://stackoverflow.com/questions/10882310/how-to-switch-to-root-user-without-entering-password-in-bash-script-on-redhat">storing the password in the script or a file</a>, or <a href="https://stackoverflow.com/questions/40668512/how-to-switch-to-different-user-inside-a-shell-script-and-execute-some-command-w">modifying /etc/sudoers</a> and execute <code>user.sh</code> and then return back to my initial root account. Is this at all possible?</p>
  
  <p>Here's what I've tried so far:</p>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/17758235/how-to-execute-a-group-of-commands-as-another-user-in-bash">Using delimeters to execute a block of code</a></p>
  
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  
  bash /user.sh
  
  su other_user &lt;&lt;EOF
  echo Current user: $USER
  EOF
  </code></pre>
  
  <p>output:</p>
  
  <pre class="lang-sh prettyprint-override"><code>root
  Current user: root
  </code></pre></li>
  <li><p>switching to a user in bash, executing a command and then logging back out</p>
  
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  
  bash /user.sh
  
  su other_user
  bash /user.sh
  exit
  </code></pre>
  
  <p>output: The script pauses execution and returns me to the terminal logged in as <code>other_user</code> however I will still be in my root account's directory that contains <code>user.sh</code></p>
  
  <pre class="lang-sh prettyprint-override"><code>root
  [other_user@my_server]$
  </code></pre>
  
  <p>if I then type <code>exit</code> I'm returned to my root account and the script completes execution</p></li>
  <li><p><a href="https://stackoverflow.com/questions/2538624/how-to-login-as-another-user-and-then-log-out-in-bash-script">using the <code>su - &lt;username&gt; -c /path/to/the/shellscript.sh</code> to execute a script as a different account and then return</a></p>
  
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  
  bash /user.sh
  
  su - other_user -c /path/user.sh
  </code></pre>
  
  <p>output:</p>
  
  <pre class="lang-sh prettyprint-override"><code>root
  -bash: /path/user.sh: Permission denied
  </code></pre></li>
  <li><p>using <code>sudo -i -u other_user</code> to log in as the user and execute the script which yields the same problem experienced with attempt #2 but I am redirected to <code>other_user</code>'s home directory.</p></li>
  </ol>
  
  <p>It may be worth noting that if I use method 2, while I'm logged in as <code>other_user</code> I am able to run <code>bash user.sh</code> and yield the desired output: <code>other_user</code></p>