NFS Server in Linux: A Practical Guide
Setting up an NFS server in Linux is a common task for teams that need centralized file sharing, scalable storage, and predictable performance. Whether you run a small lab, a departmental file store, or a large data center, a properly configured NFS server in Linux can simplify access to shared data while keeping administration straightforward. This guide walks through key concepts, version choices, installation steps, configuration of exports, security considerations, and best practices to help you deploy a reliable NFS server in Linux.
Understanding the NFS server in Linux
At its core, NFS (Network File System) lets a server expose a directory tree that clients can mount and use as if it were local storage. The NFS server in Linux handles requests from clients, enforces access rules, and serves file data over the network. On Linux, the NFS server implements server-side processes and services, including the kernel NFS daemon, various RPC services, and export rules stored in /etc/exports. A typical deployment runs multiple clients that mount one or more exported directories, allowing users and applications to read and write data remotely with a consistent permission model.
Choosing the right NFS protocol version
Deciding which version of NFS to deploy affects compatibility, security, and performance. NFSv4 is widely recommended for new deployments because it is firewall-friendly, stateful, and includes improved access control through ACLs. It also consolidates several RPC services, reducing the number of ports that must be opened in a firewall. NFSv3 remains common in environments with legacy clients or specific performance characteristics, and it can be simpler to configure in mixed OS forests. The NFS server in Linux stack supports both paths, so your choice may come down to client compatibility, security requirements, and whether you need features like Kerberos-based authentication (available in NFSv4 with proper setup). In production, many teams start with NFSv4 and add legacy support only if needed for older clients.
Installing and enabling the NFS server in Linux
Installation steps vary slightly across distributions, but the core concepts are the same. Below are representative commands for common Linux families.
- Debian/Ubuntu
- Install the NFS server package:
sudo apt-get update && sudo apt-get install nfs-kernel-server - Enable and start the service:
sudo systemctl enable --now nfs-kernel-server - Optionally verify the service is active:
systemctl status nfs-kernel-server
- Install the NFS server package:
- RHEL/CentOS/Rocky
- Install the NFS utilities:
sudo yum install nfs-utils - Start and enable:
sudo systemctl enable --now nfs-server - Check status:
systemctl status nfs-server
- Install the NFS utilities:
Once the package is installed and the service is running, the next steps involve configuring which directories to share and with whom. The exports configuration lives in /etc/exports, and the filesystem must be mounted by clients using the appropriate mount options and network permissions.
Configuring exports
The core mechanism for sharing data with the NFS server in Linux is the exports file. Each line defines a directory to export, which hosts may access it, and a set of options that govern behavior. A simple example for NFSv4 or NFSv3 looks like this:
/srv/nfs 192.168.1.0/24(ro,rw,sync,no_subtree_check)
This export allows read-only and read-write access to clients in the 192.168.1.0/24 subnet with synchronous writes and without subtree checks. In practice, you often want more nuanced control:
- Restrict access to specific hosts or subnets
- Choose between ro and rw based on the data served
- Use
syncorasyncdepending on data integrity versus performance needs - Decide on root access behavior with
root_squashorno_root_squash(caution:no_root_squashgrants root privileges on the client) - Enable or disable subtree checks with
subtree_checkorno_subtree_checkto balance performance and security
For NFSv4, the export model adapts to a single pseudo filesystem and uses a separate layout for the actual data. In practice, you’ll configure an export that maps to a directory tree on the server while letting clients access it through a unified namespace. After editing /etc/exports, apply the changes with:
exportfs -ra
To verify what the NFS server in Linux is exporting and which clients can access it, you can run:
exportfs -v
Permissions and security considerations
Security is critical when exposing data over the network. The NFS server in Linux must cooperate with your overall security posture, including file permissions, user IDs, and network access controls. Key considerations include:
- Ensure UID and GID mappings on client systems align with the server’s user accounts
- Prefer
root_squashto prevent remote root users from gaining full control unless you have a compelling reason forno_root_squash - Use proper firewall rules to limit access to the NFS port range (2049 for NFS, plus related RPC ports on older configurations)
- On Red Hat-based systems, adjust SELinux contexts so that exported paths are labeled correctly (e.g.,
semanage fcontext -a -e "/srv/nfs(/.*)?"andrestorecon -Rv /srv/nfs) - Consider upgrading to NFSv4 for more robust ACL support and streamlined firewall behavior
Always test permission changes with representative user accounts and verify that sensitive data remains protected even when a client host is compromised.
Networking and firewall considerations
Open only the ports you need and ensure the services behind them are properly secured. For NFSv4, the server often uses a single port (2049) for the file system, while legacy NFSv3 often requires rpcbind and other ports. If you use a firewall, consider:
- Allowing 2049/tcp and 2049/udp for NFS
- Allowing necessary RPC ports if you’re still using NFSv3 (e.g., 111, 2049, and dynamic ports for rpcbind)
- Enabling the NFS service at boot and ensuring that the firewall rules persist across reloads
- If SELinux is enforcing, labeling and booleans must permit NFS operations (e.g., setsebool -P nfs_export_all_rw on)
Mounting shares on clients
Clients must install the NFS client utilities and mount the exported directories. On a Linux client, steps typically include:
sudo apt-get install nfs-common # Debian/Ubuntu
sudo yum install nfs-utils # RHEL/CentOS
sudo mkdir -p /mnt/nfs
sudo mount -t nfs4 server.example.com:/srv/nfs /mnt/nfs
# For a persistent mount, add to /etc/fstab:
server.example.com:/srv/nfs /mnt/nfs nfs4 rw,_netdev 0 0
When using NFSv4, you’ll often see the domain-based path and a more constrained mount syntax. Always verify you can read and write as the intended users and test with both local and remote users to confirm permissions across the network.
Troubleshooting and maintenance
If users report slow access or failed mounts, start with logs and status checks. Useful commands include:
journalctl -u nfs-serverorjournalctl -u nfs-kernel-serverdepending on your distrosystemctl status nfs-serverorsystemctl status nfs-kernel-server- Client diagnostics:
showmount -e server,mount | grep nfs, orrpcinfo -p server - Verify exports with
exportfs -vand reapply changes withexportfs -ra
Networking issues, mismatched versions, or permission problems are the most common culprits. A methodical check of export rules, firewall state, and user mappings usually resolves most issues with the NFS server in Linux.
Best practices for production deployments
To run a robust NFS server in Linux, consider these best practices:
- Prefer NFSv4 for modern deployments, with a clearly defined domain and ACLs
- Keep the export rules minimal and tightly scoped to trusted networks
- Use autofs to mount shares on demand and reduce boot-time dependencies
- Enable strict firewalls and monitor for unusual access patterns
- Validate backups and ensure that data on NFS shares is included in your disaster recovery plan
- Regularly monitor performance, tweaking rsize/wsize and async/sync settings based on workload
Common pitfalls and how to avoid them
Common issues include misconfigured /etc/exports, mismatched permissions across client and server, and firewall rules that block essential ports. Start with a minimal, well-documented export, verify client connectivity, and then gradually add users and subnets. Always test with representative workloads before moving to production.
Conclusion
With the right version selection, careful export configuration, and deliberate security and network controls, an NFS server in Linux can deliver reliable, scalable file sharing across a mixed environment. This approach helps teams collaborate more efficiently while keeping administration straightforward and secure. By following the steps outlined here and adapting them to your specific Linux distribution and network, you can deploy a robust NFS server in Linux that meets modern requirements for performance and governance.