8

How can I add a script to nautilus for running selected file as administrator (not open as administrator)? And if it is possible, I want to run this file as administrator without entering my password.

3 Answers 3

7

Information on using sudo or gksudo on scripts without password

You need to do the following, on the terminal type sudo visudo and add a line like this at the end of the file specifying the commands you want to run without typing the sudo password:

<yourusername> hostname=NOPASSWD: <command1>, <command2>

Now you can run the specified commands without password as long as you type that command with sudo.

ie: lets you want to run shutdown -r now without having to type sudo password everytime and your username is 'joedoe'

  1. type sudo visudo on a terminal

  2. Add joedoe hostname=NOPASSWD: shutdown -r now as a new line at the end of the file

  3. on your script you can then use sudo shutdown -r now without having to type the sudo password.

To create a scrip use your favorite editor to create a <nameofyourscript>.sh with the contents:

#! /bin/bash

sudo <commandsyouwanttorun1>
<commandsyouwanttorun2>
sudo<commandsyouwanttorun3>

Use sudo to call commands that need it, it wont ask for password as long as you added that on the NOPASSWD: <commmand1>, <command2>, etc line in visudo.

After that you need to make it executable with: sudo chmod 755 <nameofyourscript>.sh.

Now you can run your script using sh <nameofyourscript>.sh on a terminal, by double clicking on it and selecting run on the dialog box or put them in your ~/.gnome2/nautilus-scripts/ which then will be available on your scripts menu when you right click on nautilus:

enter image description here

Create a right-click "Open as admin" without password

To create a scrip that opens files using administrator permissions by right clicking on them and making it so that no passwords will be asked create a script with the following:

#! /bin/bash
for file in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
gksudo "gnome-open $file" &
done

Save it on your ~/.gnome2/nautilus-scripts/, make it executable with sudo chmod 755 ~/.gnome2/nautilus-scripts/<nameofyourscript>.sh, using sudo visudo add the line <yourusername> ALL=NOPASSWD: /usr/bin/gnome-open and save the file.

You should be able to right-click a file, move to your scripts folder and selecting the script you just created to open that file using root permistions. gnome-open will handle the file type as good as possible.

click on your openasadmin script

done!

2
  • Thanks for answer! But there was a little misunderstanding: What I need was "run as admin" not "open as admin", so I chanched the line "gksudo "gnome-open $file" &" to "gksudo "gnome-terminal -x $file" &" Oct 13, 2011 at 5:50
  • Sure, the "first chapter" would give you enough info to do whatever you want to ;) glad it helped! Oct 13, 2011 at 5:52
1

Nautilus scripts usually are to be placed in $HOME/.gnome2/nautilus-scripts/ and need to be executable. That being said, the follow script should do, what you want:

#!/bin/bash

IFS='
'

for file in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
    gksudo -k "sh -c 'chmod +x $file; $file'"
done

You can access that script from nautilus by right clicking on the file, hovering over the script menu and then clicking on the filename of the script in $HOME/.gonme2/nautilus-scritps/. You still have to enter your password though.

There is no secure way you can do this with arbitrary files without entering your password. For special commands the process is described in brunopereira81's post here and uses sudo but depending on the commands will lead to a vulnerable system. If you don't care (and I strongly advice against this) you can set ALL commands to use NOPASSWD and replace gksudo by just sudo.

2
  • Why do you permanently chmod the file? Oct 12, 2011 at 20:31
  • because it was the quick way :D
    – con-f-use
    Oct 13, 2011 at 7:44
1

You can use this Nautilus extension. Nautilus-gksu Install nautilus-gksu.

sudo apt-get install nautilus-gksu

Installing it adds a 'Open as administrator' entry in your nautilus right-click menu.

enter image description here

When you need to open any files with root permission, you just have to right-click on the file, select 'Open as Administrator', as simple as that.

For more cool extensions visit : http://www.techdrivein.com/2010/09/6-useful-nautilus-extensions-and.html

2
  • "Open as administrator" still asks for a password.
    – con-f-use
    Oct 13, 2011 at 7:52
  • 1
    Ya it does.. :)
    – b-ak
    Oct 13, 2011 at 9:40

You must log in to answer this question.

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