Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
    1
    Share

NFS, stands for Network File System, is a server-client protocol used for sharing files between linux/unix to unix/linux systems. NFS enables you to mount a remote share locally. You can then directly access any of the files on that remote share.

This article will be short and to the point, so whoever wants to mount a remote nfs share in their local machine they should be able to get it up and running in 5 mins or less. Here we go!

Step – 1 Setup AWS security groups

In your EC2 instance setup following Security group exemptions, it would be a good idea to create a separate security group called “NFS Services” or something to leave these exemptions separate from the rest of your security groups.

TCP
Port (Service) Source
111 172.31.0.0/16
2049 172.31.0.0/16
32768 172.31.0.0/16
44182 172.31.0.0/16
54508 172.31.0.0/16
UDP
Port (Service) Source
111 172.31.0.0/16
2049 172.31.0.0/16
32768 172.31.0.0/16
32770 – 32800 172.31.0.0/16

I have set source to 172.31.0.0/16 for those ports , as this allows only servers within the default VPC. Or if you prefer set the IP address of the client machine (external ip) to source when you add those port exemptions.

 

Install the package nfs-utils in the NFS server instance

Install NFS packages in your Server system by using the following command:

yum install nfs-utils prcbind

Every file system being exported to remote ec2 instance via NFS, as well as the access level for those file systems, are listed in the /etc/exports file. When the nfs service starts, the exportfs command launches and reads this file, passes control to rpc.mountd (if NFSv2 or NFSv3) for the actual mounting process, then to rpc.nfsd where the file systems are then available to remote instances.

Lets say you want to export the directory /opt/nfstest of the nfs server to two remote instances having private ip’s 172.31.1.9 and 172.31.1.10

vi /etc/exports

Add

/opt/nfstest 172.31.1.9/255.255.255.0(rw, no_root_squash, async)
/opt/nfstest 172.31.1.10/255.255.255.0(rw, no_root_squash, async)

and save the file

Now we use the exportfs command. It is used to maintain the current table of exported file systems for NFS. This list is kept in a separate file named /var/lib/nfs/xtab which is read by mountd when a remote instance requests access to mount a directory.

Normally this xtab file is initialized with the list of all file systems named in /etc/exports by invoking exportfs -a.

exportfs -a

Getting the services started

Starting the Portmapper

NFS depends on the portmapper daemon, either called portmap or rpc.portmap. It will need to be started first.

service rpcbind start

it is worth making sure that it is running before you begin working with NFS

ps aux | grep rpcbind

service rpcbind status

Now start the NFS daemon

service nfs start

Verifying that NFS is running

rpcinfo -p

you will get something like this.

program vers proto port
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100011 1 udp 749 rquotad
100005 2 tcp 766 mountd
100005 3 udp 769 mountd
100005 3 tcp 771 mountd
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
300019 1 tcp 830 amd
300019 1 udp 831 amd
100024 1 udp 944 status
100024 1 tcp 946 status
100021 1 udp 1042 nlockmgr
100021 3 udp 1042 nlockmgr
100021 4 udp 1042 nlockmgr

Please note that portmap listens on port 111 and nfs on port 2049.

If you later decide to add more NFS exports to the /etc/exports file, you will need to either restart NFS daemon or run command exportfs:

exportfs -ar

Mount remote file system on client

First we need to create a mount point:

mkdir /opt/localmount

If you are sure that the NFS client and mount point are ready, you can run the mount command to mount exported NFS remote file system:

mount -t nfs 172.31.1.3:/opt/nfstest /opt/localmount

Here 172.31.1.3 is the ip of the NFS server, -t is used to specify the file system.

Restart the portmap daemon in the client

service portmap restart
done !

Now you can verify your NFS mounts with the showmount command which shows the exports from the NFS server.

showmount -e 172.31.1.3

which will show output like

/opt/nfstest 172.31.1.9/255.255.0.0,172.31.1.10/255.255.0.0

1


Leave a reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.