Monday, November 10, 2014

How To : Install and Configure NFS Server and Client on Linux – Remote Disk Access with NFS


Samba is normally used when you want to share disk space between Linux and Windows machines. The Network File System protocol (NFS) is used when disks/directories need to be shared between different Linux servers, and for that its a preferred choice.
The data storage disks on Linux  contain files stored in filesystems with a standardized directory structure.  The new  disks are added by attaching or mounting the directories of their filesystems to a directory of an already existing filesystem. This essentially makes the new hard disk to transparently appear as a subdirectory of the filesystem to which it is attached.
NFS was developed to allow a computer systems to access directories on remote computers by mounting them on a local filesystem as if they were a local disk. The systems administrator on the NFS server has to define the directories that need to be activated or exported for access by the NFS clients.
This helps if you have cluster of systems or if you are looking to keep some data like big videos or data collection on one shared file server and want clients to access them on central repository.
NFS is reasonably easy to setup. For the NFS server setup you will need install the nfs-utils and rpcbind rpm package. On CentOS , RHEL or any RHEL family distros that support yum, it can be installed as below :

yum install nfs-utils rpcbind

The main configuration file for nfs is /etc/exports , which essentially defines below three things :
- What will be exported
- To Whom it will be available
- The properties of export, like readonly or read-write etc
A sample /etc/export file will look like below :

/etc/exports
/data/files *(ro,sync)
/home 192.168.1.0/24(rw,sync)
/data/videos hostOne.domain.com(rw,sync)
/data/databases hostTwo.domain.com(rw,sync)


This /etc/exposts file does following :
- Exports /data/files to all network/systems with read-only option
- Exports /home to network 192.168.1.0/24 with read-write option
- Exports /data/videos to computer system hostOne.domain.com with read-write option
- Exports /data/databases to computer system hostTwo.domain.com with read-write option
There are other options as well that you can mention on either a per-host or per-network basis, including the no_root_squash option which will not prevent root on a client machine from writing files to the server as root; by default, NFS will map any requests from root on the client to the ‘nobody’ user on the server.
The other two files which control the exports on the server are /etc/hosts.allow and /etc/hosts.deny .
This is particularly necessary if you are using wildcards or broad network specifications in /etc/exports; using hosts.allow and hosts.deny you can fine-tune which clients do and don’t have access. For instance, you may add in /etc/hosts.deny:

portmap:ALL

and then in /etc/hosts.allow:

portmap: 192.168.1.1, 192.168.1.2, 192.168.1.3

This would only allow the hosts specified in /etc/hosts.allow to connect to the portmap service. You can get it more fine tuned and also add entries for lockd, rquotad, mountd, and statd.
Then you can start the NFS sharing on the server , which would require starting following services :
Finally, to start NFS sharing, on the server you need to start a few services:
( before starting the services they should be enabled on run level 3 to 5 , using chkconfig –level 35 SERVICENAME on )

service portmap start
service nfs start
service nfslock start
service rpcbind start



To check if the service is running correctly you can run below command , it will list the services and should list mountd , nfs and portmapper :

[root@LinuxServer tmp]# rpcinfo -p localhost
program vers proto port
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100021 1 udp 1024 nlockmgr
100021 3 udp 1024 nlockmgr
100021 4 udp 1024 nlockmgr
100005 1 udp 1042 mountd
100005 1 tcp 2342 mountd
100005 2 udp 1042 mountd
100005 2 tcp 2342 mountd
100005 3 udp 1042 mountd
100005 3 tcp 2342 mountd
[root@LinuxServer tmp]#


To see what filesystems are exported you can use the 'exportfs' command, and when ever you make any changes to the /etc/exports then you will need to run 'exportfs -ra' so that NFS re-reads the configuration.

Now your NFS server is setup and ready to accept connections from the remote client. Testing can be done from the client by mounting one of the exported partitions , this can done in following way :

mkdir /mnt/files
ls /mnt/files
mount -t nfs 192.168.1.100:/data/files /mnt/files

If mount works and mounts the remote filesystem successfully then it means its all working correctly.
You can add the mount command to /etc/fstab to make sure the mounts remain after the reboots.
NFS is very easy to use and works pretty good and can be used to solve many sharing requirements.
Do try it , its easy and quick !
Tags: , , , , ,

No comments:

Post a Comment