Tuesday, 1 September 2020

How to Setup NFS (Network File System) on RHEL/CentOS/Fedora and Debian/Ubuntu

NFS (Network File System) is basically developed for sharing of files and folders between Linux/Unix systems by Sun Microsystems in 1980. It allows you to mount your local file systems over a network and remote hosts to interact with them as they are mounted locally on the same system. With the help of NFS, we can set up file sharing between Unix to Linux system and Linux to Unix system.

Benefits of NFS

             NFS allows local access to remote files.

             It uses standard client/server architecture for file sharing between all *nix 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.

Important Files for NFS Configuration

    /etc/exports: It’s 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

To setup NFS mounts, we’ll need at least two Linux/Unix machines. Here in this tutorial, I’ll be using two servers.

     NFS Server: root@erpcloneappl with IP-10.24.248.39

     NFS Client: root@erpclonedb with IP-10.24.248.40

Installing NFS Server and NFS Client

We need to install NFS packages on our NFS Server as well as on NFS Client machine. We can install it via “yum” (Red Hat Linux) and “apt-get” (Debian and Ubuntu) package installers.

Server Side: 10.24.248.39

[root@erpcloneappl ~]# rpm -qa *nfs*

nfs-utils-1.2.3-39.el6.x86_64

nfs-utils-lib-1.1.5-6.el6.x86_64

nfs4-acl-tools-0.3.3-6.el6.x86_64

Note: If Required Package not installed then first installed.

[root@erpcloneappl ~]# yum install nfs-utils nfs-utils-lib

Now start the services on both machines.

Note: Client server does not require NFS services to be running.

[root@erpcloneappl ~]# /etc/init.d/nfs start

[root@erpcloneappl ~]# 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

First we will be configuring 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 or already existing directory named but we have share the existing directory  named as “/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment” in “/” partition

Now we need to make an entry in “/etc/exports” and restart the services to make our directory shareable in the network. to share with client server, you can also share an already existing directory with NFS.

[root@erpcloneappl ~]# vi /etc/exports

Add the following new line as shown below.

/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment 10.24.248.40/255.255.255.255(rw,sync)

In the above example, there is a directory in / partition named “/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment” is being shared with client IP “10.24.248.40” with read and write (rw) privilege, you can also use hostname of the client in the place of IP in above example.

root@erpcloneappl ~]# /etc/init.d/nfs stop

root@erpcloneappl ~]# /etc/init.d/nfs start

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.

[root@erpcloneappl ~]# vi /etc/fstab

Add the following new line as shown below.

10.24.248.39:/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment /ATTACHMENT nfs rw,vers=4,addr=10.24.248.39,clientaddr=10.24.248.40                                 0 0

Refresh the server’s list after modifying /etc/exports

[root@erpcloneappl ~]#  exportfs –r

Displays a list of shares files and options on a server

[root@erpcloneappl ~]#  exportfs –v

[root@erpcloneappl ~]# showmount -e 10.24.248.39

Above command shows that a directory named “cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment” is available at “10.24.248.39” to share with your server.

Setting Up the NFS Client

After configuring the NFS server, we need to mount that shared directory or partition in the client server.

Mount Shared Directories on NFS Client

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 share available on the remote server or NFS Server.

Client Side: 10.24.248.40

Note: Client server does not require NFS services to be running.

[root@erpclonedb ~]# rpm -qa *nfs*

nfs-utils-1.2.3-39.el6.x86_64

nfs-utils-lib-1.1.5-6.el6.x86_64

nfs4-acl-tools-0.3.3-6.el6.x86_64

First of all, create a directory which will be mount point for the NFS share.

[root@erpclonedb ~]# mkdir /ATTACHMENT

[root@erpclonedb ~]# chmod 777 /ATTACHMENT

 [root@erpclonedb ~]# chown applmgr:dba /ATTACHMENT

[root@erpclonedb ~]# showmount -e 10.24.248.39

Above command shows that a directory named “cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment” is available at “10.24.248.39” to share with your server.

[root@erpclonedb ~]# cat /etc/exports

Add the following new line as shown below.

/ATTACHMENT 10.24.248.39/255.255.255.192 (rw)

Mount Shared NFS Directory

To mount that shared NFS directory we can use following mount command.

[root@erpclonedb ~]# mount -t nfs 10.24.248.39:/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment /ATTACHMENT

The above command will mount that shared directory in “/ATTACHMENT” on the client server. You can verify it following command.

[root@erpclonedb ~]# mount | grep nfs

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“.

[root@erpclonedb ~]# vi /etc/fstab

Add the following new line as shown below.

10.24.248.39:/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment /ATTACHMENT nfs rw,vers=4,addr=10.24.248.39,clientaddr=10.24.248.40 0 0

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@erpclonedb ~]# umount /ATTACHMENT

You can see that the mounts were removed by then looking at the filesystem again.

Important commands for NFS

Some more important commands for NFS.

             showmount -e : Shows the available shares on your local machine

             showmount -e <server-ip or hostname>: Lists the available shares at the remote server

             showmount -d : Lists all the sub directories

             exportfs -v : Displays a list of shares files and options on a server

             exportfs -a : Exports all shares listed in /etc/exports, or given name

             exportfs -u : Unexports all shares listed in /etc/exports, or given name

             exportfs -r : Refresh the server’s list after modifying /etc/exports

This is it with NFS mounts for now, this was just a start, I’ll come up with more option and features of NFS in our future articles. Till then, Stay connected with Tecmint.com for more exciting and interesting tutorials in future. Do leave your comments and suggestions below in the comment box.

Create Directory:

CREATE OR REPLACE DIRECTORY XXNICSI_ATTACHMENT AS '/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment';

GRANT EXECUTE, READ, WRITE ON DIRECTORY SYS.XXNICSI_ATTACHMENT TO APPS WITH GRANT OPTION;

GRANT EXECUTE, READ, WRITE ON DIRECTORY SYS.XXNICSI_ATTACHMENT TO PUBLIC WITH GRANT OPTION;

No comments:

Post a Comment