tags:

views:

90

answers:

6

This is about me being stressed by playing the game "type a command and remember to prepend sudo or your fingers will get slapped".

I am wondering if it is possible somehow to configure my Linux system or shell such that when I forget to type e.g. "sudo apt-get install emacs", instead of just telling me that I did something wrong, gksudo would get launched, allowing me to acknowledge my credentials and get on moving. Just like UAC does on windows.

Googling hasn't helped me yet..

So is this possible? Did I miss something? Or am I asking for a square circle?

Edit 2010 July 25th: Thanks everyone for your interrest. Unfortunately, Daenyth and bmargulies answers and explanations are what I anticipated/feared since it was impossible for me to google-up a solution prior to submitting this question. I hope that some nice person will someday provide an effective solution for this.

BR, Christian

A: 

I don't think this really works in a general way (automatically deciding which application needs admin rights). However you could make aliases like this for every application:

alias alias apt-get='gksudo apt-get'

If you now enter apt-get install firefox the gnome asks for the admin password. You can store the commands in ~./bashrc

nob
I am sorry, but this didn't fit my needs. What I am looking for is a "Linux decides when to ask" button somewhere that will turn it on or off for any application. Thanks for the suggestion.
Christian Madsen
A: 

You could use a shell script like the following:

#!/bin/bash
$@
if [ $? -ne 0 ]; then
    sudo $@    # or "gksudo $@"
fi

This will run a command given in the arguments with a sudo prefix if the command came back with a non-zero return code (i.e. if it failed). Use it as in "SCRIPT_NAME apt-get install emacs" for example. You may save it somewhere in your $PATH and set it as an alias like this (if you saved it as do_sudo):

alias apt-get='do_sudo apt-get'

Edit: That does not work for programs like synaptic which do work for non-root users but will give them less privileges. However, if the application fails when invoked without root privileges (like apt-get does) this works fine.

RWS
This is extremely fragile and likely to break or do the wrong thing
Daenyth
+1  A: 

You can do what you want with a preexec hook function, similar to the command-not-found package.

Karl Bielefeldt
+3  A: 

Linux doesn't allow for this. Unlike Windows, where any program can launch a dialog box, and UAC is in the kernel, Linux programs aren't necessarily GUI-capable, and sudo is not, in this sense, in the kernel. A program cannot make a call to elevate privilege (unless it was launched with privilege to begin with and intentionally setuid'd down). sudo is a separate executable with setuid privilege, which checks for permission. If it likes what it sees, it forks the shell to execute the command line. This can't be turned inside out.

As suggested in other posts, you may be able to come up with some 'shell game' to arrange to run sudo for you for some enumerated list of commands, but that's all you are going to get.

bmargulies
You don't need any special rights to `exec gksudo` though.
Ben Voigt
True. A program could re-launch itself via fork/exec of gksudo. There would be some interesting security challenges in writing out state to be read back into the relaunch.
bmargulies
A: 

There's no way to do this given the current linux software stack. Additionally, MS has a patent on this behavior -- present a user interface identifying an account having a right to permit a task in response to the task being prohibited based on a user's current account not having that right.

Daenyth
IANAL, but it seems that if the popup doesn't pre-select an account to `su` to, then it isn't covered by the patent. The patent also excludes accounts with 'unlimited rights' which would seem to refer to `root`.
Ben Voigt
It's probably invalid due to prior art anyway, but it's worth being aware of.
Daenyth
A: 

In the case where you want to always run a command as root but might already be root, you can solve this by wrapping a little bash script around it:

#!/bin/bash
if [ $EUID = 0 ]; then
    "$@"
else
    gksudo "$@"
fi

If you call this something like alwaysroot.bash and place it in the right spot on your PATH, then you can call your other program like this:

alwaysroot.bash otherprogram -arguments...

It even handles arguments with spaces in correctly.

Donal Fellows