In this tutorial, we will learn how to install and configure NFS Server on RHEL/CentOS. Network File system (NFS) is a commonly used file-based storage system that allows remote systems to access files over a computer network and interact with them as if they were locally mounted. This enables system administrators to group resources onto centralized servers on a network for easy sharing.
Table of Contents
Installing NFS Server on RHEL/CentOS
Simple Setup Architecture
To configure NFS server, we will be using two CentOS 8 and Rocky Linux servers acting as the NFS Server while the other acting as the NFS Client respectively.
Hostname | IP Address | Role | OS |
fileserver.kifarunix-demo.com | 192.168.56.165 | NFS Server | CentOS 9 stream |
client.kifarunix-demo.com | 192.168.56.144 | NFS Client | Rocky Linux 9 |
Install NFS Packages on RHEL/CentOS
To install NFS packages on both the Server and the Client, execute the command below;
yum install nfs-utils -y
Configure NFS Server on RHEL/CentOS
Define ID Mapping Domain
ID mapping is the process of mapping user and group IDs between different systems to ensure that file access permissions are correctly applied.
Thus, once the packages are installed, edit the /etc/idmapd.conf file as follows on NFS server;
vim /etc/idmapd.conf
Uncomment the line below by removing #;
#Domain = local.domain.edu
Then replace the domain with your appropriate NFS server domain name.
Domain = kifarunix-demo.com
The domain usually defaults to the host’s DNS domain name.
Save and exit the file;
Restart the rpcidmapd
service to apply the changes:
systemctl restart rpcbind
Create NFS Share Directory
Create a directory that you want to share over the network via NFS server. For example, create a directory named “Shared” in the “/home” directory.
mkdir /home/share
Configure NFS exports
An export
is a directory or file system that is made available to remote clients for sharing. When you set up an NFS server, you can specify which directories or file systems on the server should be available for clients to access. This is done by configuring NFS exports. Once an export is configured, clients can access it by mounting it as a local file system.
Thus, edit the file /etc/exports;
vim /etc/exports
Add file system or directory to be exported to client and specify the options to apply as in the screenshot below:
Each entry for an exported file system has the following structure:
export host(options)
where:
- export is the file system or directory to be mounted on remote host
- host is the remote host (Network, host, or every system,*,)
- options are comma separated list of options
For our case:
/home/share 192.168.56.0/24(rw,sync,no_root_squash)
Options used as in the above are:
- rw allows the client server both read and write access within the shared directory.
- sync ensures that changes made are synchronized immediately.
- no_root_squash allows root user to write files to the NFS share.
Save and exit the file.
For more options, check nfs man pages.
Start and Enable NFS server
Start the NFS server and enable it to start at boot time using the following commands:
systemctl enable --now nfs-server
Open Firewall ports for NFS
There are several ports that need to be open in the firewall on both the server and client machines. Here are the ports that should be open:
- Port 111: This is the portmapper daemon, which maps RPC services to port numbers. Both TCP and UDP need to be open.
- Port 2049: This is the NFS daemon, which handles all NFS requests. Both TCP and UDP need to be open.
- Port 20048: This is the NFSv4 protocol. Both TCP and UDP need to be open.
- Port 875: This is the mountd daemon, which handles mount requests. Both TCP and UDP need to be open.
- Port 892: This is the rpcbind daemon, which is used to manage RPC services. Both TCP and UDP need to be open.
Allow these ports using the commands below;
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --permanent --add-service=rpc-bind
sudo firewall-cmd --permanent --add-service=mountd
sudo firewall-cmd --reload
Configure NFS Client on RHEL/CentOS
After configuring the NFS server, the shared directory or file system has to be mounted on the client so it can be accessed.
Define ID Mapping Domain
Just like it was done on the NFS server, edit the /etc/idmap.conf file and add a domain name like before.
vim /etc/idmap.conf
[General]
...
Domain = kifarunix-demo.com
Save and exit the file.
Enable and start rpcbind service
systemctl restart rpcbind
Mount the NFS share on the client
Mount the shared directory on NFS client using the mount command.
Before mounting, you need to discover
(finds and accesses NFS exports on an NFS server) NFS exports, that is, the shares available on the NFS server as shown below.
There are two ways to discover which file systems an NFS server exports;
- Using showmount
showmount -e NFS-Server-Address
e.g;
showmount -e fileserver
Sample output;
Export list for fileserver:
/home/share 192.168.56.0/24
- Using mount command
mount NFS-Server:/ /mnt/
e.g;
mount -t nfs fileserver:/ /mnt
Confirm that the shared directory is mounted by using df -hT. See the last line in the screenshot below.
df -hT
...
fileserver:/ nfs4 7.8G 1.2G 6.6G 15% /mnt
fileserver:/home nfs4 7.8G 1.2G 6.6G 15% /mnt/home
fileserver:/home/share nfs4 7.8G 1.2G 6.6G 15% /mnt/home/share
Configuring NFS Share Automounting on System Boot
- Automount using FSTAB
- Automount using AutoFS
Automount using FSTAB
Edit the /etc/fstab
file and add an entry for the NFS share. The entry should include the IP address of the NFS server, the path to the NFS share, the mount point directory, and the mount options. For example:
vim /etc/fstab
fileserver.kifarunix-demo.com:/home/ /mnt nfs defaults 0 0
Save the /etc/fstab
file and exit the editor.
You can test the mount by running the following command:
sudo mount -a
When system boots, the share should also automount.
Automount using AutoFS
To enable auto-mounting via AutoFS, first install autofs package on the client;
yum -y install autofs
Default configuration file for autofs is /etc/auto.master. The master map lists autofs controlled mount points on the system and their corresponding configuration files or network sources called automount maps.
Edit the /etc/auto.master file
vim /etc/auto.master
Add a direct mount point at the end of the file. Direct mounts always have /- as the starting point in the master map file.
/- /etc/auto.mount
Save and exit the /etc/auto.master file.
Edit the mount point (/etc/auto.mount
) and create a new map in the form:
mount-point options location
vim /etc/auto.mount
/mnt -fstype=nfs,rw fileserver.kifarunix-demo.com:/home
Make sure the /mnt directory exists.
Start and enable autofs:
systemctl enable --now autofs
Navigate into the /mnt directory and run ll command and you should be able to see the shared files.
or simply check mounting;
df -hT -P /mnt
To that end, you have learnt;
- how to install and configure both the NFS server and NFS client,
- NFS mounting on fstab and auto-mounting using autofs
Other Tutorials
Hi,
great….
Thanks a lot