3 Quick and Easy Ways to Open Any File as Root in Ubuntu

Spread the love

The root user is one of the most important aspects of a Linux distro. It allows you to access and modify any part of your system with ease. For example, it is simple to open and edit any file in Ubuntu if you are using root, but using root commands needs a bit of prep work. This tutorial shows some of the ways to easily open any file as root from inside your Ubuntu desktop.

This tutorial requires you to install some software. If your Ubuntu Software Center is not working, we have the fixes here.

Content

How to Run a File as Root in Ubuntu

Using the root user for daily tasks can be dangerous to your system, as Linux distros, by design, do not include any safety checks and precautions when running commands as root. As a result, most Linux distros today use “privilege escalation” programs, such as sudo and doas, to provide temporary root access and prevent common user mistakes while configuring system files. We are using sudo for the purposes of this tutorial.

1. Using Nautilus Admin

One of the quickest ways to open any file as the root user in Ubuntu is to install nautilus-admin, a small utility that acts as a plugin for privilege escalation programs in GNOME. It allows you to easily enable root access on a “per-file” basis.

  1. Run the following command to install nautilus-admin:
sudo apt install nautilus-admin

  1. Log out from your current session to reload the GNOME desktop, then open your File Manager to access a temporary root session by pressing Win and typing “files.”

  1. Doing this will open the Nautilus File Manager in your home directory. To open a file (or folder) as root, right-click it and select “Open as Administrator.”

  1. Instead of opening the file, you can use Nautilus to directly edit it as root with your favorite text editor. To edit a file in this manner, right-click it and select “Edit as Administrator.”

2. With a .Desktop File

Another quick way to open any file as the root user in Ubuntu is through .desktop files. These are special links that allow you to create custom shortcuts for any program in your system and include the ability to execute either sudo or su to launch any program as root.

  1. Open a text editor to create a .desktop file. We are using gedit here.

  1. Once inside, create a “Desktop Entry” block that contains all the information about your shortcut. For example, below is a block that does not link to any program:
[Desktop Entry]
Type=Application
Terminal=false
Name=
Exec=
  1. Give your new shortcut a name. We labeled it “nautilus-root,” as we want to create a root session for File Manager.

  1. Create a root session by adding the following command after the Exec= variable to link your File Manager to a sudo instance:
gnome-terminal -e "bash -c 'sudo -i nautilus;'"

  1. Enable the “execute bits” for your .desktop file by right-clicking your new shortcut and selecting “Allow Launching.”

  1. Double-click your .desktop file to launch a sudo prompt that will load the root session for your File Manager.

You can also hide your desktop icons if they are causing a distraction.

3. Using a Keybinding With pkexec

Lastly, it is also possible to bind a root command to a specific keyboard shortcut, which can be useful when you want to access a root session quickly. Follow the steps below to create a root keyboard shortcut.

  1. Go to your system’s Settings menu by pressing the Win key and typing “settings.”

  1. Select “Keyboard” from the left panel in the Settings menu.

  1. Select “View and Customize Shortcuts” under the “Keyboard Shortcuts” category.

  1. This will open a smaller window that lists all of the currently active shortcuts in your system. Scroll down this list and select “Custom Shortcuts.”

  1. Add a new shortcut by clicking “Add Shortcut” in the empty Custom Shortcuts window that appears.

  1. This will bring up a prompt to name the shortcut you’re creating. We are naming the shortcut “nautilus-root.”

  1. The same prompt asks you which command you want to execute. To run a root command, use pkexec. Paste in the following command to use pkexec to run a root session of Nautilus:
sh -c "pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY nautilus"

  1. Click “Set Shortcut” to bind the keyboard keys for your root command. We are setting the shortcut to Win + T, as we do not use this particular key combination.

  1. Press “Add” to save your changes and restart your machine to properly load your new root keyboard shortcut.

Also read: How to Hide the Top Bar and Side Panel in Ubuntu

Frequently Asked Questions

Is it still possible to open specific files as root in Ubuntu without Nautilus?

Yes and no. While it is possible to open a file as root in systems that do not use Nautilus, this specific feature is only available to a handful of desktop environments. For example, you can run: sudo apt install caja-admin to enable the “Administrator Mode” in MATE’s Caja File Manager.

Is it possible to create a shortcut for a program with both root and non-root access?

Yes! By default, the .desktop format allows you to create multi-function shortcuts that can run multiple commands with Actions=RootProg,NonRootProg added to the end of your “Desktop Entry” block.

Create two new blocks with the labels [Desktop Action RootProg] and [Desktop Action NonRootProg]. Include both the Name= and Exec= variables under each of your new labels.

Can I create a root keybinding if I am not using GNOME as my desktop?

Yes! Aside from the GNOME Shortcuts daemon, there are a number of third-party programs that allow you to create custom keybindings. One of the most versatile programs is the Simple X Hotkey Daemon, a utility that comes with bspwm. Its primary purpose is to provide an easy-to-use interface to create system keybindings.

To install Simple X Hotkey Daemon, run: sudo apt install sxkhd. You can learn more about how it works by running: man sxhkd.

Image credit: Alejandro Escamilla via Unsplash. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Ramces Red
Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.

Comments are closed