How to Setup NFS (Network File System) on RHEL/CentOS
Benefits of NFS
- NFS allows local access to remote files.
- It uses standard client/server architecture for file sharing between all unix/Linux based machines.
- With NFS it is not necessary that both machines run on the same OS.
- With the help of NFS we can configure centralized storage solutions.
- Users get their data irrespective of physical location.
- No manual refresh needed for new files.
- Newer version of NFS also supports acl, pseudo root mounts.
- Can be secured with Firewalls and Kerberos.
- portmap : It maps calls made from other machines to the correct RPC service (not required with NFSv4).
- nfs: It translates remote file sharing requests into requests on the local file system.
- rpc.mountd: This service is responsible for mounting and unmounting of file systems.
Important Files for NFS Configuration
- /etc/exports : Its a main configuration file of NFS, all exported files and directories are defined in this file at the NFS Server end.
- /etc/fstab : To mount a NFS directory on your system across the reboots, we need to make an entry in /etc/fstab.
- /etc/sysconfig/nfs : Configuration file of NFS to control on which port rpc and other services are listening.
Setup and Configure NFS Mounts on Linux Server
- NFS Server: IP-192.168.0.175
- NFS Client : IP-192.168.0.176
Installing NFS Server and NFS Client
[root@localhost ~]# yum install nfs-utils nfs-utils-lib -y
[root@localhost ~]#apt install nfs-utils nfs-utils-lib -y
[root@localhost ~]#yum install portmap (notrequired with NFSv4)
On CentOS 6.x, nfs version 6 is used by default.So,here we don't need to install
portmap package here.
Now start the services on both machines.
[root@localhost ~]# /etc/init.d/nfs start
[root@localhost ~]#chkconfig --level 35 nfs on
After installing packages and starting services on both the machines, we need to configure both the machines for file sharing.
Setting Up the NFS Server
Configure Export directory
For sharing a directory with NFS, we need to make an entry in “/etc/exports” configuration file. Here I’ll be creating a new directory named “nfsshare” in “/” partition to share with client server, you can also share an already existing directory with NFS.
[root@localhost ~]#mkdir /nfsshare
Now we need to make an entry in “/etc/exports” and restart the services to make our directory shareable in the network.
[root@localhost ~]# vi /etc/exports /nfsshare 192.168.0.101(rw,sync,no_root_squash) :wq! [root@localhost ~]#service nfs restart Shutting down NFS daemon: [ OK ] Shutting down NFS mountd: [ OK ] Shutting down RPC idmapd: [ OK ] Starting NFS services: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ]
In the above example, there is a directory in / partition named “nfsshare” is being shared with client IP “192.168.0.176” with read and write (rw) privilege, you can also use hostname of the client in the place of IP in above example.
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.
Setting Up the NFS Client
Setting Up the NFS Client
After configuring the NFS server, we need to mount that shared directory or partition in the client server.
Now at the NFS client end, we need to mount that directory in our server to access it locally. To do so, first we need to find out that shares available on the remote server or NFS Server.
[root@localhost ~]# showmount -e 192.168.0.175
Above command shows that a directory named “nfsshare” is available at “192.168.0.176” to share with your server.
Mount Shared NFS Directory
To mount that shared NFS directory we can use following mount command.
The above command will mount that shared directory in “/mnt/nfsshare” on the client server. You can verify it following command.
[root@localhost ~]# mount | grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) nfsd on /proc/fs/nfsd type nfsd (rw) 192.168.0.175:/nfsshare on /mnt/nfsshare type nfs (rw,vers=4,addr=192.168.0.175,clientaddr=192.168.0.176)
Test the Working of NFS Setup
You can test this by creating directory or file on one side and check on another side.This will assure your testing and make you ready to use nfs server.
The above mount command mounted the nfs shared directory on to nfs client temporarily, to mount an NFS directory permanently on your system across the reboots, we need to make an entry in “/etc/fstab“.
But first umount the mounted directory and check by df -h command.
Now mount the directory permanent in fstab file.Please follow the syntax as required for using while mounting nfs share.For more information regarding this ,please follow on web.
[root@localhost~]# vi /etc/fstab
Add the following new line as shown below.
192.168.0.175:/nfsshare /mnt/nfsshare nfs4 rsize=8192,wsize=8192,timeo=14,intr
Then save the file and for checking whether entry in fstab file is correct or note.Please put the following command.If there is any error,it will show you all.
[root@localhost ~] mount -a
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 33G 680M 31G 3% /tmpfs 499M 0 499M 0% /dev/shm /dev/sda1 477M 25M 427M 6% /boot 192.168.0.175:/nfsshare 33G 680M 31G 3% /mnt/nfsshare
Test the Working of NFS Setup
We can test our NFS server setup by creating a test file on the server end and check its availability at nfs client side or vice-versa.
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@localhost ~]# umount /mnt/nfsshare
You can see that the mounts were removed by then looking at the file system again.
[root@localhost ~]# df -h
You’ll see that those shared directories are not available any more.