Setup NFS server on CentOS
What is Linux NFS Server?
Network File Sharing (NFS) is a protocol that allows you to share directories and files with other Linux clients over a network. Shared directories are typically created on a file server, running the NFS server component. Users add files to them, which are then shared with other users who have access to the folder.
An NFS file share is mounted on a client machine, making it available just like folders the user created locally. NFS is particularly useful when disk space is limited and you need to exchange public data between client computers.
Installing NFS Server
First, we start by installing the necessary packages on the NFS Server. The nfs-utils package provides a daemon for the NFS server kernel and related tools such as the showmount program.
On CentOS 8:
dnf install nfs-utils
On CentOS 7:
yum -y install nfs-util
After successful installation we need to start nfs-server with the following command:
systemctl start nfs-server.service
Execute the following command to start nfs-server at the same time as the system:
systemctl enable nfs-server.service
Check the status of nfs-server with the following command:
systemctl status nfs-server.service
The configuration files of the NFS server are:
/etc/nfs.conf: The main configuration file for the NFS daemon and engine.
/etc/nfsmount.conf: NFS mount configuration file.
/etc/exports: The main config file of NFS, containing the list of files and shared folders on the NFS Server.
/etc/fstab: To automatically mount an NFS directory on the system.
/etc/sysconfig/nfs: NFS config file to manage rpc listening ports and services.
Next, we will create files to share on the NFS server. We will create a file /mnt/greencloudvps-test-nfs-server and a file for backup for the root user:
mkdir -p /mnt/greencloudvps-test-nfs-server
ls -l /mnt/
Then configure the NFS server /etc/export to determine which clients can access the shared file on the NFS server using the vi editor.
Add this line:
/mnt/greencloudvps-test-nfs-server: This is the directory to share the data.
10.0.0.2/8: The range of accessible addresses.
(rw,sync): Access rights.Permissions in NFS:
ro: Read only.
noaccess: Denied access.
root_squash: Prevent remote root users.
no_root_squash: Allow remote root users.
To export the above file system, we run the exportfs command with the -a option to export or unexport all directories, the -r option to re export all directories, synchronize /var/lib/nfs /etab with /etc/export and files in /etc/export.d, and -v allows the output to be fully displayed.
[[email protected] ~]# exportfs -arv
To display the current export list run the following command:
Next, if our system has a firewall, we need to allow the necessary services through the firewall such as mountd, nfs, rpc-bind:
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --permanent --add-service=mountd
Install NFS Client
To be able to use NFS on the Client we need to install the necessary package by executing the following command:
On CentOS 8:
dnf install nfs-utils nfs4-acl-tools
yum install nfs-utils nfs4-acl-tools
To check mount point on NFS Server from client:
[[email protected] ~]# showmount -e 10.0.0.1
Export list for 10.0.0.1:
Next, we create a local directory /mnt/greencloudvps-test-nfs-client to mount to the NFS Server:
mkdir -p /mnt/greencloudvps-test-nfs-client
ls -l /mnt/
Mount NFS file executes the command below:
mount -t nfs 10.0.0.1:/mnt/greencloudvps-test-nfs-server /mnt/greencloudvps-test-nfs-client/
10.0.0.1: The address of the NFS server.
/mnt/greencloudvps-test-nfs-server: Shared folder on NFS server.
/mnt/greencloudvps-test-nfs-client/: Mount point on the NFS client.
Then to confirm the file from the NFS server is mounted by running the mount command and grep nfs.
[[email protected] ~]# mount | grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
10.0.0.1:/mnt/greencloudvps-test-nfs-server on /mnt/greencloudvps-test-nfs-client type nfs4 (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.0.0.2,local_lock=none,addr=10.0.0.1)
To be able to automatically mount to NFS Server when the server starts, we need to edit the /etc/fstab file, adding the following line at the end of the file:
echo "10.0.0.1:/mnt/greencloudvps-test-nfs-server /mnt/greencloudvps-test-nfs-client/ nfs defaults 0 0">>/etc/fstab
Check the file /etc/fstab after we execute the above command:
When we want to configure the timeout for the NFS mountpoint in /etc/fstab, the syntax is as follows
:/Shared folder on NFS server /Mount point on NFS client nfs soft,timeo=30 0 0
timeo: The timeout value.
30: The time they need to be configured using deciseconds (1 deciseconds equals 1/10th of a second).
retrans: How many retries the vehicle will retry in the event of an error.
Note: timeo and retrans only work in soft nfs, not in hard nfs.