NFS allows the file system on one Linux to be accessed over a network connection by another Linux or UNIX system.
Installing NFS Services on Ubuntu
The services required to enable NFS folder sharing are not installed by default on Ubuntu. They can easily be installed, however, by opening a terminal window and entering the following command:
sudo apt-get install nfs-kernel-server
The installation process should automatically start the NFS service. To verify that the service is indeed running, execute the following command:
sudo /etc/init.d/nfs-kernel-server status
If the output from the above command indicates that the service is not running, it may started as follows:
sudo /etc/init.d/nfs-kernel-server start
Sharing Folders
Once the NFS service is installed and running, the next step is to configure any folders that are to be shared with remote systems. Any folders which are to be shared are listed in the /etc/exports file which may be edited from a terminal window as follows:
sudo gedit /etc/exports
Each folder that is to be shared via NFS must have an entry in this file. The basic syntax is as follows:
folder path hostname(permissions)
For example, to allow a system with the IP address of 192.168.2.24 to access /tmp with read-only access, the following entry would be added to the /etc/exports file:
/tmp 192.168.2.24(rw,sync,no_subtree_check)
Similarly, to also make the folder accessible to a system with the hostname ubuntu2 with read/write permission, the line would read as follows:
/tmp 192.168.2.24(ro,sync,no_subtree_check) ubuntu2(rw,sync,no_sub_tree_check)
Alternatively, to provided read/write access to all hosts, simply use the wildcard character (*):
/tmp *((rw,sync,no_sub_tree_check)
Once the folder entries have been made in the /etc/exports file, the current settings may be checked at any time by running the exportfs command:
sudo exportfs
/tmp 192.168.2.24
Mounting a Remote NFS Folder
Once a folder has been exported it may then be mounted on a client system using the mount command. To mount a remote folder from the command line, open a terminal window and create folder where you would like the remote folder to be mounted:
mkdir /tmp/mnt
Next enter the command to mount to the remote folder (in this example we use ubuntu as the remote hostname):
sudo mount ubuntu:/tmp /tmp/mnt
The remote folder will then be mounted on the local system. Once mounted, the /tmp/mnt folder will contain the remote folder and all its contents.
Mounting an NFS Filesystem on System Startup
It is possible to configure an Ubuntu Linux system to automatically mount a remote file system each time the system starts up. This is achieved by editing the /etc/fstab file. To do this use sudo to load the /etc/fstab file into your favorite editor. It will likely look something like the following:
># /etc/fstab: static file system information.
#
#
proc /proc proc defaults 0 0
# /dev/sda1
UUID=bcde7125-d38d-4362-bcd8-c64f2b512760 / ext3 defaults,errors=remount-ro 0 1
# /dev/sda5
UUID=b4ff42fa-7c9a-4c26-a640-b0af94f14820 none swap sw 0 0
/dev/hdc /media/cdrom0 udf,iso9660 user,noauto 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto 0 0
To mount, for example, a folder with the path /home/demo which resides on a system called ubuntu in the local folder with the path /nfsmount add the following line to the /etc/fstab file:
ubuntu:/home/demo /nfsmount nfs
Next time the system reboots the /home/demo folder on the remote ubuntu system will be mounted on the local /nfsmount mount point. All the files in the remote folder can then be accessed as if they resided on the local hard disk drive.
Unmounting an NFS Mount Point
Once a remote file system is mounted using NFS it can be unmounted using the unmount command with the local mount point as the command-line argument. For example, to unmount our example filesystem mount point requires the use of the following command:
sudo umount /nfsmount