How Do You Set up an nfs File Share between Two Linux Servers in GCP?

Problem scenario
You want to be able to share a directory between two Red Hat distribution Linux servers.  How do you expose an NFS share on one Linux server and make it viewable for reads and writes from a second Linux server?

You have install nfs-utils on both servers.  To do this, run this command:  sudo yum -y install nfs-utils

Designate one server to host the nfs server.  This will be the "nfs server."  Designate the other server as the client of the nfs server (or "nfs client").  In GCP we found that using the internal IP addresses worked well when the directions below call for a server IP address.  But you may adapt this solution to a non-GCP pair of servers.

1.  From the nfs server, do the next three steps (through step #4).
2.  Make the directory that will be exported as an nfs share (e.g., sudo mkdir -p /usr/bin/contint).
3.  Modify the /etc/exports file (or create one if necessary).  Add a line like this (but replace x.x.x.x with the IP address of the nfs client server and replace "/usr/bin/contint" with the directory created in step #2):

/usr/bin/contint x.x.x.x(rw,sync,no_root_squash)

4.a.  Run this command:  sudo service nfs start

4.b.  If you want the nfs service to be available after a reboot, you will have to do this:

sudo crontab -e   # to modify the crontab.  add the line below then save it (usually by pressing the Escape key then uppercase Z twice, e.g., Esc ZZ)

@reboot sudo service nfs start

5.  Log into the nfs client server.  Run the following commands from the nfs client server.   
6.  Make a directory that will be the place where you read and write to the nfs file share.  Here is an example:  sudo mkdir -p /usr/bin/viewable

7.  Run this command but replace x.x.x.x with the IP address or hostname of the nfs server: sudo showmount -e x.x.x.x

8.  Compose a command like this, but make three replacements as described below it:

sudo mount -t nfs x.x.x.x:/dir/path/above /usr/bin/viewable

i.  Replace "x.x.x.x" with the IP address or hostname of the nfs server.
ii.  Replace "/dir/path/above" with the directory referred to in the results from step #7.  
iii.  Replace "/usr/bin/viewable" with the directory you created in step #6 above.

9.  Run the command composed above.

10.  Verify it work by running this:  ls -lh /usr/bin/viewable

11.  If you want the nfs share to be usable after a reboot of the client, run a command like this: echo 'sudo mount -t nfs x.x.x.x:/dir/path/above /usr/bin/viewable' >> ~/.bashrc

(Remember to replace "sudo mount -t nfs x.x.x.x:/dir/path/above /usr/bin/viewable" with the command composed in step #8.)

Leave a comment

Your email address will not be published. Required fields are marked *