Add ‘Open File/Folder as Root / Administrator’ in Elementary OS 6 [Update 2021]

Last updated: September 4, 2021 — 17 Comments

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

Elementary OS now is at version 6.0 based on Ubuntu 20.04 LTS. For those looking for the “Open as Administrator” function to open system file via root user permission, or do copy & paste things outside of user’s home directory, things have changed.

The executable command for the file manager now is io.elementary.files, and the command to start text editor is io.elementary.code. Both are named in Flatpak app style. Gksu has gnone, and “pkexec” takes the place to allow an authorized user to execute program as root.

Add Open as Admin action in Elementary OS 6:

1. ) Firstly, search for and open “terminal” from the top-left corner Applications menu.

2. When terminal opens, run command to create (& edit via nano) the configuration file for option to edit file as root user privilege:

sudo nano /usr/share/contractor/io.elementary.code.open-as-root.contract

When the file opens in terminal, paste following lines and save via Ctrl+X, type y and hit Enter.

[Contractor Entry]
Name=Edit as Administrator
Icon=io.elementary.code
Description=Edit file via root user privilege
MimeType=text/plain;
Exec=pkexec io.elementary.code %U
Gettext-Domain=code

3. And create another config file for an option to open folder as root permission:

sudo nano /usr/share/contractor/io.elementary.files.open-as-root.contract

Then paste the following lines and save it (Ctrl+X, type y, and hit Enter):

[Contractor Entry]
Name=Open as Administrator
Icon=system-file-manager
Description=Open a folder via root user privilege
MimeType=inode/directory;
Exec=pkexec io.elementary.files %U
Gettext-Domain=file manager

After creating the two files, “Edit as Administrator” and “Open as Administrator” context menu option appear when right-clicking a file or folder immediately without restart.

As you see, you can create more actions by creating files with “.contract” extension under /usr/share/contractor directory. The name will be displayed as menu option, and you can find value of icon, MineType and Exec by checking the app shortcut file (.desktop) under /usr/share/applications directory.

That’s all. Enjoy!

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee: https://ko-fi.com/ubuntuhandbook1

17 responses to Add ‘Open File/Folder as Root / Administrator’ in Elementary OS 6 [Update 2021]

  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?

  14. awesome one

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*