3

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.

2 Answers 2

6

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'
Sign up to request clarification or add additional context in comments.

1 Comment

Though keep in mind that when you spawn off a shell using sudo, you've just created a root shell by providing a user password, which is why, IMO, that should not be enabled on a secure system.
0

The problem is that when you want to do that, you need to have the sudo password and be in the sudoers file, else it will fail. But you could try to use gksudo (graphical version of sudo) so when it gets called it asks for the password.

1 Comment

Hi, Thanks for the reply.... Is there any other way to accomplish this without entering password...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.