NFS Server Setup on Linux: Step-by-Step Guide to Share Files Securely

To set up an NFS server on Linux, install the server package (nfs-kernel-server on Ubuntu and Debian, nfs-utils on RHEL, Rocky and AlmaLinux), create a directory to share, list it in /etc/exports with the client network and options such as rw, sync and root_squash, apply the change with exportfs -ra, start the NFS service and allow NFS through the firewall.

Jul 02, 2025 - 09:47
Updated: 
103.4k

Linux Administration in IT Infrastructure · Lab / project

Cloud Computing: AWS, Azure & GCP

NFS Server Setup on Linux: Step-by-Step Guide to Share Files Securely

Quick answer: To set up an NFS server on Linux, install the server package (nfs-kernel-server on Ubuntu and Debian, nfs-utils on RHEL, Rocky and AlmaLinux), create a directory to share, list it in /etc/exports with the client network and options such as rw, sync and root_squash, apply the change with exportfs -ra, start the NFS service and allow NFS through the firewall. On the client, install the NFS client tools and mount the share with mount -t nfs. Limit exports to specific hosts, prefer NFSv4, and use Kerberos (sec=krb5p) when data is sensitive, because plain NFS trusts client IP addresses.

Key takeaways

  • NFS lets Linux machines mount a remote directory as if it were local.
  • /etc/exports controls who can mount what, and with which permissions.
  • Never export to everyone with a wildcard on an untrusted network.
  • Keep root_squash on so remote root cannot act as root on the server.
  • NFSv4 with Kerberos adds real authentication and encryption.

Table of Contents

What Is NFS and Which Version Should You Use?

Network File System (NFS) is a protocol for sharing directories over a network. NFSv4 (RFC 7530) uses a single TCP port, 2049, and supports strong authentication. NFSv4.1 (RFC 8881) and NFSv4.2 (RFC 7862) add sessions and features such as server-side copy. Older NFSv3 needs extra services such as rpcbind and is harder to firewall. Use NFSv4.1 or later unless an old client forces otherwise.

How Do You Set Up the NFS Server?

Step 1. Install the packages.

# Ubuntu or Debian
sudo apt update && sudo apt install nfs-kernel-server
# RHEL, Rocky, AlmaLinux
sudo dnf install nfs-utils

Step 2. Create the shared directory.

sudo mkdir -p /srv/nfs/share
sudo chown nobody:nogroup /srv/nfs/share   # use nobody:nobody on RHEL

Step 3. Add an export. Edit /etc/exports and add one line per share. The options are documented in exports(5).

/srv/nfs/share 192.168.10.0/24(rw,sync,no_subtree_check,root_squash)

Step 4. Apply and start.

sudo exportfs -ra
sudo exportfs -v
sudo systemctl enable --now nfs-kernel-server   # Ubuntu or Debian
sudo systemctl enable --now nfs-server          # RHEL family

Step 5. Open the firewall only to the client network.

sudo ufw allow from 192.168.10.0/24 to any port 2049 proto tcp
sudo firewall-cmd --permanent --add-service=nfs && sudo firewall-cmd --reload

On firewalld, also restrict the source network using a zone.

How Do You Mount the Share on a Client?

sudo apt install nfs-common        # or: sudo dnf install nfs-utils
sudo mkdir -p /mnt/share
sudo mount -t nfs4 192.168.10.5:/srv/nfs/share /mnt/share
df -h /mnt/share

To mount at boot, add this line to /etc/fstab. Mount options are listed in nfs(5).

192.168.10.5:/srv/nfs/share /mnt/share nfs4 defaults,_netdev 0 0

How Do You Secure an NFS Server?

ControlWhy it matters
Specific hosts or subnets in /etc/exportsA wildcard lets any reachable machine mount the share
root_squash (default)Maps remote root to an unprivileged user
ro where writing is not neededLimits damage from a compromised client
sec=krb5pKerberos authentication plus encryption in transit
Firewall port 2049 to known clientsHides NFS from the wider network and internet
File permissions on the exportServer-side permissions still apply to every user
Monitoring accessAttackers collect data from shares (ATT&CK T1039)

Without Kerberos, NFS trusts the user IDs a client claims, so anyone with root on an allowed client can read files owned by other IDs. The Ubuntu NFS guide includes Kerberos setup. Our guides on securing a Linux server and Linux file permissions cover the rest of the host. Scan only servers you own or have written permission to test, as the IT Act 2000 requires.

How Do You Fix Common NFS Problems?

SymptomLikely causeFix
access denied by serverClient IP not in /etc/exportsCorrect the host list, run exportfs -ra
Mount hangsFirewall blocking port 2049Open 2049/tcp to the client
Permission denied when writingDirectory owner or root_squash mappingFix ownership; do not disable root_squash
Files show nobody as ownerNFSv4 ID mapping domain mismatchMatch the Domain setting in /etc/idmapd.conf

WebAsha teaches these skills in its Linux administration training.

References

Frequently Asked Questions

The nfs-kernel-server package. On RHEL, Rocky Linux and AlmaLinux the equivalent package is nfs-utils, and the service is called nfs-server.

NFSv4 uses TCP port 2049. Older NFSv3 also needs rpcbind on port 111 and other services, which makes it harder to firewall safely.

It maps requests from the root user on a client to an unprivileged account on the server, so remote root cannot take over exported files.

Run sudo exportfs -ra to re-export all shares, then sudo exportfs -v to confirm the active exports and their options.

Not by default. Use NFSv4 with Kerberos and the sec=krb5p option to encrypt traffic, or keep NFS on a trusted, isolated network segment.

Use NFSv4.1 or later for new setups. It needs one port, supports stronger security, and is the default on current Linux distributions.

Add an entry to /etc/fstab with the server path, mount point, nfs4 type and the _netdev option so mounting waits for the network.

Usually the NFSv4 ID mapping domain differs between client and server. Set the same Domain value in /etc/idmapd.conf on both machines.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.