Setting Up an NFS Server and Client on CentOS 8
NFS stands for "Network File System" and is a distributed file system protocol used for sharing files and folders between Linux-based operating systems. With NFS you can mount a remote file system locally over a network. With NFS, you can share files and directories with multiple machines and update the files via the share. NFS is the best choice for you if you are looking for a centralised storage solution.
In this tutorial we will show you how to set up an NFS server and client under CentOS 8.
Requirements
- Two Server running CentOS 8.
- A static IP address 172.20.10.4 is configured on NFS server and 172.20.10.3 is configured on NFS client.
- A root password is configured on both server.
Getting Started
By default, SELinux is enabled in CentOS 8 server. So you will need to disable it first.
You can do this by editing /etc/selinux/config file:
nano /etc/selinux/config
Make the following changes:
SELINUX=disabled
Save and close the file. Then, restart your system to apply the changes.
Install NFS Server
By default, the NFS package is available in the CentOS 8 default repository. You can install it by running the following command:
dnf install nfs-utils
After installing NFS server, start NFS service and enable it to start after system reboot:
systemctl start nfs-server
systemctl enable --now nfs-server
You can also check the status of NFS service with the following command:
systemctl status nfs-server
You should see the following output:
? nfs-server.service - NFS server and services Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled) Active: active (exited) since Thu 2019-11-07 02:07:09 EST; 1s ago Process: 9721 ExecStart=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS) Process: 9699 ExecStart=/usr/sbin/rpc.nfsd (code=exited, status=0/SUCCESS) Process: 9698 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS) Main PID: 9721 (code=exited, status=0/SUCCESS) Nov 07 02:07:09 centos8 systemd[1]: Starting NFS server and services... Nov 07 02:07:09 centos8 systemd[1]: Started NFS server and services.
Create NFS Share
In this section, we will create two directories /nfsshare/data and /nfsshare/backup, and share them with NFS.
First, create required directories with the following commands:
mkdir -p /nfsshare/data
mkdir -p /nfsshare/backup
Next, give full permission to the directory with the following command:
chmod -R 777 /nfsshare
Next, you will need to export the created directories by editing /etc/exports file:
nano /etc/exports
Add the following lines:
/nfsshare/data 172.20.10.0/24(rw,sync,no_subtree_check) /nfsshare/backup 172.20.10.0/24(ro,sync,no_subtree_check,no_root_squash)
Save and close the file. Then, run the following command to make your directory shareable in the network:
exportfs -ra
You will need to run above command each time you modify the /etc/exports file.
You can see the active exports with the following command:
exportfs -v
You should see the following output:
/nfsshare/data 172.20.10.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash) /nfsshare/backup 172.20.10.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,no_root_squash,no_all_squash)
Configure Firewall
Next, you will need to allow SSH (For admin purposes) and NFS service through firewalld to access the NFS share from the remote system. Run the following command to allow SSH and NFS service through firewalld:
firewall-cmd --permanent --zone=public --add-service=ssh
firewall-cmd --permanent --zone=public --add-service=nfs
firewall-cmd --reload
Install NFS Client
Now, log in to NFS Client system and install NFS client package with the following command:
dnf install nfs-utils
Once the installation is completed, you can check the NFS shares available on the NFS server with the following command:
showmount -e 172.20.10.4
You should see the following output:
Export list for 172.20.10.4: /nfsshare/backup 172.20.10.0/24 /nfsshare/data 172.20.10.0/24
Mount the NFS Shares on the Client
Next, create two directory on the client machine where you want to mount the NFS shares.
mkdir /home/backup
mkdir /home/data
Next, mount the directories shared on the NFS server with the following commands:
mount -t nfs4 172.20.10.4:/nfsshare/data /home/data
mount -t nfs4 172.20.10.4:/nfsshare/backup /home/backup
You can now check the mounted NFS shares with the following command:
df -h
You should see the following output:
Filesystem Size Used Avail Use% Mounted on /dev/sda1 92G 36G 51G 42% / none 4.0K 0 4.0K 0% /sys/fs/cgroup none 5.0M 0 5.0M 0% /run/lock none 1.9G 65M 1.9G 4% /run/shm none 100M 44K 100M 1% /run/user /dev/sda5 184G 104G 71G 60% /home 172.20.10.4:/nfsshare/data 13G 1.8G 11G 14% /home/data 172.20.10.4:/nfsshare/backup 13G 1.8G 11G 14% /home/backup
If you want to unmount the NFS shares run the following commands:
umount /home/data
umount /home/backup
Automount NFS Shares
If you want to mount the NFS shares automatically on every reboot, edit /etc/fstab file on the client machine:
nano /etc/fstab
Add the following lines:
172.20.10.4:/nfsshare/data /home/data nfs4 rw,sync,hard,intr 0 0 172.20.10.4:/nfsshare/backup /home/backup nfs4 rw,sync,hard,intr 0 0
Save and close the file when you are finished.
Now you can reboot the machine and mount points will be permanent even after the reboot.
Conclusion
Congratulations! you have successfully installed and configured the NFS server and client on CentOS 8. Your server is now ready to serve files. Remember NFS doesn't have much security, so it is recommended to enable Kerberos authentication.