ls: Cannot Access .gvfs: Permission Denied


Whenever I run an application in terminal as root (such as sudo gedit /etc/default/varnish) , subsequently when I open another terminal I get "ls: cannot access .gvfs: Permission denied" error at top line of terminal.

I found a solution on the net

  umount /path/to/.gvfs
  rm -rf .gvfs

but it only fixes problem temporarily.

It appears that I have two mounted instances of gvfs in my system

  $ sudo mount |grep gvfs
    gvfsd-fuse on /run/user/1000/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=33)
    gvfsd-fuse on /home/****/.gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=0,group_id=0)

I don't know if it's related to, a few months ago I had to change permission in my home folder like

  sudo chown -R $USER:www-data

Would you please help me fix it?

After I unmount /run/user/1000/gvfs I don't get that error.

Best Answer

Running graphical applications with sudo can sometimes lead to problems like this.


sudo runs the program with superuser privileges (like running as root), but the program still sees the current home directory as your home directory.

So, when the software writes its configuration files, it'll end up creating files in your home directory that are owned by the root user. You end up with files you yourself cannot edit or delete, and software running as you won't be able to modify it either, leading to more problems.

What is the best solution?

There is an alternative: gksudo.

This variant of sudo will set up environment variables, such as the home directory, in such a way that makes running graphical applications as root a lot safer and won't confuse programs into creating root-owned files in your home directory.

Why does this affect graphical applications?

This doesn't just affect graphical applications, nor does it affect all graphical applications. It affects applications that store configuration inside the current user's home directory. This is just more common among graphical applications.

Sometimes applications will be able to detect if they're run with sudo and adjust their behaviour accordingly, but this is not common with graphical applications which are usually not expected to be run with sudo.

How do I fix the problem?

You'll need to find root-owned files and directories within your home directory and remove them. It's better to remove them than change their ownership in my opinion, as they were not intended for your user, but the root user, so there may be unexpected effects if you simply change ownership. You can of course back them up in case you decide there's something you want in them later.

To find root-owned files in your home directory:

find ~ -user root

If you are still having problems with some applications (run as your user), try rebooting to clear out /tmp and anything still running. Occasionally some applications might have corrupted their existing config files and need you to remove all their configuration in your home directory, but hopefully this won't be the case for many.