Share Files using
NFS in Ubuntu
Ever since I
switched to Ubuntu I had a need to shares files between all my PCs. Since
I was running Ubuntu I had not much of an idea on how to do this. I had
tried Samba (Unix reverse engineered version of Windows SMB protocol). I
had had very little luck with Samba as the connection to the share was spotty or
sometimes would not connect at all.
I then learned of the Unix NFS (Network File Sharing) protocol. This
seemed to be a better solution and since I had no reason to be compatible with
Windows machines I decided to give it a try.
All I can say is that it does work much better than Samba and far easier to set
up. The connections always work and NFS is the best option for a
Unix/Linux based network.
After I got my NFS home network all set up I decided to share it with everyone
on my site.
Lets get to work.
We will go about this tutorial in a nice step by step manner with full examples.
This by no means is a complete tutorial on NFS, but rather a simple HOWTO.
First step is to install the proper packages for NFS. Open a terminal and
type or copy/paste the following:
sudo apt-get
-y install nfs-kernel-server nfs-common portmap
Second we need to define some parameters. NFS uses IP addresses to
identify PCs on the network. I will set up a home network of two PCs and
one laptop. As many home routers will assign an IP address I recommend you
give each PC an IP address outside the router's DHCP range or bind a certain IP
to a specific MAC address. I will not cover this as static IPs are covered
in numerous Ubuntu tutorials and your router's documentation should cover MAC
binding if the router does have this function.
To find the IP address of a computer on the network open a terminal and type
ifconfig -a
This will list the IP
address and MAC address of the PC, network card and other information.
Each PC's IP address
PC #1 IP - 192.168.1.195
PC #2 IP - 192.168.1.196
PC #3 IP - 192.168.1.102
Each PC's share
PC #1 Share - /media/storage
PC #2 Share - /home/rich
PC #3 Share - /media/stuff
To create a share with NFS you will need to edit your /etc/exports file.
Don't worry, it is a simple edit and very easy to do. TO edit your exports
file do the following in a terminal:
gksudo gedit
/etc/exports
You will be prompted for
your password. Type in your password and hit enter. If you prefer a
different text editor then substitute gedit for one you prefer.
Add the following line to the end of your exports file:
PC #1
/media/storage 192.168.1.1/24(ro,async)
PC #2
/home/rich 192.168.1.1/24(ro,async)
PC #3
/media/stuff 192.168.1.1/24(ro,async)
As you can see
the entries are similar for all the PCs. Here is an explanation of each
parameter using PC #1 for example.
/media/storage -
files shared
192.168.1.1/24 -
means that all PCs from 192.168.1.1 to 192.168.1.255 will be able to access this
share.
ro
- read only, if you wanted to have read/write substitute rw
async
- reply before disk write, this improves performance
Once you have made the modifications hit save and then exit.
You will need to restart the NFS server to institute the changes. To do
that type the following in a terminal.
sudo /etc/init.d/nfs-kernel-server
restart
Now that you have modified your exports and restarted the NFS server you will
need to mount the shares. Easiest way to do that is your /etc/fstab file.
First we must create a mount point for the shares. I will use the
following as an example.
/network/PC_1
/network/PC_2
/network/PC_3
These names are arbitrary, so you may choose another mount point if you wish.
I gave it network for a descriptive purpose. Create these mount points by
doing the following in a terminal:
For PC #1
sudo mkdir
/network/PC_2
sudo mkdir /network/PC_3
For PC #2
sudo mkdir
/network/PC_1
sudo mkdir /network/PC_3
For PC #3
sudo mkdir
/network/PC_1
sudo mkdir /network/PC_2
As you can see each PC will have a mount point for the OTHER PCs on the network
Now that the mount points have been created you will need to edit your fstab
file. To edit your fstab do the following in a terminal:
gksudo gedit /etc/fstab
Supply your password and
add this line to the fstab of each PC:
For PC #1
192.168.1.195:/home/rich /network/PC_2 nfs rsize=8192,wsize=8192,timeo=14,soft,intr
192.168.1.102:/media/stuff /network/PC_3 nfs rsize=8192,wsize=8192,timeo=14,soft,intr
For PC #2
192.168.1.196:/media/storage /network/PC_1 nfs rsize=8192,wsize=8192,timeo=14,soft,intr
192.168.1.102:/media/stuff /network/PC_3 nfs rsize=8192,wsize=8192,timeo=14,soft,intr
For PC #3
192.168.1.195:/media/storage /network/PC_1 nfs rsize=8192,wsize=8192,timeo=14,soft,intr
192.168.1.196:/home/rich /network/PC_2 nfs rsize=8192,wsize=8192,timeo=14,soft,intr
Notice that
each PC has identified a share with the OTHER PCs on the network. Here is
an explanation of each option using PC #1 first line as an example:
192.168.1.195:/home/rich
- file system to be mounted
/network/PC_2
- mount point on the local machine
nfs -
type of file system
rsize=8192
- read size in bytes
wsize=8192
- write size in bytes
timeo=14 -
time in tenths of a second that an NFS server will wait for a request to
complete
soft - soft
mount
intr - allows
signals to interrupt the NFS calls
When you have all the fstab files edited you will need to mount the shares.
To mount the shares do the following in a terminal:
sudo mount -a
This command will mount all the shares in your fstab files. Now assuming
that each PC is powered up your can navigate to the mount point and browse
files!!!
Of course others
will say there are better ways to mount shares. This tutorial is just to
lay out the basics and get your home network going.
Recommendations for a more secure network would be to allow only ONE IP address
instead of .1 through .255 access.
Some have reported that a hard vs. soft mount is better for data integrity.
If your home network is small like mine then a soft mount is the way to go IMO.
As far as hard vs. soft mounts. If one on the PCs goes down on the network
Ubuntu will eventually give up trying to connect back to the share. With a
hard mount Ubuntu will never give up and a reboot may be required. The
soft mounts give up after about a minute. If you were to use this over a
large network with ONE central server with many clients connected I would go
with the hard option.
I hope this tutorial helped.