What is NFS ?
NFS, Network File System, is a distributed file system protocol used for sharing files and directories over a network. The files and directories can be accessed as if they were located on the remote computer.
Installing NFS server
The following core packages need to be installed:
| Package | Description | |
|---|---|---|
nfs-kernel-server | Debian based distros only | Main NFS server package on Debian based distributions. |
nfs-utils | Red Hat based distros only | NFS server components and client utilities. |
rpcbind | Only for legacy NFSv2 & NFSv3 | Central mapping service for older NFS versions. This is replaced by a well-known port, 2049/tcp, in NFSv4. See NFSv4 Only Server |
sudo apt-get updatesudo apt-get install nfs-kernel-server rpcbind -ysudo yum updatesudo yum install nfs-utils rpcbind -yOnce the packages are installed, you can start the NFS server service.
sudo systemctl start nfs-serversudo systemctl start rpcbindOptionally, you may want to configure the service to auto-start on boot.
sudo systemctl enable nfs-serversudo systemctl enable rpcbindSharing Directories with NFS
Files or folders to be accessed by NFS clients are configured in the file /etc/exports.
This file is maintained by the system administrator.
Lines beginning with a # are treated as comments while empty lines are ignored.
A basic directory entry is in the following format:
[folder_path] [network_address](export_options)NOTEThere is no space between
network_addressandexport_options
Multiple allowed network addresses can also be specified for a single directory entry as follows:
[folder_path] [network_address_1](export_options) [network_address_2](export_options)| Value | Optional | Description |
|---|---|---|
| folder_path | N | Absolute path in the server where the folder is located. |
| network_address | N | An ip or network allowed to access the shared folder. see network address formats |
| export_options | Y | A list of options that specify access levels granted to nfs clients. see export options |
Network Addresses
IP addresses, Network addresses and Domain names are accepted values in the configuration file.
Wildcard characters, *, ?, and character classes [] can be used to specify the network address.
The following are examples of accepted network address entries:
- server1.mydomain.local
- 192.168.101.100
- 192.168.101.0/24
- *.mydomain.local
- client_?.mydomain.local
- client_[123].mydomain.local
| character | Description |
|---|---|
* | Matches any sequence of characters. |
? | Matches any single character. |
[] | matches any character within the brackets. |
Export Options
Here are some common NFS options that are key when troubleshooting NFS issues.
| option | Description |
|---|---|
| ro | Clients have read only access to files on the share. |
| rw | Clients have read-write access to files on the share. |
| sync | Forces the server to write to disk before replying to client. |
| async | Forces the server to reply to client even before changes are fully flushed to disk. |
| root_squash | Maps the root user IDs on client machine to an anonymous user ID. This prevents the client’s root user from having root privileges on the NFS server’s file system. |
| no_root_squash | Disables root_squash |
| all_squash | Maps all client’s user IDs including root to an anonymous user ID. This is useful for read-only shares. |
| anonuid= | Sets the UID for the anonymous user. |
| anongid= | Sets the GID for the anonymous user. |
Sample exports file
# allow clients by ip/folders/myfolder1 192.168.22.10(rw,anoguid=34,anouid=34)/folders/myfolder1 192.168.22.11(rw,anoguid=34,anouid=34)
# allow all ips in a network/folders/myfolder2 192.168.22.0/24(rw,anoguid=12,anouid=12)
# allow multiple networks/folders/myfolder3 192.168.22.0/24(rw) 192.168.23.0/24(rw,anoguid=12,anouid=12)
# using patterns to specify network address/folders/myfolder4 192.168.*.0/24(rw) 192.168.23.0/24(rw)Reloading the exports file
Any time the file, etc/exports, has been modified, it needs to be reloaded.
# reload exports filesudo exportfs -r
# view exported directory and file entriessudo exportfs -vMounting NFS Shares in Linux
Only clients specified, or match the rules specified, in /etc/exports can mount shared NFS directories.
NFS client package must be installed to mount NFS directories: nfs-utils in Red Hat based distros and nfs-common in Debian based distros.
sudo apt-get updatesudo apt-get install nfs-common -ysudo yum updatesudo yum install nfs-utils -yOnce the packages are installed, the NFS share can be mounted using mount command.
mount -t nfs nfs_server:folder_path mount_point -o mount_options
| Value | Description |
|---|---|
| nfs_server | NFS server address. |
| folder_path | Shared directory in NFS server. |
| mount_point | Location to mount the NFS share in local server or client. |
| mount_options | A comma-separated list of options to control how the mount behaves. |
NOTEThe mount_point must be a valid path in the client.
# mount /folders/myfolder1 shared by 168.10.100 at /mount_pointmount -t nfs 192.168.10.100:/folders/myfolder1 /mount_pointNFS Mount Options
Here are some common mount options:
| option | Description |
|---|---|
| nfsvers= | Force the use of a specific NFS version. |
| tcp | Force TCP protocol to be used. |
| udp | Force UDP protocol to be used. |
| nosuid | Disables set-user-ID and set-group-ID bits. |
| noexec | Prevents execution of executables on the mounted file system. |
| rsize= | Read block size. |
| wsize= | write block size. |
Mounting NFS shares on system startup
NFS shares can also be automatically mounted during startup.
This requires NFS share entries to be added to /etc/fstab.
An NFS server entry in fstab is specified as follows:
nfs_server:folder_path mount_point nfs mount_options dump pass
192.168.10.100:/folders/myfolder1 /mount_point nfs defaults 0 0192.168.10.100:/folders_00 /mount_point_2 nfs defaults,nfsvers=4,tcp,bg 0 0Unmounting NFS shares in Linux
To unmount the share, you can use the umount command by specifying either:
nfs_server:folder_pathormount_point
# unmount by nfs_server:folder_pathumount 192.168.10.100:/folders/myfolder1
# unmount by mount_pointumount /mount_pointNFSv4 Only Server
NFSv2 and NFSv3 rely on three core services: nfsd, mountd, and lockd.
These services do not have fixed ports. When they start, they bind to dynamic ports.
They then register the chosen ports with the rpcbind service.
NFS clients have to query the rpcbind service on the server to discover the specific port numbers for each necessary NFS function.
NFSv4 integrates these functions into the main NFSv4 protocol itself.
The protocol also uses a fixed well-known port, 2049/tcp, for communication.
This eliminates the need for these decoupled services and hence the need for rpcbind service.
To run NFSv4 only server, all legacy NFS versions need to be disabled.
In this case rpcbind is not needed and can be disable or masked.
This is the recommended approach for a secure NFS server deployment.
Disabling legacy NFS versions
To disable the legacy NFS versions, the recommended approach is to create a configuration file in /etc/nfs.conf.d/ with the appropriate configurations.
The name of the configuration file does not matter as long as the extension is .conf.
[nfsd]vers2=nvers3=nOnce the file is created, you can restart the NFS server to apply the changes.
sudo systemctl restart nfs-serverYou can verify the enabled or disabled NFS versions by checking the file /proc/fs/nfsd/versions.
A + indicates the version is enabled, while a - indicates it is disabled.
cat /proc/fs/nfsd/versionsFinally you need to disable or mask rpcbind service.
Disabling the service only prevents it from being auto-started on boot. The service can still be manually started.
# disable rpcbindsudo systemctl disable --now rpcbindMasking the service prevents it from being started completely. Attempts to start the service will fail.
# stop and mask rpcbind servicesudo systemctl stop rpcbindsudo systemctl mask rpcbindSummary
nfs-kernel-server,nfs-utils,rpcbindpackages are needed to run an NFS server depending on the desired NFS version and the server Linux distro.- NFS shares are configured in ‘/etc/exports’
- NFS shares can be mounted on the client’s file system just like client local drives.
- NFS shares can be added to
/etc/fstab, hence being mounted on system startup. - For secure NFS deployments, prefer NFSv4 as it has evolved to a more secure protocol than v2 and v3.
Conclusion
You should now be able to:
- Deploy a secure NFS server.
- Mount and unmount NFS shares on local file system.
- Troubleshoot NFS server related problems.
- Troubleshoot permission issues when dealing with mounted NFS shares.
NOTEHappy File Sharing !!!