Skip to main content

Getting started with NFS

These basics can get you started using NFS to make files accessible to another computer.
Image
Organized papers

The Network File System (NFS) has been around a long time. It was developed in 1984 at Sun Microsystems. Version 3 was released to the public in 1995 and is still widely used today, though with many improvements. Sun turned NFS over to the IETF, and they released version 4 in 2000 (which has also seen many improvements since it was first released). In this current age of such rapid development and advanced technology, you might ask yourself: "Of what use is such a well-heeled technology?"

NFS was created to allow users on one computer to access files on another. This concept still sounds quaint in the age of the internet, but maybe you don’t need the internet. Maybe you just need a central repository for files that can be simply and easily shared. Unlike CIFS/SMB (Samba), NFS can be managed with *nix access control. Unlike FTP, NFS can be mounted locally and accessed without having to reconnect each time it’s needed. And NFS version 4 works with LDAP and Kerberos to allow centralized, secure authentication.

For this discussion, we’ll talk about the basics, meaning what you need to get started with NFS. First, you’ll need to make sure you have the right packages installed and enabled. One of the good things about having been around for so long, and being so widely used, is that NFS support has been "baked into" the Linux kernel for some time. The necessary packages usually come already installed.

Getting NFS running

To be sure that you have NFS installed on your Red Hat Enterprise Linux or CentOS (version 7 or later), run the following:

# yum install -y nfs nfs-utils nfs-secure

Note: These packages don’t always all need to be installed separately. It depends on the version of your OS.

Next, make sure it’s enabled and started:

# systemctl enable nfs nfs-server
# systemctl start nfs nfs-server

Note: Again, you won’t always need to run both of these commands. It depends on your specific version.

Sharing over NFS

Next, let’s create a directory on the server and share it. I like to create a directory for NFS and make folders under that directory to keep things organized:

# mkdir -p /nfs/enable

Then, in your favorite text editor, edit the file /etc/exports. Add this line:

/nfs/export/enable *(rw)

Save the file and run this command:

# exportfs -rv

The result should show that you are now exporting the /nfs/export/enable directory.

Let’s take a minute to look at that export line. On the left, we can see what we are exporting. On the right, we can see that we exported that to the whole world (*) and made it both readable and writable. It’s important to be aware of this issue because it won’t be that often that you’ll want to make things that easy for bad guys. You might, for example, want to limit that to just a certain subnet and make it read-only:

/nfs/export/enable 192.169.0.* (ro,async)

There are lots of ways you can set this feature up. I encourage you to look at the exports man page if you want to learn more about your options. And as I said before, with NFS version 4 you can use LDAP and Kerberos to secure and control access, but that’s for another article.

Accessing NFS shares

Now what? Well, let’s go to your client computer and make the directory that will contain the files on your server when we connect via NFS:

# mkdir -p /nfs/import/enable

Now, mount the remote filesystem locally using this command:

# mount -v -t nfs3 server://nfs/export/enable /nfs/import/enable

Note: Here, we specify nfs3. The reason for this choice is that for nfs4, we need to have some other things set up as well (which are, again, for another article).

Now you should be able to change to that directory and see the files on the remote machine.

Making that access permanent

To make this connection a "regular thing" and mount that remote filesystem automatically, open /etc/fstab in your favorite editor and add this line:

server://nfs/export/enable /nfs/import/enable nfs3 _netdev,rw,defaults 0 0

The _netdev is important. Otherwise, when your client machine boots, it will hang if there’s a problem reaching the network.

Wrapping up

As I already mentioned, it’s important to note that this is a basic, simplified setup. But, this information should be enough to get you started testing different configurations and uses, and then putting this useful tool to work for you. For example, I use NFS at home to make a large, centralized storage server available on all of the machines in my house, including my wife’s Microsoft laptop (it used to be the case that Microsoft did not support NFS, and in fact made it difficult to use, but they’ve since made Windows more NFS-friendly).

Topics:   Networking   Storage  
Author’s photo

Glen Newell

Glen Newell has been solving problems with technology for 20 years. As a Systems Engineer and administrator, he’s built and managed servers for Web Services, Healthcare, Finance, Education, and a wide variety of enterprise applications. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.