Introduction:
NFS, the Network File System, is an internet standard protocol used by Linux, UNIX and similar operating system as their native netwok file system. It is an open standard under active extension which supports native Linux permissions and file system features.
Red Hat Enterprise Linux 7 supports NFSv4 (Version 4 of the protocol) by default, and falls back automatically to NFSv3 and NFSv2 if that is not available. NFSv4 uses the TCP protocol to communicate with the server, while older version of NFS may use either TCP or UDP.
NFS servers, exports share (directories) and NFS clients mount an exported share to local mount point (directory). The local mount point must exist. NFS shares can be mounted a number of ways:
manually mounting an NFS share using the mount command.
automatically mounting an NFS share at boot time using /etc/fstab.
Mounting an NFS share on demand through a process known as automounting.
Table of Contents
Configuring NFS Server
Install the required packages [root@nfsserver ~]# yum install nfs-utils* |
Make directory which you want to share [root@nfsserver ~]# mkdir /public [root@nfsserver ~]# mkdir /private |
Give Full Permission to both Shared directories [root@nfsserver ~]# chmod 777 /public [root@nfsserver ~]# chmod 777 /private |
Now we need to make an entry in “/etc/exports” and restart the services to make our directory shareable in the network. [root@nfsserver ~]# vi /etc/exports /public (client IP)192.168.0.2(ro,sync) /private (client IP)192.168.0.2(rw,sync) |
Check files are export to client machine or not. [root@nfsserver ~]# exportfs -arvh |
Note: NFS Options
Some other options we can use in “/etc/exports” file for file sharing is as follows.
ro: With the help of this option we can provide read only access to the shared files i.e client will only be able to read.
rw: This option allows the client server to both read and write access within the shared directory.
sync: Sync confirms requests to the shared directory only once the changes have been committed.
no_subtree_check: This option prevents the subtree checking. When a shared directory is the subdirectory of a larger file system, nfs performs scans of every directory above it, in order to verify its permissions and details. Disabling the subtree check may increase the reliability of NFS, but reduce security.
no_root_squash: This phrase allows root to connect to the designated directory.
For more options with “/etc/exports“, you are recommended to read the man pages for export.
Add services in firewall [root@nfsclient ~]# firewall-cmd –permanent –add-service=nfs [root@nfsclient ~]# firewall-cmd –permanent –add-service= bind [root@nfsclient ~]# firewall-cmd –permanent –add-service= mountd [root@nfsclient ~]# firewall-cmd –reload |
Configuration NFS client
[root@nfsclient ~]# yum install autofs -y |
Add new file in auto.master [root@nfsclient ~]# vim /etc/auto.master Add after “/etc/auto.misc” /mnt /etc/auto.txt :wq |
Create our auto.txt file [root@nfsclient ~]# vim /etc/auto.txt public -ro,sync, (server IP) 192.168.0.1:/public private -rw,sync, (server IP) 192.168.0.1:/private :wq |
Start and enable autofs service [root@nfsclient ~]# systemctl start autofs [root@nfsclient ~]# systemctl enable autofs [root@nfsclient ~]# systemctl status autofs |
[root@nfsclient ~]# mount -a [root@nfsclient ~]# mount [root@nfsclient ~]# mount | grep mount |
Test the Working of NFS Setup
[root@nfsclient]# cd /mnt [root@nfsclient]# ls [root@nfsclient]# cd /public [root@nfsclient]# touch abc [root@nfsclient]# cd.. [root@nfsclient]# cd /private [root@nfsclient]# touch abc [root@nfsclient]# vim abc This is our NFS Server. :wq |
Server end [root@nfsserver]# cd /public [root@nfsserver]# vim xyz This is our NFS Client :wq [root@nfsserver]# cd .. [root@nfsserver]# cd /private [root@nfsserver]# vim xyz2 [root@nfsserver]# ls Client end [root@nfsserver]# c /mnt [root@nfsserver]# ls [root@nfsserver]# cd /private [root@nfsserver]# ls |
Removing the NFS Mount
If you want to unmount that shared directory from your server after you are done with the file sharing, you can simply unmount that particular directory with “umount” command. See this example below. root@nfsclient ~]# umount /mnt/public root@nfsclient ~]# umount /mnt/private |
You can see that the mounts were removed by then looking at the filesystem again. [root@nfsclient ~]# df -h -F nfs |