1

I have seen the existing question about this, but the answer only specifies using an alias, which would mess up the interactive shell and not work for applications using exec() or similar means. I don't really want to move the sudo executable in /usr/bin because it would make upgrading difficult, but I don't really know how else this would be done. I would also like it to use askpass when called from a shell script.

1

You can check whether the current bash instance is interactive. However this may not work for other shells.

Now if you create a proxy script you can encapsulate the decision how to start sudo. Name it sudoAskpass.sh, for instance:

#!/bin/bash

BASH_PARAMETERS="$1"
shift

case "${BASH_PARAMETERS}" in
    *i*)
        sudo -A "$@"
        ;;
    *)
        sudo "$@"
        ;;
esac

exit $?

You need to alias sudo nevertheless, as within the script the $- parameter indicates a non-interactive shell:

alias sudo='/path/to/sudoAskpass.sh "$-"'
  • I think you misunderstood; I want the askpass to always be active, but I don't want to have to edit every script to use sudoAskpass.sh because aliases don't carry over to scripts – goose121 Oct 28 '17 at 18:36
  • @goose121 Well, your question missed the point to explicitely state what your real problem was, so I made some assumptions. Glad I could help, though. – Murphy Oct 28 '17 at 21:41
  • Yeah, that's my fault. Sorry about that. – goose121 Oct 30 '17 at 20:03
1

I figured out how to do it. First, make a script called sudo somewhere which is before /usr/bin in your path with the following contents:

#!/bin/bash

/usr/bin/sudo -A "$@"

This just passes its arguments onto the real sudo, but appends the -A argument. Then, in /etc/zshrc (or your shell's system-wide rc-file), make an alias back to the original sudo:

alias sudo="/usr/bin/sudo"
  • Note: This solution depends on the order of directories in PATH. It won't work if /usr/bin is placed before /usr/local/bin, – Murphy Nov 2 '17 at 9:24
  • Ah yes, I forgot to add that. – goose121 Nov 2 '17 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.