To configure an NFS server, you install the NFS server package, create a directory to share, define the share in the /etc/exports file, and start the NFS services. This guide walks through the full process on current Linux distributions, covering permissions, security options, and client-side mounting.
Background: What NFS Is and Why It’s Used
Network File System (NFS) is a distributed file system protocol originally developed by Sun Microsystems in 1984. It lets a computer access files over a network as if they were on its local disk. NFS is widely used in enterprise environments, home labs, and cloud infrastructure because it is simple, fast, and supported by all major Linux distributions and Unix-like systems.
NFS works on a client-server model: the server exports one or more directories, and clients mount those directories into their own file trees. The protocol has evolved through several versions; NFSv4 and NFSv4.2 are the current standards, offering better security, locking, and performance than older v2 and v3.
Prerequisites and Initial Setup
Before configuring the server, ensure you have:
- A Linux server with a static IP address (or a hostname resolvable by clients).
- Root or sudo access on the server.
- Firewall rules allowing NFS traffic (typically ports 2049/TCP for NFS itself, plus portmapper on 111 and related services).
- Clients that are on a trusted network or that you can restrict via export options.
On Debian/Ubuntu systems, install the server package with:
sudo apt update
sudo apt install nfs-kernel-server
On RHEL/CentOS/Fedora, use:
sudo dnf install nfs-utils
After installation, enable and start the service (on systemd systems):
sudo systemctl enable --now nfs-server
Creating and Exporting a Shared Directory
Decide which directory to share. For example, to share /srv/nfs/share:
- Create the directory:
sudo mkdir -p /srv/nfs/share - Set ownership and permissions:
sudo chown nobody:nogroup /srv/nfs/share(or a user of your choice, depending on how you want permissions to map). - Edit the exports file:
sudo nano /etc/exports - Add a line like:
/srv/nfs/share 192.168.1.0/24(rw,sync,no_subtree_check) - Apply the exports:
sudo exportfs -ra
The exportfs -ra command re-reads the exports file and applies changes without restarting the service.
Understanding /etc/exports Options
The exports file controls who can access what and how. Common options include:
rw– read-write access;ro– read-only.sync– writes are committed to disk before the server replies (recommended for data integrity).no_subtree_check– disables subtree checking, improving reliability when files are renamed or deleted.no_root_squash– allows root on the client to have root on the server (dangerous, use only when necessary).root_squash– maps root on the client to the anonymous user (nobody) by default; this is the default and safer.all_squash– maps all users to the anonymous user.anonuid/anongid– specify the UID/GID for anonymous users.
For example, to share with a specific host read-only and squash all users to UID 1000:
/srv/nfs/share 192.168.1.50(ro,all_squash,anonuid=1000,anongid=1000)
Security Considerations and Firewall Configuration
NFS was not designed with strong security in mind. Always restrict exports to specific IP ranges or hostnames, never use * unless you are on an isolated network. Use root_squash by default to prevent root on clients from having root on the server.
For firewalls (using firewalld on RHEL or ufw on Ubuntu), allow NFS services. On RHEL:
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --permanent --add-service=mountd
sudo firewall-cmd --permanent --add-service=rpc-bind
sudo firewall-cmd --reload
On Ubuntu with ufw:
sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw allow from 192.168.1.0/24 to any port 111
sudo ufw allow from 192.168.1.0/24 to any port 2049
If you use NFSv4 only, you can often skip opening port 111 and the rpcbind services, since NFSv4 does not require them for mount operations.
Client-Side Mounting and Testing
On the client, install the NFS client package (usually nfs-common on Debian/Ubuntu, nfs-utils on RHEL). Then mount the share:
sudo mkdir -p /mnt/nfs
sudo mount -t nfs 192.168.1.100:/srv/nfs/share /mnt/nfs
To make the mount persistent across reboots, add an entry to /etc/fstab:
192.168.1.100:/srv/nfs/share /mnt/nfs nfs defaults,timeo=50,retrans=2 0 0
Test by creating a file on the server and reading it from the client, and vice versa. Use df -h to confirm the mount.
Troubleshooting Common Issues
If clients cannot mount, check the following:
- Is the export list correct? Run
showmount -e server_ipon the client to see available exports. - Are firewall rules allowing traffic? Test with
telnet server_ip 2049ornc -vz server_ip 2049. - Are permissions correct on the shared directory? The client needs execute permissions on the path components.
- Is SELinux blocking access on RHEL? Check
getenforce; if enforcing, set the booleannfs_export_all_rwto on. - For NFSv4, ensure the server is set to NFSv4 only if you intend that; otherwise, use
vers=3orvers=4in the mount options.
A common misconception is that NFS is inherently slow. In practice, with modern kernels and NFSv4.2, performance is comparable to local disk for many workloads, but latency and network bandwidth matter. Use rsize=1048576,wsize=1048576 mount options to increase throughput on fast networks.
Frequently Asked Questions
How do I make an NFS share permanent?
Add the export line to /etc/exports and run exportfs -ra. On the client, add a line to /etc/fstab with the server IP and path. Ensure the NFS service is enabled at boot (systemctl enable nfs-server).
What is the difference between NFSv3 and NFSv4?
NFSv4 integrates file locking, mount protocol, and security negotiation into one protocol, using TCP by default. It also supports stateful operations, which improve crash recovery. NFSv3 is stateless and often requires additional services like rpcbind and lockd. For new setups, use NFSv4 or NFSv4.2.
Why is my client getting ‘Permission denied’?
Check the exports file for the correct network or hostname. Ensure the directory permissions allow the user on the client. If using root_squash, the client root is mapped to nobody, so the directory must be accessible by nobody. Also verify SELinux or AppArmor is not blocking.
Can I share the same directory to multiple clients?
Yes, list multiple networks or hostnames separated by spaces in the exports file. For example: /srv/nfs/share 192.168.1.0/24(rw) 10.0.0.0/24(rw). Each client can mount the share independently.
How do I change the NFS port to a custom one?
NFSv4 uses port 2049 by default. To change it, edit the NFS configuration file (e.g., /etc/nfs.conf) and set nfsd_port to a custom port. You must also update the firewall and client mount options accordingly. However, this is rarely needed and can complicate setup.
Is NFS secure over the internet?
No, standard NFS is not encrypted or authenticated strongly. For internet access, use SSHFS, VPN, or NFS over Kerberos (sec=krb5p) which provides encryption. For local networks, NFS is generally acceptable if you trust the network.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Delete Google Search Results About You
- OnePlus Buds Pro 3 Price Drop: Are They Worth It?
- Cloud Migration Ways to Prepare
- Gadgets After Support Ends: Do They Improve?
- Remote Work Productivity Hacks That Actually Work
- Epic Games Removes Dark and Darker? Fact Check