Setting Up The Server
Installing The PackagesLinux NFS uses the Client-Server model, so the first step in getting NFS set up is setting up the server. Because the core NFS capabilities are rooted in the kernel, there isn’t much required in the way of packages, but there are still a few regardless of the distribution as well as some configuration. Almost all major distributions have NFS enabled, so unless you’re running a custom one, it should already be set up. The next step in getting the server set up is to install the packages.
$ sudo apt-get install nfs-kernel-headersOn Fedora
$ sudo yum install nfs-utils system-config-nfs
Configuring ExportsOnce the packages are finished installing, it’s time to configure the exports file. The exports file specifies which directories the server will… export to the network. The structure of the file is fairly simple. To the left are the directories which should be shared, and to the right are the IP addresses and subnets of the machines they should be shared with along with any specific options. It looks something like this:
/export 192.168.1.0/255.255.255.0(rw,sync,no_subtree_check) /home/user/shared 192.168.1.122/255.255.255.0(rw,sync,no_subtree_check)In the first example, the directory
/exportis being shared to all computers on that particular subnet. By specifying an address of
192.168.1.0the “0” acts as a wildcard for any IP on the subnet. The second example is similar, but it specifies that only that address can access the shared directory. There are only a few options available for NFS shares.
- ro: specifies that the directory may only be mounted as read only
- rw: grants both read and write permissions on the directory
- no_root_squash: is an extremely dangerous option that allows remote “root” users the same privilege as the “root” user of the host machine
- subtree_check: specifies that, in the case of a directory is exported instead of an entire filesystem, the host should verify the location of files and directories on the host filesystem
- no_subtree_check: specifies that the host should not check the location of the files being accessed withing the host filesystem
- sync: this just ensures that the host keeps any changes uploaded to the shared directory in sync
- async: ignores synchronization checks in favor of increased speed
Running With SystemdOnce the exports file is ready to go, the server can be started and made to start on boot with Systemd. Of course, if you happened to be running a non-Systemd distro, it will work with other init systems too. There are two parts to starting the NFS server, rpcbind and nfs-server. Start them both with:
$ sudo systemctl start rpcbind $ sudo systemctl start nfs-serverOn Debian and Ubuntu, the server will have started automatically, so you may need to run:
$ sudo systemctl restart rpcbind $ sudo systemctl restart nfs-serverAfter that, the server will be up and running. Once again, since Debian and Ubuntu run services at boot by default, nothing is needed to make NFS run automatically at boot. To get NFS to start on boot with Fedora run:
$ sudo systemctl enable rpcbind
$ sudo systemctl enable nfs-serverAdditional security can be implemented with the hosts file Portmap, but that’s a bit out of the scope of this basics article. The above setup provides an excellent starting point and will work well for home networks.
Connecting The ClientSetting up a client to connect to an NFS share requires much less effort. Again, for both the debian distros and the
$ sudo apt-get install nfs-common rpcbindFedora:
$ sudo yum install nfs-utilsOnce that’s done start, or in the case of Debian/Ubuntu restart, rpcbind.
$ sudo systemctl (re)start rpcbindOf course, like with the server, it’s probably a good idea to enable rpcbind to run at startup. This is even more of a concern with a client that will have to it be able to mount remove volumes at boot. Actually mounting a NFS volume is nearly the same as mounting a local one. For a one time mount, the syntax is the same. To mount the NFS directory
/exportlocate on the server at
188.8.131.52to the local directory
# mount 192.168.1.15:/export /media/nfs-volumeNFS volumes can be mounted on boot, or have a mount point designated using
/etc/fstab. To mount the same volume as above automatically at boot, add the following line to
192.168.1.15:/export /media/nfs-volume nfs defaults,user,exec 0 0The options specified will allow a user access to the directory and allow execution on the directory. If you don’t want the drive to be mounted at boot, but still available in
noautooption. For how little it takes to set up a basic NFS configuration, it can be a very useful tool for sharing data between Linux computers on a network.