Network File System is a great way of sharing files on a network of connected Linux boxes. NFS is really robust albeit less used or understood by lots of people. It's mainly used by SYS Admins and Devops guys to mount network folders on a local instance. This makes the remote folder seem as if it's native to the local file system. Amazon uses in its Elastic file system for instance.
Personally I use NFS for backups. I mount a remote NFS share from a backup box on a local instance. Scripts then can routinely copy files directly to the NFS shared without much trouble.
Also am currently using NFS to mount an app folder on local development server on the LAN on my localhost. So I don't have to use FTP or SCP or even git to keep pushing changes to the development box.
Setting up NFS correctly can be tricky. The important thing to note is here is that NFS follows a client-server implementation. NFS is built on the RPC protocol (remote procedure call) and imposes a client−server relationship on the hosts that use NFS. An NFS server is a host that owns one or more filesystems and makes them available on the network; an NFS client mounts remote filesystems from one or more servers, and uses them in a way equivalent to local filesystems.
Setting up NFS Server on Ubuntu
//1. Install NFS server. You first have to install the NFS server which is available on Ubuntu repositories. So you can simply use apt-get to install the software.
sudo apt-get update
sudo apt-get install nfs-kernel-server
//2. Install nfs client. You can optionally install nfs client although it’s usually installed alongside the the server.
sudo apt-get update
sudo apt-get install nfs-common
//3. Editing /etc/exports The next thing you have to do is edit the /etc/exports file. This is where you specify which folders you want to share and with which clients.
For instance to share a folder located at /home/dave/my-nfs-share with client PC whose IP is 192.168.56.2, enter the following;
The gotcha is on folder permissions. You have to make sure that the UID and GIDs of the owner of the shared folder match exactly the UID and GIDs of the client user mounting the nfs share.
For instance if /home/dave/my-nfs-share belongs to user dave with UID 1000 and group www-data with GID 33 on the NFS server. Make sure that the client user mounting the NFS shared folder also has UID 1000 and belongs to a group whose GID is 33. This is mean booting either your server or client into recovery mode and changing the UID like so ;
sudo usermod -u 1000 user.
//4. Create nfs table
Next, you should create the NFS table that holds the exports of your shares by typing:
sudo exportfs -a
//5. start/restart the nfs server Finally start or restart the NFS server for the changes to take effect;
sudo service nfs-kernel-server start
Setting up NFS client
Now everything we have been doing has been happening on the server-end. It's time to mount our folder on the client PC. Remember these have to be on the same local area network. This won't work if PCs are geographically separated on the internet.
There are two ways to mount an NFS share on the client. You can either do it directly on the commandline or you can use /etc/fstab to mount the nfs share at boot permanently.
//mount on cmd If the nfs server ip is 192.168.56.1, then first create folder where you want to mount your remote nfs share on localhost.
Then mount it with the mount command
mount 192.168.56.1:/home/dave/my-nfs-share /mnt/nfs-share
Tip: you can use
showmount -e 192.168.56.1 just to be sure what nfs share is available to you.
//mount on /etc/fstab To mount your nfs share permanently so that whenever your pc boots up, it's already mounted, add this to your /etc/fstab file.
192.168.56.1:/home/dave/my-nfs-share /mnt/nfs-share nfs rsize=8192,wsize=8192,timeo=14,intr
Just as a side note. If you run into any issues, make sure you have opened the following ports on firewall on your server.
For instance run
sudo iptables -A INPUT -p tcp -m tcp --dport 892 -j ACCEPT to open port 892.
List of useful commands
showmount -e : Shows the available shares on your local machine
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
So basically that's how you configure an nfs share on server and client.