Via a desktop shortcut, an admin will launch a shell as another user. The user should go into the shell with no elevated privileges, but execute the first command with elevated privileges. In the end, the user should be in non-privileged shell that is kept open for further use.

How do I do this?

Question Edited For WIP Answer.

Latest test:

gksudo "gnome-terminal --execute bash -c 'boss "apt-get update"; bash'"

Does not work.

What follows is the answer thanks to Laurent helping me brainstorm. Unfortunately my situation was too sophisticated for me to express well.

I have an admin user who never goes online. He uses proxy users to fetch things and do his bidding. The user named update is in the sudoers file and is allowed to apt-get with NOPASSWD. admin wants a shortcut leading to a gnome-terminal that automatically calls apt-get update upon execution because admin wants to install something right after that, something that can't be predicted to be put in a script. Auto updates won't work for admin, because admin is fire-walled from the internet and is forced to hire goons for system updates and fetching other downloads.

Thank you Laurent for leading me to the right answer! Here it is:

gksudo -u update "gnome-terminal --execute bash -c 'sudo apt-get update;bash'"
  • It doesn't work because you are not placing the quotes correctly. Should be: gksudo "gnome-terminal --execute bash -c 'apt-get "update";bash'". (what does boss? - I think it was left from previous command so I did't use it). Anyways Quotes have to be alternated (", ', " again for example) and the parameters of the command you give have to be quoted. – laurent Dec 23 '12 at 21:26
  • just in case you need the boss command, here is the quotes sequence you need to use: gksudo "gnome-terminal --execute bash -c 'boss "apt-get 'update'"';bash" considering apt-get as a parameter of command boss (?!? is that what you want?). Anyways this will work for other commands instead of apt-get and boss. – laurent Dec 23 '12 at 21:45
  • sudo password is valid for a time. I don't understand very well why you are doing it this way but when the admin launches the terminal and input his password, the user can use sudo during a time without entering the password... I think the best way would be to use visudo and configure sudo to allow the user to use only the sudo commands he needs.... – laurent Dec 23 '12 at 21:52
  • laurent, I was not able to get it to work without those quotes. I have edited the gnome menu command shortcut. I've always had trouble with shortcuts parsing different than the command line parses. All this extra parenthesis. – cxx6xxc Dec 23 '12 at 23:32

You have to make the launcher using this:

gksudo "gnome-terminal --execute bash -c 'apt-get "update"; bash'"

Edit after definition of not opening terminal as root (to leave it in unelevated privileges after running the command:

gnome-terminal --execute bash -c "sudo apt-get 'update';bash"

This way, the terminal will open, ask for password (if needed) and continue.

Obs: if your command doesn't need sudo, just remove it from the command. I used gksudo on the first answer because your example was using it so I thought it was needed for the whole process (and leaving the terminal in elevated privileges).

Both launcher will work but 1st one will ask password with in GUI and run the whole process as sudo leaving the terminal open in "sudo mode" and 2nd launcher will open terminal, ask for password there, run command as sudo and leave terminal in "user mode".

You can edit sudo (with visudo) to make it use sudo without password (and maybe restrict this user to run only a few command if not only this one) but this is not the most recommended thing to do.

If you can give more info about what you are trying to do, maybe there is a different way to do it? (can be better... or worse... of course :) )

  • Ok, I edited things with a command we could all try. Although you will have to update sudoers and replace a relevant user. But, Anyway, see my edit. I simply replace the first command with apt-get update. Still not working. – cxx6xxc Dec 23 '12 at 19:55
  • Edited answer. But see my comment on the question – laurent Dec 23 '12 at 21:21
  • Edited the first part of this answer to use your new command apt-get update too – laurent Dec 23 '12 at 21:29
  • I marked yours as the answer, because you brought a mere inch a way. Thanks! I edited the question, answer at end. – cxx6xxc Dec 23 '12 at 22:03
  • As you used at the final of the question, the quotes around the parameters are optional in this case but there are commands instead of apt-get where you need the quotes so to be generic as apt-get was only an example, I used the quotes. – laurent Dec 23 '12 at 22:21

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.