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.
Topic: Linux Administration in IT Infrastructure · Lab / project
Also covers: Cloud Computing: AWS, Azure & GCP
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?
- How Do You Set Up the NFS Server?
- How Do You Mount the Share on a Client?
- How Do You Secure an NFS Server?
- How Do You Fix Common NFS Problems?
- References
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?
| Control | Why it matters |
|---|---|
| Specific hosts or subnets in /etc/exports | A wildcard lets any reachable machine mount the share |
| root_squash (default) | Maps remote root to an unprivileged user |
| ro where writing is not needed | Limits damage from a compromised client |
| sec=krb5p | Kerberos authentication plus encryption in transit |
| Firewall port 2049 to known clients | Hides NFS from the wider network and internet |
| File permissions on the export | Server-side permissions still apply to every user |
| Monitoring access | Attackers 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?
| Symptom | Likely cause | Fix |
|---|---|---|
| access denied by server | Client IP not in /etc/exports | Correct the host list, run exportfs -ra |
| Mount hangs | Firewall blocking port 2049 | Open 2049/tcp to the client |
| Permission denied when writing | Directory owner or root_squash mapping | Fix ownership; do not disable root_squash |
| Files show nobody as owner | NFSv4 ID mapping domain mismatch | Match the Domain setting in /etc/idmapd.conf |
WebAsha teaches these skills in its Linux administration training.
References
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0