-1

I want to create a desktop shortcut to turn off conservation mode on my laptop. For that I need to run this command: echo 0 | sudo tee /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode I made a desktop entry, but I don't know how to run it as superuser. This way it doesn't do anything. The closest thing I found was pkexec but that doesn't work.

0

2 Answers 2

-1

Use an alias. In your /home/user directory there is a file called .bashrc.

Put this at the end of the file, leaving a blank line at the end.

alias name_of_alias='your_command_from_the_question_here'

Save the file then, from terminal, run source .bashrc.

Now you can use name_of_alias as a command freely. Type it at command line, all by itself. To use it in the desktop link you need to use: bash -c "name_of_alias".

You do need pkexec to do it, if it doesn't want to pop up using bash -c, add it to the front of the command or try it with just the alias.

e.g. pkexec name_of_alias or pkexec bash -c name_of_alias

One of them will surely work.


Alternative to pkexec/alias:
This not secure, and is not for production use! It will keep the password visibly obfuscated and can be for personal use.

#!/bin/bash

if [ "$#" -eq 0 ]; then
    echo "Usage: $0 your_commands_here"
    exit 1
fi

command="$1"

password=$(yad --title="SU Password" --skip-taskbar --window-icon="input-keyboard" --on-top --geometry="300x150" --borders=15 --image="input-keyboard" --text-align=center --text="Enter Password:" --entry --separator="" --item-separator="" --hide-text)

exit_code=$?

if [[ $exit_code -ne 0 ]]; then
  exit 1
fi

# Decode the base64-encoded password
decoded_password=$(echo "$password" | base64 -d)

# Execute the command with sudo and the decoded password
fld1=$(echo "$decoded_password" | sudo -S -p "" -k "$command")

exit 0

Save to: yad_sudo.sh
Change mode to executable: chmod +x yad_sudo.sh
Usage: ./yad_sudo.sh your_commands_here

Use this for the shortcut:

bash -c /path/to/script/yad_sudo.sh echo 0 | sudo tee /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode
2
  • I got the alias to work and I can run it from the terminal, but with pkexec it doesn't work. Just with pkexec my_alias it says: Cannot run program conv_off: No such file or directory But with pkexec bash -c my_alias it says /usr/bin/bash: sor: 1: conv_off: command not found. Can you help with this?
    – Matee_k
    Jan 18 at 15:58
  • I put your command in a shell script like this. Save it as conserve_toggle.sh Run chmod +x conserve_toggle.sh Now use pkexec bash -c /path/to/script/conserve_toggle.sh for the shortcut. If pkexec doesn't pop up, try pkexec && bash -c...
    – JayCravens
    Jan 18 at 22:26
-1

Either configure your desktop icon to run the command in a terminal, so that sudo has somewhere to ask you for your password, or add an entry to the sudoers file to say that you can run the required command as root without a password.

A good way to debug this sort of thing is to add redirection of output (both stdout and stderr) into a file so that you can check it after you've tried the command. E.g.:

exec >/tmp/mylogfile 2>&1; set -x; ...

(You might need to put that whole script inside a bash -c option.)

You must log in to answer this question.

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