Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use sudo with password as parameter [closed]

Tags:

linux

bash

sudo

I would like to run sudo with my password as parameter so that I can use it for a script. I tried

sudo -S mypassword execute_command 

but without any success. Any suggestions?

like image 264
ccman Avatar asked Aug 14 '12 15:08

ccman


People also ask

How do I give sudo a password?

The -S (stdin) option allow the sudo command to read password from a standard input instead of a terminal device. If you want to store the password in a file you can use the cat command instead of echo like the following example.

Why can I sudo without supplying a password?

In all cases, you must know the root password in advance before sudo can be configured to commands without a password. You might have sudo access and grant another user account passwordless access for commands.


1 Answers

The -S switch makes sudo read the password from STDIN. This means you can do

echo mypassword | sudo -S command 

to pass the password to sudo

However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons

like image 145
stonesam92 Avatar answered Oct 14 '22 08:10

stonesam92