2

In KDE setting you can assign a shortcut to execute your arbitrary command.

To assign a command to shortcut in KDE, you can do the following. In System Settings -> Shortcuts -> Custom Shortcuts, right click, choose New -> Global Shortcut -> Command/URL. Go to Action tab and fill in the command. And in Trigger tab assign the actual shortcut. And this mechanism works normally for the non-sudo commands.

enter image description here

But unfortunately, when I use the command that needs root privileges (let's assume sudo systemctl start something), it is just not executed.

Is there a way to bypass this limitation? I want to be able to trigger action, that requires evaluated permissions.

Ashark
  • 909

2 Answers2

2

One solution is to use an executable, which is owned by root and has a SUID bit.

Most likely, you do not have your own binary and instead you want to use a command. If you make your bash script file with suid bit, suid bit will be ignored, see Why does setuid not work?.

So, for your command you need to compile a binary.

Create the file start_smth.c:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main() { setuid(0); system("systemctl start something"); return 0; }

Compile it and set permissions:

$ gcc start_smth.c -o start_smth
$ sudo chown root:root start_smth
$ sudo chmod 4755 start_smth

In the Command/URL field in settings fill in the path to the start_smth binary file, for example /home/user/bin/start_smth.

enter image description here


If you have many commands you need to prepare in such way, you may use this script compile_and_set_permissions.sh:

#!/bin/bash

FILE="$1" FILE_NO_EXT="${FILE%.*}"

gcc "$FILE" -o "$FILE_NO_EXT" sudo chown root:root "$FILE_NO_EXT" sudo chmod 4755 "$FILE_NO_EXT"

Then pass a c files as a parameter:

$ compile_and_set_permissions.sh stop_smth.c
Ashark
  • 909
1

Use the name of your terminal emulator (for example Konsole) with -e flag, and then add your command:

konsole -e sudo systemctl start something
Eldian
  • 11