Skip to content
Follow
Ubuntu Linux

Setting Up NFS Server on Ubuntu 24.04

Richard
Written by
Richard
Updated 8 min read
Setting Up NFS Server on Ubuntu 24.04
Setting Up NFS Server on Ubuntu 24.04

Network File System (NFS) lets you share folders between computers on your local network on Ubuntu 24.04 without copying files around on a USB drive. A directory on one Linux machine shows up on another machine as if it were a local folder.

Advertisement

The setup has two halves. On the server, you install the nfs-kernel-server package and choose which folders to share. On each client, you install the client package and mount the shared folder. This tutorial covers both halves, plus two ways to mount automatically at boot or on demand.

⚡ Quick Answer

Install the `nfs-kernel-server` package using `sudo apt install nfs-kernel-server`. Then, export your desired directory by editing `/etc/exports` and restart the service with `sudo systemctl restart nfs-server`.

Advertisement

Install NFS Server

To set up an NFS server on Ubuntu 24.04, you install the nfs-kernel-server package on the machine that will hold the files. You need a user with sudo rights and a working network connection. Two terminal commands do the install: sudo apt update refreshes the package list, and sudo apt install nfs-kernel-server downloads and installs the server software.

  1. Open a terminal on the server.
  2. Run the command below to install the nfs-kernel-server package.

Apt will list the packages it plans to install and ask you to confirm. Press Y and wait for the prompt to return.

Advertisement
🐧Bash / Shell
sudo apt update
sudo apt install nfs-kernel-server

NFS maps user and group names between machines, and both sides should use the same domain name for that. Set it in the configuration file below.

  1. Open the configuration file with the command below, using sudo so you can save changes.
🐧Bash / Shell
sudo nano /etc/idmapd.conf
  1. Find the highlighted line. It is commented out, so remove the comment character at the start.
  2. Change the value to your own domain name. Use the same value on the client later.
💻Code
[General]

Verbosity = 0
# set your own domain here, if it differs from FQDN minus hostname
Domain = example.com

[Mapping]

Nobody-User = nobody
Nobody-Group = nogroup
  1. Save the file and exit the editor. In nano, press Ctrl+O, then Enter, then Ctrl+X.
Advertisement

If you made a mistake, open the file again and fix the line. Put the comment character back to return to the default.

Export NFS Share

An NFS share is a folder on the server that other computers on your network are allowed to use. You list the folders to share, and the rules for who can access them, in the exports file. You edit it from the terminal with sudo nano /etc/exports.

For this tutorial, we will export a folder in our home directory: /home/nfsshare. Make sure the folder exists on the server before you export it.

  1. Run the command below to open the NFS exports config file. The file may be empty or contain only comments. That is normal on a fresh install.
Advertisement
🐧Bash / Shell
sudo nano /etc/exports
  1. Add a line at the end of the file that exports the directory above. Each export goes on its own line. It names the folder, the clients allowed to connect, and the options in brackets.
  2. Save the file and exit the editor.
💻Code
# /etc/exports: the access control list for filesystems which may be exported
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
#
/home/nfsshare 192.168.156.0/24(rw,no_root_squash)

NFS exports accept more options than the ones in the example line. They control things like read-only or read-write access and how the server treats the root user on a client. The table below lists the common ones. Pick the strictest set that still lets your users do their work.

OptionDescription
no_root_squashIt will enable only read requests on a NFS volume.
rwIt allows both read and write requests on a NFS volume.
roIt will enable only read requests on an NFS volume.
syncIt replies to requests only after the changes have been committed to stable storage. (Default)
root_squashIt maps requests from uid/gid 0 to the anonymous uid/gid.
asyncThis option allows the NFS server to violate the NFS protocol and reply to requests before any changes made by that request have been committed to stable storage.
secureThis option requires that requests originate on an Internet port less than IPPORT_RESERVED (1024). (Default)
insecureThis option accepts all ports.

The NFS server only reads the exports file when the service starts or reloads. So once the share is defined as described above, restart the NFS server service.

  1. Run the restart command shown below.
  2. Check that the command finishes without an error message. If it reports an error, the usual cause is a typo in the exports file or a folder that does not exist.
Advertisement
🐧Bash / Shell
sudo  mkdir /home/nfsshare
sudo systemctl restart nfs-server

Setup NFS Client

An NFS client is any computer that connects to the server’s share. Before it can reach shared folders, it needs the nfs-common package. Install it from the terminal with sudo apt install nfs-common. Do this on every machine that will use the share.

  1. Open a terminal on the client computer.
  2. If you haven’t done so already, run the command below to install the NFS client packages.
🐧Bash / Shell
sudo apt install nfs-common

As on the server, set the domain name in the configuration file so it matches on both machines.

  1. Open the configuration file with the command below.
Advertisement
🐧Bash / Shell
sudo nano /etc/idmapd.conf
  1. Uncomment the highlighted line by removing the comment character at the start.
  2. Change the value to your domain name. It must be the same one you used on the server.
💻Code
[General]

Verbosity = 0
# set your own domain here, if it differs from FQDN minus hostname
Domain = example.com

[Mapping]

Nobody-User = nobody
Nobody-Group = nogroup
  1. Save the file and exit the editor.

Mount NFS share on client

Mounting connects the server’s shared folder to a folder on the client. Once it is mounted, you open the local folder and see the remote files. You do this with the sudo mount command, giving it the server address, the share path, and the local folder. The connection is made right away. The local folder must already exist, and anything already in it is hidden while the share is mounted.

🐧Bash / Shell
sudo  mount -t nfs srv1.example.com:/home/nfsshare /mnt
  1. Run the mount command shown above, with your own server address and paths.
  2. Run the command below to view your file system and confirm the share is there.
Advertisement
💻Code
df -hT

You should see output similar to the one below. Look for a line that shows the server address and share path, mounted on your local folder.

💻Code
Filesystem                      Type   Size  Used Avail Use% Mounted on
tmpfs tmpfs 336M 2.1M 334M 1% /run
/dev/sda2 ext4 49G 9.5G 37G 21% /
tmpfs tmpfs 1.7G 0 1.7G 0% /dev/shm
tmpfs tmpfs 5.0M 8.0K 5.0M 1% /run/lock
srv1.example.com:/home/nfsshare nfs4 40G 9.8G 28G 27% /mnt

A manual mount does not survive a reboot. To mount the share every time the client starts, add it to the /etc/fstab file. Make a copy of the file first, because a bad line in it can slow down or interrupt boot.

  1. Open the /etc/fstab file with the command below.
🐧Bash / Shell
sudo nano /etc/fstab
  1. Go to the end of the file and add a new line, as shown below. It uses the same server address, share path, and local folder as your manual mount.
💻Code
# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/3fe5d1b3-fd02-4c71-80df-eaf44cbec7d5 / ext4 defaults 0 1
/swap.img none swap sw 0 0

srv1.example.com:/home/nfsshare /mnt nfs defaults 0 0
  1. Save the file and exit the editor.

To undo this, open the file again and delete the line you added.

Mount with AutoFS

AutoFS connects an NFS share only when someone opens it, and disconnects it again after a period of inactivity. That avoids the problem of a permanent mount hanging when the server is off or unreachable. You install the autofs package, add an entry to the master file, and then define the share in a second file.

  1. Run the command below to install the tool.
🐧Bash / Shell
sudo apt install autofs
  1. Once it is installed, open its master file with the command below.
🐧Bash / Shell
sudo nano /etc/auto.master
  1. Add a new line at the end of the file, as shown below. It tells AutoFS which parent folder to manage and which file holds the mount definitions.
💻Code
/-    /etc/auto.mount
  1. Save the file and exit the editor.
  1. Next, open the auto.mount config file with the command below.
🐧Bash / Shell
sudo nano /etc/auto.mount
  1. Add a new line to the file, as shown below. It names the folder users will open, the mount options, and the server share it points to.
💻Code
# [mount point]  [option]         [location]
/mnt -fstype=nfs,rw srv1.example.com:/home/nfsshare
  1. Save the file and exit the editor.

AutoFS reads its configuration only at startup, so restart the service to apply your changes.

  1. Run the restart command below.
🐧Bash / Shell
sudo systemctl restart autofs
  1. Verify the mount with the command below. The share may not show up until you open the folder, because AutoFS mounts on first access. If it is missing, browse into the folder and run the check again.
💻Code
df -hT /mnt

You should see output similar to the one below, with the server share listed against your local folder.

💻Code
Filesystem                      Type  Size  Used Avail Use% Mounted on
srv1.example.com:/home/nfsshare nfs4 40G 9.8G 28G 27% /mnt

That should do it!

Conclusion:

Setting up an NFS server on Ubuntu 24.04 gives you efficient file sharing across your network. If you followed the steps above, both the server and the client should now be working. Here are the key takeaways:

  • NFS OverviewThe network file system (NFS) allows files and directories to be shared over a network.
  • Installation StepsUse sudo apt install nfs-kernel-server to install the NFS server.
  • ConfigurationModify the /etc/idmapd.conf file to set your domain name and manage user permissions effectively.
  • Exporting SharesEdit the /etc/exports file to export specific directories, controlling access with various options. Restart the NFS server service after each change.
  • Client SetupInstall NFS client packages (nfs-common) and configure the client to access the server shares.
  • Mounting NFS SharesUse commands like mount or modify /etc/fstab for automatic mounting.
  • AutoFS ToolFor automatic mounts accessible to all users, install and configure AutoFS. It mounts the share only when someone opens it.

These steps give you a secure and efficient NFS setup on your Ubuntu system. Share only the folders you need, and keep the export options as strict as your use case allows.

Have a question about this guide?

Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.

Did this guide fix your problem?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.

Advertisement

1 Comment

Leave a Comment

Your email address will not be published. Required fields are marked *