Share Files in Raspberry Pi and other Linux PC using NFS

In practice, I always need to transfer or share files in several raspi and Linux PCs, I am lazy to use scp command to do that, so I configured NFS to save my time.

What is NFS?

NFS was developed at a time when we weren’t able to share our drives like we are able to today – in the Windows environment. It offers the ability to share the hard disk space of a big server with many smaller clients. Again, this is a client/server environment. While this seems like a standard service to offer, it was not always like this. In the past, clients and servers were unable to share their disk space.
Thin clients have no hard drives and thus need a “virtual” hard-disk. The NFS mount their hard disk from the server and, while the user thinks they are saving their documents to their local (thin client) disk, they are in fact saving them to the server. In a thin client environment, the root, usr and home partitions are all offered to the client from the server via NFS.

Network environment:

NFS server: 172.17.168.74
NFS client: 172.17.168.109
For server, we must ensure server use static IP address and don’t change it(you should edit network configuration file /etc/network/interfaces), you need to configure network to static IP in Linux, there is only one server in LAN.
For clients, there can be many clients in LAN, clients can use dynamic IP or static IP, these clients had better have similar IP address, such as 172.17.168.109, 172.17.168.60, 172.17.168.12 …

Installing NFS in Raspberry Pi and another Linux

Making your computer an NFS server or client is very easy. Raspberry Pi NFS client needs:

$ sudo apt-get install nfs-common portmap

while a Linux NFS server needs:

$ sudo apt-get install nfs-kernel-server nfs-common portmap

Create shared folder

In server PC, create a folder that you want to share:

$ mkdir /home/zcl/share    (‘share’ is the folder that you want to share)

In client raspberry pi, we create a folder:

$ mkdir /home/pi/share

NFS Server Configuration

NFS exports from a server are controlled by the file /etc/exports. Each line begins with the absolute path of a directory to be exported, followed by a space-seperated list of allowed clients.
Now, we open Root Terminal in server Linux:

$ sudo nano /etc/exports

Add following lines at the end to exports file:

/home/zcl/share 172.17.168.*(rw,sync, no_root_squash) 

Press “Ctrl + O” to save and press “Ctrl +X” to quit.
rw: read and write
ro: read only
no_root_squash: Allows root users on client computers to have root access on the server.

Restart NFS Service in server

$ sudo /etc/init.d/nfs-kernel-server restart

NFS Client Configuration

NFS volumes can be mounted by root directly from the command line. Before we mount, we can check NFS shared folder in client:

$ sudo showmount –e 172.17.168.74

You will see the following if all setting and network connection is good:

Export List for 172.17.168.74:
/home/zcl/share 172.17.168.*

If you see the above lines, continue to mount:

$ sudo mount 172.17.168.74:/home/zcl/share /home/pi/share

Now you can put files to ‘share’ folder in server, you will see same files appear in client.
It is more usual for clients to mount NFS volumes automatically at boot-time. NFS volumes can be specified like any others in /etc/fstab:

$ sudo nano /etc/fstab

Add the following lines to fstab:

172.17.168.74:/home/zcl/share /home/pi/share nfs rsize=8192,wsize=8192,timeo=14,intr

Press “Ctrl + O” to save and press “Ctrl +X” to quit.
Now NFS volumes can be mounted automatically at boot-time.

Good night!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s