3

I use a program (yay, in archlinux) which uses sudo to elevate privileges. However, if it doesn't find sudo, it will switch to su, which is what I want it to do, since my user is not a sudoer, but I do have sudo installed (because there's another program which needs sudo to be installed even when run as root, I know it's silly).

Now the question is, how can I run this program (yay), while making it think sudo doesn't exist?

Note that yay is a package manager (AUR helper) which also builds and installs packages from source, and you can't do those builds as root, for security reasons, which is fair enough.

Also note that I've already opened an issue here on yay's repo, but while that's being fixed, I'd appreciate an alternative solution.

7
  • Can't you just run it as root? I mean su -c yay?
    – terdon
    Oct 20 at 10:46
  • Unfortunately not, since package builds from source can't be run as root.
    – adrin
    Oct 20 at 10:48
  • I was thinking you could just remove sudo's directory from your PATH when launching yay, but that won't work since on Arch, sudo is in /sbin which is just a symlink to /usr/bin and removing that would mean none of the scripts called by yay will work. If adding your user to sudoers isn't an option, I fear you're stuck and will have to modify yay to remove the sudo check.
    – terdon
    Oct 20 at 10:52
  • I have to assume that you've read the manual of yay. In there there is the mentioning of a --sudo option that takes the command to call instead of sudo. When you try yay --sudo su, how does it fail? Did you alse try modifying the su command in this way by means of the --sudoflags option?
    – they
    Oct 20 at 11:03
  • yay --sudo=su just doesn't do anything, no fail, no message, just exits.
    – adrin
    Oct 20 at 11:19
3

The only workaround I can think of without modifying yay itself would be to have a wrapper script which you will launch as root and which will temporarily remove sudo before launching yay:

#!/bin/sh

su -c "mv /sbin/sudo /sbin/sudo.old" && yay "$@"
su -c "mv /sbin/sudo.old /sbin/sudo"

That will ask you for the root password twice though. Another option is to just run the script as root and then in the script, use su to switch to another user:

#!/bin/sh

normalUser="$1"
shift

mv /sbin/sudo /sbin/sudo.old &&
su -c "yay $@" $normalUser
mv /sbin/sudo.old /sbin/sudo

You would then run this as:

su -c "/path/to/yay.wrapper.sh adrin $yayArguments" 

This should be safe in your case since you don't actually use sudo, but I would still make a copy of /sbin/sudo so you can easily get it back if something goes wrong, just in case.

1
  • While this might work, I think this is suboptimal - yay can be running for a very long time (potentially hours while it builds heavier packages), and for all that time sudo won't be available (OP mentioned at least one "some other program" that requires sudo).
    – Rogach
    Oct 22 at 8:22
2

The easiest way to bypass this problem will be to use the --sudo=su option.

tl;dr - run yay --sudo=su -Syu to achieve the desired effect.

Explanation why yay --sudo=su didn't do what you expected is below.


The issue is due to the way yay handles CLI arguments. From the docs:

If no arguments are provided 'yay -Syu' will be performed.

If no operation is selected -Y will be assumed.

If no arguments are provided yay -Syu is performed (full system update), but --sudo=su counts as an argument, so yay selects the -Y operation (list packages matching the query, which is empty in this case, and thus the command exits immediately).

Here's the piece of code responsible: https://github.com/Jguer/yay/blob/next/pkg/settings/parser/parser.go#L618

1
  • This perfectly solves my issue. But I think the general question is still valid, even though I don't need it now for my current use case.
    – adrin
    Oct 22 at 10:46

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.