Pages

Tuesday, December 11, 2018

Network File System

A Network  file system ( NFS ) is a type of file system mechanism that enables storage and retrieval of data from multiple disks or directories that are spanned across network. The Nfs lets uses to access file systems or files from remote machines as if they were located on the local Machines. Nfs was originally developed by sun microsystems. 

Some of major points of Nfs are,
    Nfs allows local access to remote files
    It uses the standard Client/server architecture for file sharing between multiple machines
    It is not necessary that machines run on same Operating system
    Users can access data irrespective of physical location
    No refresh is required if new files are added.
    Have options to be secured by firewalls and kerberos

Important Services: In order to create a nfs , we need to install 3 services  
Portmap : it maps calls made from other machines to the correct RPC service 
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: While working with nfs, we will be editing some system files which are, 
/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. 

Installation ( Server Side ): For the demo, we will have 2 machines  “10.131.237.55” and “10.131.237.58”. We will be using “10.131.237.58” as our client machine and “10.131.237.55” as our server machine. We will create a directory on our server which will be shared to client. The client can access files from our shared location that is in server. 

In both the machine install the necessary package,  
   [root@nfsserver ~]# yum install nfs-utils nfs-utils-lib 
   [root@nfsserver ~]# yum install portmap  

Start the nfs and portmap services as below, 
   [root@nfsserver ~]# /etc/init.d/portmap start 
   [root@nfsserver ~]# /etc/init.d/nfs-server restart 

Note - if it says portmap not available while restarting, just ignore and start the nfs-server service 

In order to share the directory with nfs, we need to make an entry in the /etc/exports configuration file. We will create a directory first under the /usr/local location as nfsshare in the server. This location will be shared with the client machine.  

Create the directory using, [root@nfsserver ~]# mkdir /usr/local/nfsshare 
Set the owner and permission on this directory as, 
     Chmod -R 755 /usr/local/nfsshare 
     Chown root:root /usr/local/nfsshare 

Add entries to the /etc/exports file as below, 
[root@manja17-I19279 nfsshare]# cat /etc/exports 
/usr/local/nfsshare 10.131.237.58(rw,sync,no_root_squash,no_all_squash) 

Note - If you see the /etc/exports contains the IP address “10.131.237.58”. This is the IP address of the client machine from where we will be accessing the shared location that is created on the server. 

Restart the Nfs service and check if the locations are shared using, 
[root@manja17-I19279 nfsshare]# service nfs-server restart 
Redirecting to /bin/systemctl restart nfs-server.service 

[root@manja17-I19279 nfsshare]# showmount -e 
Export list for k8s-master: 
/usr/local/nfsshare 10.131.237.58 

From the above output we can see that the /nfsshare is now exported. 

NFS Options : we see that inside the /etc/exports file we have defined the shared location and also some options with them. Below are the options that we can use to define in the /etc/exports file 

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. 

Installation ( Client Side ) : Now that we have configured our nfs server and mounted a location to share. Lets configure the Client to access this shared location. 
In both the machine install the necessary package,  
   [root@nfsserver ~]# yum install nfs-utils nfs-utils-lib 
   [root@nfsserver ~]# yum install portmap  

Start the nfs and portmap services as below, 
   [root@nfsserver ~]# /etc/init.d/portmap start 
   [root@nfsserver ~]# /etc/init.d/nfs-server restart 

Note - if it says portmap not available while restarting, just ignore and start the nfs-server service 

Mount Shared NFS directory on the Client machine - 
Create a directory first to sync the shared location on to our client machine, 
[root@manja17-I19278 yum.repos.d]# mkdir /mnt/nfsshare 

All the contents that we see in the /nfsshare of the server machine will now be seen on the /mnt/nfsshare location of client machine. 

Mount the shared location using, 
[root@manja17-I19278 nfsshare]# mount -t nfs 10.131.237.55:/usr/local/nfsshare /mnt/nfsshare 

The above command will mount that shared directory in “/mnt/nfsshare” on the client server. You can verify it following command. 
[root@manja17-I19278 nfsshare]# mount | grep nfs 
nfsd on /proc/fs/nfsd type nfsd (rw,relatime) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime) 
10.131.237.55:/usr/local/nfsshare on /mnt/nfsshare type nfs4 (rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.131.237.58,local_lock=none,addr=10.131.237.55) 
  
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@nfsclient ~]# vi /etc/fstab  
Add the following new line as shown below. 
10.131.237.55:/usr/local/nfsshare /mnt/nfsshare  nfs defaults 0 0 

Now lets create some files in the /usr/local/nfsshare directory in the nfs server and we will be able to see those files in the /mnt/nfsshare of the client location 

Some of the nfs related commands that can be usefull are, 
showmount -e : Shows the available shares on your local machine 
showmount -e 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 

More to Come, Happy learning :-)

No comments :

Post a Comment