Easily Share Files Between Linux Systems with NFS

Download Article
Simple instructions to set up an NFS server on Linux and connect a client computer
Download Article

NFS (Network File System) makes it easy to share files and directories from a Linux server to other Linux systems on the network. To set up file sharing with NFS, you'll need to install the NFS server software on the host system and the client software on other Linux systems that need access to the files. Then, users on allowed systems can access directories shared from the host system. This How.com.vn article will teach you how to set up NFS to share files between Linux computers.

Things You Should Know

  • To share files between Linux computers using NFS, start by installing "nfs-kernel-server" (Ubuntu/Debian) or "nfs-utils" (Red Hat/Fedora) on the server.
  • Create the directory you want to share, and add it to /etc/exports along with the hostnames/IP addresses that should have access.
  • Install the NFS client package on the other Linux systems, mount the shared directory, and add it to /etc/fstab.
Section 1 of 2:

Creating the Server

Download Article
  1. How.com.vn English: Step 1 Install the NFS server on the host system.
    You can install the latest version of NFS on your Linux server (NFS 4) using your distribution's package manager.
    • For example, if you're using Ubuntu or another Debian-based Linux, run sudo apt install nfs-kernel-server in a terminal to install the NFS server package.[1]
    • On a Red Hat -based Linux server, use sudo dnf install nfs-utils.[2]
  2. How.com.vn English: Step 2 Start the NFS server.
    Because NFS is a systemd service, you'll use systemctrl to start it. Run this command:
    • Ubuntu/Debian: sudo systemctl start nfs-kernel-server.service
    • Red Hat/Fedora/CentOS: sudo systemctl enable --now nfs-server
    • Note that beginning with NFS 4, it's no longer necessary to use the rcpbind service with NFS.
    Advertisement
  3. How.com.vn English: Step 3 Create a directory for sharing files (optional).
    If you're using NFS to store users' home directories on a central server instead of on individual clients, you can skip this step. If you want to store other general files in a directory that's accessible to other Linux systems on the network, you can create a directory for your NFS share. Here's how:
    • Let's say you want to create a directory at /var/nfs/employees for files that employees need to access on their Linux systems. To create that directory, use sudo mkdir -p /var/nfs/employees.
      • Using sudo ensures that the directory is owned by root on the server. This will prevent users with root access on their own systems from having root access on the server directory.
    • Then, change the group ownership of the directory using sudo chown nobody:nogroup /var/nfs/employees to match how NFS translates any root operations on the client system. However, don't change group ownership of /home if you plan to also share home directories via NFS unless you want to break things.
    • You can create multiple directories to share if you'd like. You can share as many directories as you need to.
  4. How.com.vn English: Step 4 Open /etc/exports for editing.
    Directories listed in /etc/exports are those you'll be sharing with NFS. You can use any text editor, such as Vim or Nano, to edit the file.
    • Use sudo to edit the file. E.g., sudo vim /etc/exports.
    • If you're setting up NFS for the first time, this file won't exist yet. Don't worry if it's blank.
  5. How.com.vn English: Step 5 List each directory you want to share from this server.
    The syntax you'll use is <directory> <host(s) to share with>(<options>). Here are some examples:
    • /var/nfs/employees*.internal.wikihow.com(ro,sync,subtree_check)
      • In this example, the directory /var/nfs/employees can be mounted on any host at *.internal.wikihow.com with read-only (ro) permissions. subtree_check double-checks permissions of subdirectories before mounting.
    • /home192.168.122.0/24(rw,no_subtree_check)
      • In this example, users' home directories can be mounted with read/write permissions by any client with an IP address on the 192.168.122.* network. no_subtree_check disables a security measure that can slow down access to home directories. It's best to only use subtree_check for read-only filesystems.[3]
    • To view a list of all options and how they work, check out https://linux.die.net/man/5/exports.
  6. How.com.vn English: Step 6 Apply your configuration changes.
    When you make changes to /etc/exports, run the command sudo exportfs -ar to make them active.
  7. How.com.vn English: Step 7 Allow NFS through...
    Allow NFS through your firewall. Before you can mount NFS directories on client systems, you'll need to ensure the NFS service is allowed through your firewall. The commands will vary depending on the type of firewall you have.
    • For example, if you're using firewalld, use sudo firewall-cmd --add-service nfs –permanent.
    • For ufw, you can use sudo ufw allow from <client_ip> to any port nfs.
  8. Advertisement
Section 2 of 2:

Connecting Client Computers

Download Article
  1. How.com.vn English: Step 1 Install NFS on the client systems.
    Each Linux system on your network that needs access to the shared files must install the NFS client.
    • Ubuntu/Debian: sudo apt install nfs-common
    • Red Hat, Fedora, CentOS: Use the same command you used to install NFS on the server sudo dnf install nfs-utils
  2. How.com.vn English: Step 2 Create a directory to mount the shared files.
    You can name this whatever you'd like. For example, you can type sudo mkdir /nfs to create a folder called "nfs."
    • In our previous example, we created /var/nfs/employees on the server system. If we want to mount that directory at /nfs/employees on the client system, we'll also need to sudo mkdir /nfs/employees locally.
    • Create a directory even if you're mounting home directories from the NFS server. E.g., you can create /nfs/home on this system.
  3. How.com.vn English: Step 3 Mount the shared directory to the client system.
    To do this, you'll use the mount command. In this example, we'll mount /var/nfs/employees from our server, whose IP address is 192.168.0.5, to the /nfs/employees directory on the client system:
    • sudo mount 192.168.0.5:/var/nfs/employees /nfs/employees
  4. How.com.vn English: Step 4 Open /etc/fstab for editing.
    To ensure the files shared from the NFS server are mounted automatically at boot time, we'll want to add the mounts to /etc/fstab on the client systems. You can use any text editor, such as Vim, Nano, or Pico, to edit the file.
    • Use sudo to edit the file. E.g., sudo vim /etc/fstab.
  5. How.com.vn English: Step 5 Add the NFS server name and directory to /etc/fstab.
    Add a separate entry for each directory you want to mount. The syntax is serverIP:sharedDirectory nfs rsize=8192,wsize=8192,timeo=14,intr. For a full explanation of the different values, use man nfs at the prompt.
    • Replace serverIP with the IP address or hostname of the NFS server.
    • Replace sharedDirectory with the local NFS mount point (e.g., /nfs/home).
    • Using the above examples, the line might look like: 192.168.1.5:/export/Shared /sharedFiles nfs rsize=8192,wsize=8192,timeo=14,intr.
  6. How.com.vn English: Step 6 Test the mount.
    To make sure the shared files are mounted on the client system, type mount -a to view all mount points. You should now be able to access files from the server system on the client.
  7. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      About This Article

      How.com.vn English: Nicole Levine, MFA
      Written by:
      How.com.vn Technology Writer
      This article was co-authored by How.com.vn staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for How.com.vn. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 272,595 times.
      How helpful is this?
      Co-authors: 16
      Updated: November 20, 2023
      Views: 272,595
      Categories: Linux
      Thanks to all authors for creating a page that has been read 272,595 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement