How to execute shell script via sudo user

11,795

With this command, the deploy script is executed under the root user, however, the ./tmp/cp1.sh script is ran by the current shell under your current privileges. To avoid this, you can prefix sudo to both:

sudo /usr/local/sbin/deploy | sudo ./tmp/cp1.sh

Or you can spawn a shell from within sudo, so the shell is already running as root:

sudo sh -c '/usr/local/sbin/deploy | ./tmp/cp1.sh'
Share:
11,795
Ramesh.T
Author by

Ramesh.T

Updated on June 30, 2022

Comments

  • Ramesh.T
    Ramesh.T almost 2 years

    Can someone give an example of running shellscript via sudo user?

    I tried like this.

    sudo /usr/local/sbin/deploy | ./tmp/cp1.sh

    The above script is executed as a normal user not as the sudo user.