Setting Up NFS Server on Ubuntu 24.04

This article outlines the steps to set up a Network File System (NFS) server on Ubuntu 24.04, enabling file sharing over a network. It covers installing the NFS server, exporting a directory, configuring clients, and mounting the NFS share for access, ensuring effective management of file permissions and security.

This article explains how to set up an NFS server on Ubuntu 24.04.

Network File System (NFS) is a protocol that lets you share files and directories over a network. You can store files on a single server and have multiple clients access them.

With NFS, you can effectively manage permissions for users and groups, ensuring your files are securely shared and protected.

The following steps guide you through the process of setting up NFS on an Ubuntu machine.

Install NFS Server

To set up NFS server on Ubuntu, you must first install its packages.

Run the command below to install the [nfs-kernel-server] package.

sudo apt update
sudo apt install nfs-kernel-server

After installation, please open the configuration file below and add your domain name.

sudo nano /etc/idmapd.conf

Uncomment and change the highlighted line to match your domain name.

[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

Save and exit the file.

Export NFS Share

Exporting an NFS share means making a directory or filesystem on a server accessible to other machines using the Network File System (NFS) protocol.

For this tutorial, we will be exporting a folder in our home directory [/home/nfsshare].

To do that, run the command below to open the NFS exports config file.

sudo nano /etc/exports

Then, add a line to export the directory mentioned.

# /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)

Additional options one can use with NFS exports.

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.

Please set up the NFS share as mentioned earlier, and then restart the NFS server services.

sudo  mkdir /home/nfsshare
sudo systemctl restart nfs-server

Setup NFS Client

Now that the NFS server is installed and configured, you can set up a client to access the shares.

If you haven’t done so, run the command below to install the NFS server client packages on the client computer.

sudo apt install nfs-common

After installation, please open the configuration file below and add your domain name.

sudo nano /etc/idmapd.conf

Uncomment and change the highlighted line to match your domain name.

[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

Save and exit the file.

Mount NFS share on client

You can mount the NFS share created on the server with everything setup by running the command below.

sudo  mount -t nfs srv1.example.com:/home/nfsshare /mnt

To view your file system, run the command below.

df -hT

You should see output similar to the one below.

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

If you want to mount the NFS share automatically every time, open the /etc/fstab file.

sudo nano /etc/fstab

Add a new line at the end of the file.

# /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

Save and exit.

Mount with AutoFS

If you want to mount the NFS share automatically so everyone has access, use the AutoFS tool.

Run the command below to install the tool.

sudo apt install autofs

Once installed, open its master file.

sudo nano /etc/auto.master

Then, add a new line at the end of the file.

/-    /etc/auto.mount

Save and exit.

Next, open the [auto.mount] config file.

sudo nano /etc/auto.mount

Then, add a new line to the file.

# [mount point]  [option]         [location]
/mnt -fstype=nfs,rw srv1.example.com:/home/nfsshare

Save and exit.

Restart AutoFS services.

sudo systemctl restart autofs

Verify the mount.

df -hT /mnt

You should see an output similar to the one below.

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:

In conclusion, setting up an NFS server on Ubuntu 24.04 enables efficient file sharing across a network. Following the outlined steps, you can successfully configure the server and client. Here are the key takeaways:

  • NFS Overview: The network file system (NFS) allows files and directories to be shared over a network.
  • Installation Steps: Use sudo apt install nfs-kernel-server to install the NFS server.
  • Configuration: Modify the /etc/idmapd.conf file to set your domain name and manage user permissions effectively.
  • Exporting Shares: Edit the /etc/exports file to export specific directories, controlling access with various options.
  • Client Setup: Install NFS client packages (nfs-common) and configure the client to access the server shares.
  • Mounting NFS Shares: Use commands like mount or modify /etc/fstab for automatic mounting.
  • AutoFS Tool: For automatic mounts accessible to all users, install and configure AutoFS.

These steps ensure a secure and efficient NFS setup on your Ubuntu system.

Comments

One response to “Setting Up NFS Server on Ubuntu 24.04”

  1. […] How to install and export NFS share on Ubuntu […]

Leave a Reply

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