Ubuntu – How to add a script to nautilus for running selected file as administrator

administratornautilusscripts

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.

Best Answer

  • 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!