How to configure NFS on Linux

Sharing files between computers and servers is an essential networking task. Thankfully, NFS (Network File System) is available for Linux systems and makes the job extremely easy. With NFS properly configured, moving files between computers 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 Linux distro, although the configuration can differ slightly between them.

In this guide, we’ll show how to install and configure NFS on major Linux distros, like Ubuntu and others based on Debian, and Fedora and others based on Red Hat. The configuration will involve a server (which hosts the files) and one client machine (which connects to the server to view or upload files). Follow along with the steps below to get NFS setup on your own system.

In this tutorial you will learn:

  • How to install NFS server
  • How to configure NFS server shares
  • How to connect to NFS server from client machines

Configuring an NFS server share on Linux

Configuring an NFS server share on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software NFS
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Set up NFS server



This section of the guide will go over the configuration for the NFS server – in other words, the machine that will be hosting the NFS shares. Client machines can then connect to the server to access and/or upload files.

  1. The first thing we need to do is install the NFS server package, which is available from the main repositories. Use the appropriate command below to install the software on your system.

    On Ubuntu, Linux Mint, and other Debian-based distros:

    $ sudo apt install nfs-kernel-server
    

    On Fedora, CentOS, AlmaLinux, and other RHEL-based distros:

    $ sudo dnf install nfs-utils
    
  2. Next, ensure that the NFS service is running and will start automatically on subsequent machine boots.
    $ sudo systemctl enable --now nfs-server
    
  3. If you don’t already have a directory created that you want to share out, it’s time to create one now. For this example, we’ll store our NFS share as /media/nfs.
    $ sudo mkdir -p /media/nfs
    
  4. Next, we will edit the /etc/exports configuration file. Here, you can configure which directories you’re sharing and who can access them. You can also set specific permissions for the shares to further limit access. Use nano or your favorite text editor to open the file.
    $ sudo nano /etc/exports
    
  5. In the file, each share gets its own line. That line begins with the location of the share on the server machine. Across from that, you can list the hostname of an accepted client, if is available in the server’s hosts file, or an IP or range of IPs. Directly behind the IP address, place the rules for the share in a set of parenthesis. Altogether, it should look something like this:
    /media/nfs		192.168.1.0/24(rw,sync,no_subtree_check)
    


    You can include as many shares as you like, provided each has its own line. You can also include more than one hostname or IP in each line and assign them different permissions. For example:

    /media/nfs		192.168.1.112(rw,sync,no_subtree_check) 192.168.1.121(ro,sync,no_subtree_check)
    

    In the second instance, each of those machines could view and read from the share, but only the computer at 192.168.1.112 could write to it.

  6. Editing the /etc/exports file with our NFS shares and options

    Editing the /etc/exports file with our NFS shares and options

  7. There are plenty more options that you can choose from to configure how the server handles you share for each guest. Here is a complete breakdown of what’s available, but the above configuration will suffice for most generic scenarios.

    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 within 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



  8. Once you have everything set up the way you want, save and exit the file. Then, execute the exportfs command to load your new exports configuration.
    $ sudo exportfs -arv
    exporting 192.168.1.0/24:/media/nfs
    

Your share is now accessible from the client machines that you configured in your /etc/exports file. See the next section for instructions on connecting to the NFS share.

Connect to NFS server from client machine(s)

This section of the guide will show how to use a client machine to connect to the NFS share that we have configured in the previous section.

  1. The first thing we need to do is install the appropriate NFS packages on our system. Use the appropriate command below to install it with your system’s package manager.

    On Ubuntu, Linux Mint, and other Debian-based distros:

    $ sudo apt install nfs-common
    

    On Fedora, CentOS, AlmaLinux, and other RHEL-based distros:

    $ sudo dnf install nfs-utils
    
  2. With the package installed, you’ll be able to mount the NFS share(s). So, to try it out, pick a directory to mount to, and run the mount command as root privileges to mount the networked share. We are specifying the IP of the NFS server in this command, which happens to be 192.168.1.110.
    $ sudo mount -t nfs4 192.168.1.110:/media/nfs /media/share
    
  3. Mounting the NFS share on our client system, and then checking to see our test file that was created on the server

    Mounting the NFS share on our client system, and then checking to see our test file that was created on the server



  4. Provided the mount succeeded, you’ll be able to access your shared files in the directory where you mounted them. For a more permanent solution, you can add the share to your client’s /etc/fstab file. The overall syntax looks a lot like the command that you just used to mount your share. Start with the location of the share on your network. Follow that with where the share is to be mounted. The filesystem type here is nfs4. The options are up to you, but using the defaults and allowing user access are pretty common for non-sensitive shares. The end result should look a bit like the example below.
    192.168.1.110:/media/nfs	/media/share	nfs4	defaults,user,exec	0 0
    

    If you aren’t certain if the share will always be available on the client, add noauto to the list of options to prevent your system from trying to mount it automatically.

    192.168.1.110:/media/nfs	/media/share	nfs4	defaults,user,exec,noauto	0 0
    
  5. Editing the NFS share to /etc/fstab file so it gets mounted automatically

    Editing the NFS share to /etc/fstab file so it gets mounted automatically

  6. To execute the fstab you just edited, run the following mount command.
    $ sudo mount -a
    

    Your share should be mounted exactly where you specified.

Closing Thoughts

Your NFS server is now ready to start serving files, and you shouldn’t have any trouble setting up the rest of your client machines. Remember that NFS doesn’t have much in the way of security, so you’re going to need other methods to restrict access to your files, should you choose to share anything more sensitive.



Comments and Discussions
Linux Forum