Use sudo without password INSIDE a script

前端 未结 8 1944
礼貌的吻别
礼貌的吻别 2020-11-29 03:29

For some reason I need, as user, to run without sudo a script script.sh which needs root privileges to work.
I saw as the only solution to put sudo INSIDE script.sh. Let

8条回答
  •  一整个雨季
    2020-11-29 03:51

    From my blog: IDMRockstar.com:

    The kicker is that sometimes, I need to run commands as root. Here's the quick and dirty way I accomplish that without divulging the passwords:

    #! /bin/bash
    read -s -p "Enter Password for sudo: " sudoPW
    echo $sudoPW | sudo -S yum update
    

    This way the user is prompted for the password (and hidden from terminal) and then passed into commands as needed, so I'm not running the entire script as root =)

    If you have a better, way, I'd love to hear it! I'm not a shell scripting expert by any means.

    Cheers!

    .: Adam

提交回复
热议问题