14

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?

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

kenn
  • 5,232

2 Answers2

12

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

Explanation

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: pkexec.

Note: a previous version of this answer recommended the now-deprecated command gksudo.

The pkexec command will run the command in an environment which can safely run graphical applications as superuser without polluting your home directory with its configuration. Among other things, the home environment variable will be set appropriately.

To run X applications, you need to set some additional environment variables:

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY <command> <arguments>

What you can do is create a script to emulate the old gksudo behavior. Create a shell script gksudo in /usr/local/bin containing the following, and make it executable:

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY $@

The DISPLAY and XAUTHORITY environment variables aren't usually needed, but this version makes it compatible with certain system configurations.

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.

thomasrutter
  • 37,804
3

Just give the user permissions to the .gvfs/ and it should fix it.

sudo chown -R $USER:$USER ~/.gvfs/
kenn
  • 5,232