NFS mounts work to share a directory between several virtual servers. This has the advantage of saving disk space, as the home directory is only kept on one virtual private server, and others can connect to it over the network. When setting up mounts, NFS is most effective for permanent fixtures that should always be accessible.
Setup
Set up the server machine and the client. The following IP addresses will refer to each one:
Server: 10.96.70.3
Client: 10.96.70.5
Setting Up the NFS Server
1. Download the Required Software
sudo apt-get install nfs-kernel-server portmap
2. Export the Shared Directory
We wanted to share two directories: /home and /var/nfs.
Create /var/nfs/:
sudo mkdir /var/nfs/
Change the ownership of the directory to the user, nobody and the group, no group.
sudo chown nobody:nogroup /var/nfs
Export the directories:
sudo vim /etc/exports
Add the following lines to the bottom of the file:
/home 10.96.70.3 (rw,sync,no_root_squash,no_subtree_check)
/var/nfs 10.96.70.3 (rw,sync,no_subtree_check)
rw: This option allows the client server to both read and write 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 filesystem, 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.
run the following command to export all shares:
sudo exportfs -a
Setting Up the NFS Client
Download the Required Software
sudo apt-get install nfs-common portmap
Mount the Directories
sudo mkdir -p /mnt/nfs/dir1
sudo mkdir -p /mnt/nfs/dir2
mount the tow shares as below:
sudo mount 10.96.70.3:/home /mnt/nfs/dir1
sudo mount 10.96.70.3:/var/nfs /mnt/nfs/dir2
Use the df -h to verify that the directories have been mounted.
sudo df -h
Filesystem Size Used Avail Use% Mounted on . . . 10.96.70.3:/home 20G 948M 19G 5% /mnt/nfs/dir1 10.96.70.3:/var/nfs 20G 948M 19G 5% /mnt/nfs/dir2
Testing the NFS Mount
Create a file in each director:
sudo touch /mnt/nfs/dir1/example /mnt/nfs/dir2/example
sudo ls /home
sudo ls /var/nfs/
to ensure that the mount is always permanents by adding the directories to the fstab file on the client. The mounts will be active after the server reboots.
sudo vim /etc/fstab
Add the following lines to the end of fstab file:
10.96.70.3:/home /mnt/nfs/dir1 nfs auto 0 0
10.96.70.3:/var/nfs /mnt/nfs/dir2 nfs auto 0 0
Removing the NFS Mount
sudo umount /mnt/nfs/dir1
sudo umount /mnt/nfs/dir2
Open the port in firewall or in iptables:
Open Port 111 (TCP and UDP) and 2049 (TCP and UDP) for the NFS server.
Done.