NFS (Network File System) is a distributed file system that allows you to share files & directories with others over a network. It allows remote users to access your files & folders as if they are mounted locally. In this article, we will look at how to install NFS Server and client on CentOS.
How to Install NFS Server & Client in CentOS
Here are the steps to setup NFS Server & Client in CentOS. Please note, for our example, we will be using the following IP addresses for NFS Server and client.
NFS Server IP: 220.127.116.11
NFS Clients IPs: From the 18.104.22.168/24 range
Also read : How to Sort Python Dictionary by Value
1. Install NFS Server
Open terminal and run the following command to install NFS server.
$ dnf install nfs-utils
Also read : How to Empty a File in Linux
2. Enable & Start NFS Server
Run the following commands to enable & start NFS server.
$ systemctl start nfs-server.service $ systemctl enable nfs-server.service $ systemctl status nfs-server.service
The last command will display the status of NFS server and services.
Also read : How to Install Flask in Ubuntu
3. Create File System
We will create a test file system to export & share on NFS server.
$ mkdir -p /mnt/nfs_shares/test $ mkdir -p /mnt/backups $ ls -l /mnt/nfs_shares/
Also read : How to Remove .php from URL in NGINX
4. Export directories
In order to export the above directories we need to add them to configuration file at /etc/exports .
$ sudo vi /etc/exports
Add the following lines to allow access from only clients in 22.214.171.124/24 IP range.
Let us look at the above configuration.
- rw – allows read & write to these folders
- sync – allows NFS to write operations when requested
- no_all_squash – map GIDs and UIDs from client to identical ones on NFS server
- root_squash – map requests from root user to UID/GID 0
Export the directory with the following command
$ exportfs -arv
In the above command, -a means export all, -r means reexport all directories, and -v means verbose output.
Also read : How to Convert List to String in Python
5. Update Firewall
Finally, update firewall rules to allow NFS server to receive requests from remote clients, by adding following rules.
$ firewall-cmd --permanent --add-service=nfs $ firewall-cmd --permanent --add-service=rpc-bind $ firewall-cmd --permanent --add-service=mountd $ firewall-cmd --reload
Also read : How to Run Scripts on Startup in Ubuntu
6. Setup NFS Clients
Open terminal on your client systems, and run the following commands to install NFS client.
$ dnf install nfs-utils nfs4-acl-tools [On CentOS/RHEL] $ sudo apt install nfs-common nfs4-acl-tools [On Debian/Ubuntu]
Also read : How to Install HAProxy in Ubuntu
7. List exported file system
Run the following command to list the exported folders on NFS server.
$ showmount -e
Also read : How to Import CSV in Python
8. Create Local Directory
Create local directory on NFS client to mount remote directories from NFS server. Mount them as ntf file.
$ mkdir -p /mnt/backups $ mount -t nfs
Confirm it by running mount command with grep nfs
$ mount | grep nfs
You will see the mounted folder location in output.
Run the following command to add an entry in /etc/fstab file and create a persistent mount that works even after system reboot.
$ echo "
126.96.36.199:/mnt/backups /mnt/backups nfs defaults 0 0">>/etc/fstab
To unmount this file, run the following command from the NFS client.
$ umount /mnt/backups
Also read : How to Install Tomcat in Ubuntu
9. Test the setup
Finally, we create a file on server and check if it can be seen on the client. Run the following commands to do so.
$ touch /mnt/backups/file_created_on_server.text [On NFS Server] $ ls -l /mnt/backups/file_created_on_server.text [On NFS client]
Now we create a file on client and check if it can be seen on the server. Run the following commands to do so.
Also read : How to Install Google Cloud SDK in Ubuntu
$ touch /mnt/backups/file_created_on_client.text [On NFS Client] $ ls -l /mnt/backups/file_created_on_client.text [On NFS Server]
That’s it. NFS Server and client have been installed and configured in your system.