Sharing files between computers and servers is an essential networking task. Thankfully, Linux’s NFS(Networked File System) makes it extremely easy. With NFS properly configured, moving files between machines is as easy as moving files around on the same machine. Since NFS functionality is built directly into the Linux kernel, it is both powerful and available on every distro, though the configuration differs slightly between them.
Setting Up The Server
Installing The Packages
Linux 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-headers
$ sudo yum install nfs-utils system-config-nfs
Once 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
/export is being shared to all computers on that particular subnet. By specifying an address of
192.168.1.0 the “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 Systemd
Once 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-server
On Debian and Ubuntu, the server will have started automatically, so you may need to run:
$ sudo systemctl restart rpcbind $ sudo systemctl restart nfs-server
After 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-server
Additional 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 Client
Setting 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 rpcbind
$ sudo yum install nfs-utils
Once that’s done start, or in the case of Debian/Ubuntu restart, rpcbind.
$ sudo systemctl (re)start rpcbind
Of 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
/export locate on the server at
126.96.36.199 to the local directory
# mount 192.168.1.15:/export /media/nfs-volume
NFS 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 0
The 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
/etc/fstab add the
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.