Add ‘Open File/Folder as Root’ Option in Elementary OS Freya

February 16, 2015

open as root administrator

This is a step by step tutorial shows how to add “open as root (or administrator)” option in Pantheon file browser in Elementary OS 0.3 Freya.

Elementary OS 0.3 Freya is lightweight and beautiful Linux distribution based on Ubuntu 14.04 LTS. Its default Pantheon file browser now has ‘Open in terminal’ option in folders’ context menu out-of-the-box.

But if you want to open a file or folder as root permission in Pantheon, you have to do a little tweak on your system.

Open as root / administrator

1. Open Software Center, search for and install gksu, a graphical frontend to su. It’s useful to graphical programs that need to ask a use’s password to run program as another user.

install-gksu

2. Open terminal from the Application menu.

open-terminal

3. Copy and paste below command in terminal and hit run:

sudo touch /usr/share/contractor/folder-openasroot.contract && gksudo scratch-text-editor /usr/share/contractor/folder-openasroot.contract

The code before ‘&&’ will create a empty file ‘folder-openasroot.contract’ under /usr/share/contractor directory, and the code after will open the file with the Scratch text editor.

When the file opens, paste below context and close the file (It’ll be saved automatically).

[Contractor Entry]
Name=Open folder as root
Icon=pantheon-files
Description=Open current folder as root privilege
MimeType=inode;
Exec=gksudo pantheon-files %U
Gettext-Domain=pantheon-files

Open folder as root

Now re-open your file browser and you’ll see the “Open folder as root” in folders’ context menu.

4. Similar to step 3, to add “Open file as root”, run below command instead:

sudo touch /usr/share/contractor/file-openasroot.contract && gksudo scratch-text-editor /usr/share/contractor/file-openasroot.contract

When the file opens, paste below and close it.

[Contractor Entry]
Name=Open file as root
Icon=scratch-text-editor
Description=open file as root with scratch
MimeType=text
Exec=gksudo scratch-text-editor %U
Gettext-Domain=scratch-text-editor

Done.

I'm a freelance blogger who started using Ubuntu 5+ years ago and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me on Google Plus or email to ubuntuhandbook1@gmail.com

16 responses to Add ‘Open File/Folder as Root’ Option in Elementary OS Freya

  1. Thank you so much I have been searching for this since Luna.

  2. I change scratch-text-editor by gedit, How would the code in this new scenario ? thanks!

    • Replace the scratch-text-editor with gedit and skip the part before &&(include &&) since it automatically created non-exist file, and the new created files will be opened with Gedit text editor, and don’t forget to click Save button after editing.

  3. Hello,

    It’s a great tweak. Unfortunately it doesn’t work for me. After step 3, I got the following error:

    (gksudo:6189): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,

    (gksudo:6189): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,

    (gksudo:6189): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,

    (gksudo:6189): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,

    (gksudo:6189): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,
    Gtk-Message: Failed to load module “canberra-gtk-module”
    [_LOG_LEVEL_INFO 12:01:24.470746] Application.vala:155: Scratch version: 2.1.0
    [_LOG_LEVEL_INFO 12:01:24.470856] Application.vala:157: Kernel version: 3.16.0-36-generic
    [_LOG_LEVEL_DEBUG 12:01:24.475746] Settings.vala:158: Loading settings from schema ‘org.pantheon.scratch.saved-state’
    [_LOG_LEVEL_DEBUG 12:01:24.476119] Settings.vala:158: Loading settings from schema ‘org.pantheon.scratch.settings’
    [_LOG_LEVEL_DEBUG 12:01:24.476724] Settings.vala:158: Loading settings from schema ‘org.pantheon.scratch.services’
    [_LOG_LEVEL_DEBUG 12:01:24.551837] [Gtk] Connecting to session manager
    [_LOG_LEVEL_WARN 12:01:24.556319] [Gtk] Attempting to add a widget with type GtkScrolledWindow to a GtkDialog, but as a GtkBin subclass a GtkDialog can only contain one widget at a time; it already contains a widget of type GtkBox
    [_LOG_LEVEL_FATAL 12:01:24.624829] [Gtk] gtk_header_bar_pack: assertion ‘gtk_widget_get_parent (widget) == NULL’ failed
    [_LOG_LEVEL_FATAL 12:01:24.624975] Scratch will not function properly.
    [_LOG_LEVEL_FATAL 12:01:24.791380] file /build/buildd/scratch-text-editor-2.1.0~r1491/src/MainWindow.vala: line 421: uncaught error: No such file or directory (g-io-error-quark, 1)
    [_LOG_LEVEL_FATAL 12:01:24.791536] Scratch will not function properly.
    [_LOG_LEVEL_FATAL 12:01:24.791684] [Gtk] gtk_action_set_visible: assertion ‘GTK_IS_ACTION (action)’ failed
    [_LOG_LEVEL_FATAL 12:01:24.791808] Scratch will not function properly.
    [_LOG_LEVEL_FATAL 12:01:24.792246] file /build/buildd/scratch-text-editor-2.1.0~r1491/src/MainWindow.vala: line 421: uncaught error: No such file or directory (g-io-error-quark, 1)
    [_LOG_LEVEL_FATAL 12:01:24.792371] Scratch will not function properly.
    [_LOG_LEVEL_DEBUG 12:01:24.795463] SplitView.vala:180: WelcomeScreen shown succefully
    [_LOG_LEVEL_DEBUG 12:01:24.798251] Settings.vala:158: Loading settings from schema ‘org.pantheon.scratch.plugins.folder-manager’
    [_LOG_LEVEL_DEBUG 12:01:24.798680] MainWindow.vala:472: create ‘unsaved’ directory: /root/.local/share/scratch-text-editor/unsaved
    [_LOG_LEVEL_DEBUG 12:01:24.799817] SplitView.vala:188: WelcomeScreen hidden succefully
    [_LOG_LEVEL_DEBUG 12:01:24.800952] SplitView.vala:126: View added succefully
    [_LOG_LEVEL_DEBUG 12:01:25.822508] sql-schema.vala:204: schema_version is 10
    [_LOG_LEVEL_DEBUG 12:01:25.823919] Document.vala:706: Could not find mount location
    [_LOG_LEVEL_INFO 12:01:25.824020] Document.vala:157: Opening “folder-openasroot.contract”
    [_LOG_LEVEL_INFO 12:02:07.700351] Document.vala:213: Closing “folder-openasroot.contract”
    [_LOG_LEVEL_DEBUG 12:02:07.700387] Document.vala:647: Backup file deleting: /usr/share/contractor/folder-openasroot.contract~
    [_LOG_LEVEL_DEBUG 12:02:07.700439] Document.vala:651: Backup file doesn’t exists: /usr/share/contractor/folder-openasroot.contract~
    [_LOG_LEVEL_FATAL 12:02:07.707313] [Gtk] gtk_container_remove: assertion ‘gtk_widget_get_parent (widget) == GTK_WIDGET (container) || GTK_IS_ASSISTANT (container) || GTK_IS_ACTION_BAR (container)’ failed
    [_LOG_LEVEL_FATAL 12:02:07.707355] Scratch will not function properly.
    [_LOG_LEVEL_DEBUG 12:02:07.707389] SplitView.vala:147: View removed succefully
    [_LOG_LEVEL_DEBUG 12:02:07.707543] SplitView.vala:180: WelcomeScreen shown succefully
    john@john-AsusPC:~$ sudo touch /usr/share/contractor/file-openasroot.contract && gksudo scratch-text-editor /usr/share/contractor/file-openasroot.contract

    (gksudo:6266): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,

    (gksudo:6266): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,

    (gksudo:6266): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,

    (gksudo:6266): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,

    (gksudo:6266): Gtk-WARNING **: Unable to locate theme engine in module_path: “pixmap”,
    Gtk-Message: Failed to load module “canberra-gtk-module”
    [_LOG_LEVEL_INFO 12:03:20.648378] Application.vala:155: Scratch version: 2.1.0
    [_LOG_LEVEL_INFO 12:03:20.648489] Application.vala:157: Kernel version: 3.16.0-36-generic
    [_LOG_LEVEL_DEBUG 12:03:20.653125] Settings.vala:158: Loading settings from schema ‘org.pantheon.scratch.saved-state’
    [_LOG_LEVEL_DEBUG 12:03:20.653501] Settings.vala:158: Loading settings from schema ‘org.pantheon.scratch.settings’
    [_LOG_LEVEL_DEBUG 12:03:20.654128] Settings.vala:158: Loading settings from schema ‘org.pantheon.scratch.services’
    [_LOG_LEVEL_DEBUG 12:03:20.705958] [Gtk] Connecting to session manager
    [_LOG_LEVEL_WARN 12:03:20.713331] [Gtk] Attempting to add a widget with type GtkScrolledWindow to a GtkDialog, but as a GtkBin subclass a GtkDialog can only contain one widget at a time; it already contains a widget of type GtkBox
    [_LOG_LEVEL_FATAL 12:03:20.734051] [Gtk] gtk_header_bar_pack: assertion ‘gtk_widget_get_parent (widget) == NULL’ failed
    [_LOG_LEVEL_FATAL 12:03:20.734125] Scratch will not function properly.
    [_LOG_LEVEL_FATAL 12:03:20.840128] [Gtk] gtk_action_set_visible: assertion ‘GTK_IS_ACTION (action)’ failed
    [_LOG_LEVEL_FATAL 12:03:20.840288] Scratch will not function properly.
    [_LOG_LEVEL_DEBUG 12:03:20.848007] SplitView.vala:180: WelcomeScreen shown succefully
    [_LOG_LEVEL_DEBUG 12:03:20.854372] Settings.vala:158: Loading settings from schema ‘org.pantheon.scratch.plugins.folder-manager’
    [_LOG_LEVEL_DEBUG 12:03:20.854908] MainWindow.vala:476: ‘unsaved’ directory already exists.
    [_LOG_LEVEL_DEBUG 12:03:20.883055] sql-schema.vala:204: schema_version is 10
    [_LOG_LEVEL_DEBUG 12:03:20.886330] Document.vala:706: Could not find mount location
    [_LOG_LEVEL_INFO 12:03:20.888348] Document.vala:157: Opening “folder-openasroot.contract”
    [_LOG_LEVEL_DEBUG 12:03:20.905698] SplitView.vala:188: WelcomeScreen hidden succefully
    [_LOG_LEVEL_DEBUG 12:03:20.908236] SplitView.vala:126: View added succefully
    [_LOG_LEVEL_DEBUG 12:03:20.994705] sql-schema.vala:204: schema_version is 10
    [_LOG_LEVEL_DEBUG 12:03:20.995820] Document.vala:706: Could not find mount location
    [_LOG_LEVEL_INFO 12:03:20.995906] Document.vala:157: Opening “file-openasroot.contract”
    [_LOG_LEVEL_INFO 12:03:44.492396] Document.vala:213: Closing “folder-openasroot.contract”
    [_LOG_LEVEL_DEBUG 12:03:44.492430] Document.vala:647: Backup file deleting: /usr/share/contractor/folder-openasroot.contract~
    [_LOG_LEVEL_DEBUG 12:03:44.492498] Document.vala:656: Backup file deleted: /usr/share/contractor/folder-openasroot.contract~
    [_LOG_LEVEL_INFO 12:03:44.492640] Document.vala:213: Closing “file-openasroot.contract”
    [_LOG_LEVEL_DEBUG 12:03:44.492666] Document.vala:647: Backup file deleting: /usr/share/contractor/file-openasroot.contract~
    [_LOG_LEVEL_DEBUG 12:03:44.492712] Document.vala:651: Backup file doesn’t exists: /usr/share/contractor/file-openasroot.contract~
    [_LOG_LEVEL_FATAL 12:03:44.499797] [Gtk] gtk_container_remove: assertion ‘gtk_widget_get_parent (widget) == GTK_WIDGET (container) || GTK_IS_ASSISTANT (container) || GTK_IS_ACTION_BAR (container)’ failed
    [_LOG_LEVEL_FATAL 12:03:44.499832] Scratch will not function properly.
    [_LOG_LEVEL_DEBUG 12:03:44.499866] SplitView.vala:147: View removed succefully
    [_LOG_LEVEL_DEBUG 12:03:44.500080] SplitView.vala:180: WelcomeScreen shown succefully
    *** BUG ***
    In pixman_region32_init_rect: Invalid rectangle passed
    Set a breakpoint on ‘_pixman_log_error’ to debug

    Could you please help me resolving it?

  4. Thanks for your reply, unfortunately it doesn’t work – there is no “open in terminal” option after right-clicking on a file or folder.

  5. Great. Thanks ;)

  6. hello, thanks for the tweak but after following the steps twice wheni right click on a folder there is no “open as root ” or similar option. does anyone know why this may be?

    • You may go to the File browser -> Computer/System -> usr -> share -> contractor, and check out the new created files (and their content) are really there.

      Or try gedit editor (install it from Software Center), run command to create & edit file:

      gksudo gedit /usr/share/contractor/folder-openasroot.contract

      When file opens, paste the rules and finally click SAVE. Log out and back in.

  7. Awesome, this was what i was looking for. I recently also had to run blender as root to get the CUDA support to work so I added this in as well. As always use it at your own risk when running executables as root, but it is handy.

    sudo touch /usr/share/contractor/file-runasroot.contract && gksudo scratch-text-editor /usr/share/contractor/file-runasroot.contract

    [Contractor Entry]
    Name=Run Executable as root
    Icon=pantheon-files
    Description=Run the Executable with root privileges
    MimeType=application/x-executable;
    Exec=gksudo %U
    Gettext-Domain=pantheon-files

  8. I used gedit instead and it worked great for me as long as the file is a text file. I would also like to open other file types that gedit will open like .desktop files. I tried to change the MineType to include desktop but it failed. Can you give me some guidance?

  9. In fact the method works only if you choose either one option (having open folder as root or file) When you make both none is available after restart or log out.

    System: Elementary Freya x64
    PC: Acer Aspire V

  10. Many thanks for this… worked perfectly! (And your English is great)

    Best.

  11. Ben, after you paste the code to scratch, hit the space bar before closing the file, else it won’t save it and it will not work for you.

    Regards,
    Grzegorz

  12. Jubair Siddeeque September 7, 2016 at 1:54 pm

    Thank you. New to Elementary

  13. I accidentally dumped the code for the open-as-root file into a terminal and the line “Exec=gksudo pantheon-files %U” seems to have done something. Now when I open Files, it always opens into the folder /%U. Do you have any idea how I fix this?