How to open folder as root and related question

I noticed that many big distros include a context menu entry to open as root/admin. Taking a look at the code ( you can see this:

uri = file.get_uri()
admin_uri = uri.replace("file://", "admin://")
subprocess.Popen([NAUTILUS_PATH, admin_uri])

It fails if you right click on a folder – it will open the current folder as root, not the selected one.

I’m trying a slightly different approach:["pkexec-script", "nautilus", file.get_uri().replace("file://", "").replace("%20", " ")])

  1. This doesn’t fix the issue mentioned above (but fixes another one that is irrelevant in this topic)
  2. As you can see I had to replace %20 with space, which breaks files/folders that actually contain %20. My question is if there’s a better way of doing this.


Is it possible you are getting mixed up between file items and background items?

I’d suggest looking at a something like urllib or GUri to change the scheme rather than replace

Also: Use GAppInfo to launch apps, not subprocess

Cool! Using urrlib fixed one of the issues:

urlEncoded = urlparse(file.get_uri()).path

Now, the other issue I finally got what’s causing it, but I don’t know how to proper fix it. The problem is the method get_background_items() (see original py file in the first post). This method is necessary to process clicks on the current folder, but it breaks when clicking on a subfolder. Any ideas?

Thanks once again!

Running nautilus like this is a really bad approach. And, we have a better solution already!

Just go to Other Locations, then Connect to Server with the admin:/// URL. After that, add a bookmark for that URL and rename it.

Once you do that, you can simply browse to the file system as administrator, only running a minimal amount of code as root. Making everything much safer! Every time you open a new file in an application, you’ll be re-prompted to authenticate, so it is still not possible for other applications to access things incorrectly.

Now, going back to the start. Don’t run pkexec-script to launch nautilus. Instead, just open nautilus with the admin:// scheme rather than file://.

(Right, I didn’t realise you were explicitly avoiding using admin://. I can only stress that it is a bad idea to do that.)

Thanks for your suggestion. I’m almost there!

I still don’t know how to fix the get_background_items() issue I mentioned in the post above. Any ideas?

I’m not sure what the issue is? get_background_items is for when the “background” - i.e. the folder itself - is clicked rather than any items in the folder.

So there is no “subfolder” involved?

The issue is when trying to open a subfolder as root so this get_background_items() is also triggered and it messes with get_file_items() in such way that the result is having the current folder being opened as root, and not the subfolder. I know that because I debugged the script and also because if I remove get_background_items() method then subfolders open as root normally.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.