5

I am searching for a way to configure sudo to not ask for a password (e.G. adding the NOPASSWD to the /etc/sudoers configuration but echoing the command to be executed for confirmation by asking something

Shall command cat /etc/passwd be executed? (y/N)

1
  • 1
    Who does this command get echoed to? Who is to approve it? How does this link to sudo?
    – Chris Down
    Mar 7, 2018 at 2:40

1 Answer 1

3

I think the below function may be able to solve your prompt problem:

sudo () {
    local command=$@
    read -rp "Shall command $command be executed? (y/N): "
    local YORN_RESP="$(grep -i "[YN]" <<<"${REPLY:0:1}" || echo 'N')"
    if [[ "$YORN_RESP" == [Yy] ]]; then
        command sudo "$@"
    else
        return 1
    fi
}

As long as this function is loaded it will take precedence over the sudo command, however it will not prevent people from executing /bin/sudo directly.

For the no password sudo it sounds like you have already found the solution but you need to uncomment the following line in your /etc/sudoers file:

%wheel  ALL=(ALL)       NOPASSWD: ALL
9
  • I don't think you want $@ there in the read. Mar 7, 2018 at 3:17
  • Have you tried actually running your code? Mar 7, 2018 at 3:29
  • 3
    You should use command sudo instead of /bin/sudo, since sudo may not always be located in /bin. And you can remove a bunch of local declarations if you run it in a subshell (sudo () ( ... ) instead of sudo () { ... }).
    – muru
    Mar 7, 2018 at 6:15
  • 2
    Also, drop RESP and use the default REPLY variable that read puts its data into.
    – Kusalananda
    Mar 7, 2018 at 7:34
  • 1
    From a UI perspective, I’d expect to be able to hit Enter at that prompt and have it assume Y; you seem to be requiring an entry. I’d suggest accepting an empty result as Y or lowercasing the Y in the prompt.
    – Jeff Schaller
    Mar 7, 2018 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .