Purpose: In this tutorial we will learn how to use a NFS mounted filesystem or rather learn how to mount a NFS filesystem and use it on a client computer. You must have heard a lot of embedded developers talking about NFS mount filesystems and all that. You might have go intimidated by that but don’t worry by the end of this post you will know how to do NFS mount from scratch. Trust me, it is easier than what you think.
Basic Concepts: Let’s say you would like to share a directory on your Linux system so that other computers (running Linux) can access the data as if it were a local directory. In order to do that first we need to declare a directory on your Linux machine (NFS server) to be capable of NFS mounting. Then from any client computer on your local network we can mount the directory from your NFS server on the client’s local filesystem so that it appears as if it were a local directory.
- Machine 1 = NFS Server which will host (provide) the directory or an entire filesystem
- Machine 2 = Client machine which will mount the directory as NFS mount.
- Kernel = 2.6.26-1-686 (or any default Debian Linux Kernel)
Step 1: Kernel Configuration Requirements
There are two ways to do NFS mounting: kernel way and the user space way. In this tutorial we are going to learn how to do NFS the kernel way. In order to do that you need certain kernel options to be enabled. If you are using a default distribution kernel then you should be fine as it already has all the options enabled to do NFS mounting. However if you are using a custom kernel then make sure the following settings are enabled:
Note: In order to avoid any errors or failures, preferably use the exact same kernel on both the machines. Once you get it working then you can remove some of the above options that are not required either on NFS server or on client machine.
Step 2: Install necessary packages
You will need the following packages on your Machine 1 (NFS Server)
Machine 1# apt-get update
Machine 1# apt-get install nfs-kernel-server nfs-common portmap
Step 3: Select a directory to be mounted as NFS
I am going to share the following directory for my NFS mount:
Now edit the file:
Machine 1# nano /etc/exportfs
and add the following line in it:
- /root/development = Is the path of the directory which you would like to be mounted as NFS.
- 192.168.0.0/24= All client machines (say Machine 2) whose IP starts with “192.168” will be able to mount the directory as NFS.
- (rw,sync,no_subtree_check) = Permission for that client (Machine 2). Here we are telling that Machine 2 can read/write on the NFS directory
Now update the settings by giving the following command:
Machine 1# /etc/init.d/nfs-kernel-server restart
Machine 1# exportfs -a
Now are you are ready to host the above directory as a NFS share.
Step 4: Mount the directory as NFS mount on Client
On your Machine 2 you first need to install the following package:
Machine 2# apt-get install nfs-common
Now give the following command:
Machine 2# mkdir /mnt/nfs-data
Machine 2# mount 192.168.0.1:/root/development /mnt/nfs-data/
- 192.168.0.1 = IP address of NFS Server (Machine 1)
- /root/development = The directory on your NFS server that you would like to moun
- /mnt/nfs-data = The destination directory on your Machine 2 where you would like to mount the /root/development directory.
Step 5: Start accessing files
Simply go to the directory on your Machine 2 where you mounted the NFS directory
Machine 2# cd /mnt/nfs-data
Machine 2# ls
builds_pkgs chroots devel_image mydata
As you can see the directories in blue are the directories which resides in your /root/development folder on your NFS Server (Machine 1).
You can now create folders/files, edit files, delete folders/files, etc. as if that entire directory was a part of your local filesystem.
Step 6: Un-mount NFS share
Once you are done with the your work you can simply un-cmount the NFS directory/share by giving the following command:
# cd /mnt
# umount nfs-data
- You can also verify whether the NFS directory has been mounted or not on your Machine 2 by giving the following command:
Machine 2# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 9.2G 499M 8.3G 6% /
tmpfs 247M 0 247M 0% /lib/init/rw
udev 10M 84K 10M 1% /dev
tmpfs 247M 0 247M 0% /dev/shm
192.168.0.1:/root/development 367G 51G 298G 15% /mnt/nfs-data
- To see what all directories that are being offered by the NFS server:
Machine 2# showmount -e 192.168.0.1
Export list for 192.168.0.1:
- To see all the directories that are mounted by some client:
Machine 2# showmount -d 192.168.0.1
All mount points on 192.168.0.1:
NFS is a very popular technique used by embedded developers. It is also possible to mount an entire filesystem of a Linux machine over NFS when it boots i.e. we can just have a GRUB bootloader installed on a machine and the entire filesystem will be NFS mounted over the network. If I get time, I will write a tutorial for that too.