- How can I cd into a directory as root?
- 3 Answers 3
- Open File as Root
- Linux admin:// Command
- Conclusion
- 3 Quick and Easy Ways to Open Any File as Root in Ubuntu
- Content
- How to Run a File as Root in Ubuntu
- 1. Using Nautilus Admin
- 2. With a .Desktop File
- 3. Using a Keybinding With pkexec
- Frequently Asked Questions
- Is it still possible to open specific files as root in Ubuntu without Nautilus?
- Is it possible to create a shortcut for a program with both root and non-root access?
- Can I create a root keybinding if I am not using GNOME as my desktop?
- How to open Ubuntu file manager as root user
- Open Ubuntu Nautilus File Manager as root
- Edit or Open Ubuntu Files and Folders as root Administrative
- Warning: Running File Manager as Admin
How can I cd into a directory as root?
Let’s say there’s a directory that I don’t have the privileges to access. Obviously sudo cd foo won’t work, because cd is a shell builtin in every shell ever. So far, I’ve just been using sudo bash (yes I know there are better ways) to get a root prompt. Then, I can cd into the directory to poke around. Is there a better way to do this?
That’s not really a valid case. It may be useful to be able to read the directory, but that doesn’t require the directory to be your working directory, i.e. you don’t need cd .
3 Answers 3
Nope, there isn’t a way to cd to a directory that only allows root without being root. There really shouldn’t be too many directories that have this limitation. Most of the time it’s the access to a given file that’s limited such as the /etc/shadow file or specific log files under /var/log .
You can use sudo ls to see them instead of bash . Also when using sudo to become root you typically want to set user ( su command) instead of bash , so use this command instead:
You can also accomplish the same thing with a sudo’s -i switch:
excerpt from man page regarding sudo -i
The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the target user as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed.
For the files that you can’t access you can use either of these sudo commands:
$ sudo tail /var/log/messages $ sudo less /etc/shadow
Open File as Root
If you need to open a file as root in linux you should never use sudo. Sudo can cause files and folders in your home directory to be owned by root. The key is to open graphical applications with root privileges while maintaining user ownership. The proper way was to use gksu or gksudo which are the graphical variants of sudo. These prevented files and folders from being owned by root. Unfortunately gksu and gksudo are no longer available for most linux distributions. So what is the proper way now to open a file or directory as root in linux? Some linux distros have implemented their own way of handling root for graphical applications. For example, distros using the KDE desktop environment use (kdesu) to open graphical applications with root privileges. In this tutorial I’ll be showing you a method that works across most linux distributions. It involves using the admin command which presents you with a graphical password prompt to grant root.
Linux admin:// Command
If for example, you need to open a file in a text editor like gedit as root. Enter the following command in a terminal to open a file in gedit as root. Make sure to change (/path/to/file) to the actual absolute path to a file you wish to open as root.
You can use the admin command with your preferred text editor. To open a directory as root with your file manager, enter the following in a terminal. In this example we’ll be using thunar file manager, but you can use nautilus or whichever you have installed on your system. Again replace (/path/to/directory) to the actual absolute directory path you wish to open as root.
thunar admin:///path/to/directory
Conclusion
3 Quick and Easy Ways to Open Any File as Root in Ubuntu
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.
sudo apt install nautilus-admin
- 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.”
- 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.”
- 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.
- 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=
- Give your new shortcut a name. We labeled it “nautilus-root,” as we want to create a root session for File Manager.
- 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;'"
- Enable the “execute bits” for your .desktop file by right-clicking your new shortcut and selecting “Allow Launching.”
- 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.
- 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.”
- This will bring up a prompt to name the shortcut you’re creating. We are naming the shortcut “nautilus-root.”
- 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"
- 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.
- Press “Add” to save your changes and restart your machine to properly load your new root keyboard shortcut.
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.
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.
Our latest tutorials delivered straight to your inbox
How to open Ubuntu file manager as root user
By default, the File Manager in Ubuntu or in any other Linux distro uses a non-root user. I mean a user that doesn’t have Administrative rights can access them graphically. It improves overall system security. However, if you want to run File manager as the root user or want to open & edit files and folder with administrative rights that are possible as well. However, you have to use the command terminal to enable that. The following tutorial carried out on Ubuntu 20.04 LTS focal fossa. However, the steps will be the same for earlier versions such as Ubuntu 19.10/19.04; 18.10/18.04 including Debian, Linux Mint, or any similar OS running Nautilus File manager.
Open Ubuntu Nautilus File Manager as root
- Open command terminal either from Applications or using keyboard shortcut- Ctrl+Alt+T.
- Run Nautilus file manager with sudo. Here is the syntax:
Edit or Open Ubuntu Files and Folders as root Administrative
If you want to edit any file or open folder as root user directly from the Nautilus file manager’s graphical user interface. Then, we can embed an option “Edit as Administrator” or “Open as Administrator” privileges in its contextual menu.
- Again open command terminal, if you already have not.
- Install the Nautilus Admin package.
sudo apt install nautilus-admin
Warning: Running File Manager as Admin
Although, the above tutorial is useful in case you are not a well-experienced command terminal user, however, be cautious. Make sure you wouldn’t alter or edit some core configuration files that could leave your Ubuntu unstable or un-bootable.