How to enable local file caching for NFS share on Linux

Last updated on September 22, 2020 by Dan Nanni

If you often mount and access a remote NFS share on your system, you will probably want to know how to improve NFS file access performance. One possibility is using file caching. In Linux, there is a caching filesystem called FS-Cache which enables file caching for network file systems such as NFS. FS-Cache is built into the Linux kernel 2.6.30 and higher.

In order for FS-Cache to operate, it needs cache back-end which provides actual storage for caching. One such cache back-end is cachefiles. Therefore, once you set up cachefiles, it will automatically enable file caching for NFS shares.

In this tutorial, I will describe how to enable local file caching for NFS shares by using cachefiles.

Requirements for Setting Up CacheFiles

One requirement for setting up cachefiles is that local filesystem support user-defined extended file attributes (i.e., xattr), because cachefiles use xattr to store extra information for cache maintenance.

If your local filesystem is EXT4-type, you don't need to worry about this since xattr is enabled in EXT4 by default.

However, if you are using EXT3 filesystem, then you need to mount the local filesystem with user_xattr option. To do so, edit /etc/mtab to add user_xattr mount option to the disk partition that will be used by cachefiles for file caching. For example, assuming that /dev/hda1 is such a partition:

/dev/hda1 / ext3 rw,user_xattr  0 0

After modifying /etc/fstab, reload it by running:

$ sudo mount -o remount /

Set Up CacheFiles

In order to set up cache back-end using cachefiles, you need to install cachefilesd, a userspace daemon for managing cachefiles.

To install cachefilesd on Ubuntu or Debian:

$ sudo apt-get install cachefilesd

To install cachefilesd on CentOS, Fedora or RedHat:

$ sudo yum install cachefilesd
$ sudo chkconfig cachefilesd on

After installation, enable cachefilesd by editing its configuration file as follows.

$ sudo vi /etc/default/cachefilesd

Next, mount a remote NFS share with fsc option:

$ sudo vi /etc/fstab /mnt nfs rw,hard,intr,fsc

Alternatively, if you mount the remote NFS share from the command line, specify fsc as a command-line option:

$ sudo mount -t nfs /mnt -o fsc

Finally, restart cachefilesd:

$ sudo service cachefilesd restart

At this point, file caching should be enabled for the mounted NFS share, which means that previously accessed files in the mounted NFS share will be retrieved from local file cache.

If you want to flush NFS file cache for any reason, simply restart cachefilesd.

$ sudo service cachefilesd restart

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal (Credit Card) or Bitcoin (1M161JGAkz3oaHNvTiPFjNYkeABox8rb4g).

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean