0

I toggle my resolution with:

sudo xrandr --output eDP-1-1 --mode "3840x2400"

How do I make xrandr not require sudo? So e.g. I can do:

xrandr --output eDP-1-1 --mode "3840x2400"

I am in the video group. I would not like to expose my password in a regular file, but am willing to make some kind of system script which automatically becomes root temporarily just for xrandr purposes.

(The script will be called when I click a toggle button in a menubar, so in fact it will be a python script which detects the resolution and toggles between 3840x2400 and 1920x1200.)


EDIT: I believe some strange circumstance caused xrandr to require being run as root. This may have been nvidia-drm.modeset kernal parameter, or something like having to manually re-add modelines despite them existing. If you find yourself in this situation, I really don't know what to suggest. Please feel free to comment. Somehow the requirement to use sudo went away with a reboot. I am accepting the top answer.

1 Answers1

5
  1. xrandr doesn't require sudo. If a user is logged in to an X session, they can run xrandr to query and change screen settings.

  2. It's possible to set up /etc/sudoers such that a user (or users) can run a command as root without needing to enter a password. This is best used in combination with a short and simple wrapper script that does just the one thing that needs root and nothing else. Then allow that script to be run as root by the user with NOPASSWD:.

    Wrapper scripts like this should, preferably, take no arguments or other user-supplied input if at all possible. And if user input is unavoidable, it should take the bare minimum necessary to do the required job and validate all input before using it (and, for better security, instead of checking for known-bad things and rejecting them, it should check for known-good things and reject everything else).

    e.g.

    #!/bin/sh
    xrandr --output eDP-1-1 --mode "3840x2400"
    

    save this as, e.g., mode3840x2400.sh, make it executable with chmod, and add something like the following to /etc/sudoers (do not edit this file directly, use visudo):

    username ALL = NOPASSWD: /full/path/to/mode3840x2400.sh
    

Alternatively, here's an example of a wrapper script that accepts an argument requesting either of your desired resolution settings while rejecting everything else. It defaults to 1920 if no argument is supplied.

#!/bin/sh
res=${1:-1}

case "$res" in

  1. xrandr --output eDP-1-1 --mode "1920x1200" ;;
  2. xrandr --output eDP-1-1 --mode "3840x2400" ;;

*) echo "Error: unknown argument '$res'" > /dev/stderr; exit 1; esac

BTW, as mentioned above, xrandr doesn't need sudo to work - any user logged in to X can run it. I'm only using it as an example of something that could be run as root.

cas
  • 78,579
  • Thank you for the assistance. Your assurance that xrandr didn't require root (and the commenter saying it required a valid X auth) made me realize something was off. For more details, see edit at bottom of answer. Accepting answer, though feel free to edit however you see fit given the question edit to maximize usefulness to others. Cheers. – ninjagecko Aug 24 '23 at 16:12