4

When I run updatedb from a terminal, I get this error:

updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'

Any ideas about how to get updatedb to work?

xiota
  • 4,709
  • 5
  • 26
  • 53
Ken Sandoval
  • 363
  • 2
  • 10
  • 23

4 Answers4

4

I realize this is an old post, but I do not see this solution elsewhere.

The problem is not the mlocate.db file, as you'd expect. It is the permissions on the mlocate executable. This will fix the issue:

sudo chmod 2755 /usr/bin/mlocate

This is the source of the trouble:

$ ls -lathr /usr/bin/mlocate 
-rwxr-xr-x 1 root mlocate 39K Nov 17  2014 /usr/bin/mlocate

You need a setuid bit on the group to allow you to run the locate command without sudo. Once it is fixed, your ls output should look like this:

$ ls -lathr /usr/bin/mlocate 
-rwxr-sr-x 1 root mlocate 39K Nov 17  2014 /usr/bin/mlocate

Simply doing a chmod as some others recommend against the db file itself will only work until the next time someone runs updatedb. Then the permissions will be reset.

Martin Thornton
  • 5,151
  • 11
  • 30
  • 39
  • I am not sure how this would be a security risk, as that is actually how it is configured by Ubuntu out of the box. The security risk would be never finding out what changed it to begin with.. :) – Chris Hubbard May 31 '18 at 22:36
3

For me after updates to Ubuntu 14 I still kept getting

$ locate chruby
locate: can not open `/var/lib/mlocate/mlocate.db': Permission denied

so the issue was not about broadening read permissions on that file, unlike other answers say.

My "nonrootuser" is master. So as root I added that user to the group mlocate:

# addgroup master mlocate

After listing the details of that directory to see ownerships:

# ls -al /var/lib/mlocate
drwxr-xr-x  2 root root        4096 Jun 19 13:21 ./
drwxr-xr-x 94 root root        4096 Jan 23 11:27 ../
-rw-r-----  1 root mlocate 50134121 Jun 19 13:21 mlocate.db
Marcos
  • 730
  • 1
  • 7
  • 26
2

Please run the command with sudo:

sudo updatedb
chili555
  • 58,968
  • 8
  • 93
  • 129
  • Using `sudo` is unnecessary when the user is added to the `mlocate` group. – xiota May 30 '18 at 20:06
  • @xiota - It's a super-simple answer, though, and worked without having to add the user to a group (which is often the case, if you're working on a personal computer with no other users.) I enter `sudo ` all day long, but haven't used `updatedb` in a while and forgot that it needed those permissions. Something like, `updatedb: can not open a temporary file for '/var/lib/mlocate/mlocate.db'` often leads me to believe it has nothing to do with permissions and that the file is simply not there (since I don't see that often.) This was the fastest and most efficient answer, in this case. – Steven Ventimiglia Jul 30 '19 at 19:26
  • Something "working" doesn't mean it's a good solution. Running programs as administrator on Windows also "worked", and look how much havoc that wreaked. – xiota Jul 30 '19 at 19:59
-1

Read man updatedb and learn how to write updatedb's output elsewhere, in a directory you have write permission for.

waltinator
  • 35,099
  • 19
  • 57
  • 93