Use sudo with password as parameter [closed]

2019-01-05 01:19发布

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?

标签: linux bash sudo
5条回答
别忘想泡老子
2楼-- · 2019-01-05 02:02

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

查看更多
【Aperson】
3楼-- · 2019-01-05 02:04

One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.

查看更多
乱世女痞
4楼-- · 2019-01-05 02:08
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
查看更多
贼婆χ
5楼-- · 2019-01-05 02:16

You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):

sudo chmod +s myscript
查看更多
beautiful°
6楼-- · 2019-01-05 02:23
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
查看更多
登录 后发表回答