Project

General

Profile

Support #251

Installing an NFS Server on Debian/Ubuntu

Added by Daniel Curtis almost 7 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Network Attached Storage
Target version:
-
Start date:
12/02/2013
Due date:
% Done:

100%

Estimated time:
0.50 h
Spent time:

Description

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
An NFS mount is set up between at least two virtual servers. The machine hosting the shared network is called the server, while the ones that connect to it are called ‘clients’.

This tutorial requires 2 servers: one acting as the server and one as the client. We will set up the server machine first, followed by the client. The following IP addresses will refer to each one:
  • Master: 192.168.10.10
  • Client: 192.168.10.200

The system should be set up as root. You can access the root user by typing:

sudo su-

Setting Up the NFS Server

Download the Required Software

Start off by using apt-get to install the nfs programs.

apt-get install nfs-kernel-server portmap

Export the Shared Directory

The next step is to decide which directory we want to share with the client server. The chosen directory should then be added to the /etc/exports file, which specifies both the directory to be shared and the details of how it is shared.

Suppose we wanted to share two directories: /home and /var/nfs.

Because the /var/nfs/ does not exist, we need to do two things before we can export it.

First, we need to create the directory itself:

mkdir /var/nfs/

Second, we should change the ownership of the directory to the user, nobody and the group, no group. These represent the default user through which clients can access a directory shared through NFS.
Go ahead and chown the directory:

chown nobody:nogroup /var/nfs

After completing those steps, it’s time to export the directories to the other VPS:

nano /etc/exports

Add the following lines to the bottom of the file, sharing both directories with the client:

/home           192.168.10.200(rw,sync,no_root_squash,no_subtree_check)
/var/nfs        192.168.10.200(rw,sync,no_subtree_check)

Note: If a network range is used, a subnet mask must also be present:
/var/nfs        192.168.10.0/24(rw,sync,no_subtree_check)

These settings accomplish several tasks:
  • 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

Note: An export can be set to individual IP addresses or networks:

NFS clients may be specified in a number of ways:
single host
You may specify a host either by an abbreviated name recognized be the resolver, the fully qualified domain name, an IPv4 address, or an IPv6 address. IPv6 addresses must not be inside square brackets in /etc/exports lest they be confused with character-class wildcard matches.
netgroups
NIS netgroups may be given as @group. Only the host part of each netgroup members is consider in checking for membership. Empty host parts or those containing a single dash (!) are ignored.
wildcards
+Machine names may contain the wildcard characters * and ?, or may contain character class lists within [square brackets]. This can be used to make the exports file more compact; for instance, *.cs.foo.edu matches all hosts in the domain cs.foo.edu. As these characters also match the dots in a domain name, the given pattern will also match all hosts within any subdomain of cs.foo.edu.
IP networks
You can also export directories to all hosts on an IP (sub
!) network simultaneously. This is done by specifying an IP address and netmask pair as address/netmask where the netmask can be specified in dotted-decimal format, or as a contiguous mask length. For example, either '/255.255.252.0' or '/22' appended to the network base IPv4 address results in identical subnetworks with 10 bits of host. IPv6 addresses must use a contiguous mask length and must not be inside square brackets to avoid confusion with character-class wildcards. Wildcard characters generally do not work on IP addresses, though they may work by accident when reverse DNS lookups fail.

Once you have entered in the settings for each directory, run the following command to export them:

exportfs -a

If any mistakes are mades, you can unmount the exports via:

exportfs -ua

Setting Up the NFS Client

Download the Required Software

Start off by using apt-get to install the nfs programs.

apt-get install nfs-common portmap

Mount the Directories

Once the programs have been downloaded to the the client server, create the directories that will contain the NFS shared files

mkdir -p /mnt/nfs/home
mkdir -p /mnt/nfs/var/nfs

Then go ahead and mount them

mount 192.168.10.10:/home /mnt/nfs/home
mount 192.168.10.10:/var/nfs /mnt/nfs/var/nfs

You can use the df -h command to check that the directories have been mounted. You will see them last on the list.

df -h

Filesystem             Size  Used Avail Use% Mounted on
/dev/sda                100G  9480M   90G   5% /
udev                   119M  4.0K  119M   1% /dev
tmpfs                   49M  208K   49M   1% /run
none                   5.0M     0  5.0M   0% /run/lock
none                   122M     0  122M   0% /run/shm
192.168.10.10:/home      500G  510M   499.5G   5% /mnt/nfs/home
192.168.10.10:/var/nfs   500G  510M   499.5G   5% /mnt/nfs/var/nfs

Additionally, use the mount command to see the entire list of mounted file systems.

mount

Your list should look something like this:

/dev/sda on / type ext4 (rw,errors=remount-ro,barrier=0) [DOROOT]
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
rpc_pipefs on /run/rpc_pipefs type rpc_pipefs (rw)
192.168.10.10:/home on /mnt/nfs/home type nfs (rw,vers=4,addr= 192.168.10.10,clientaddr=192.168.10.200)
192.168.10.10:/var/nfs on /mnt/nfs/var/nfs type nfs (rw,vers=4,addr=192.168.10.10,clientaddr=192.168.10.200)

Testing the NFS Mount

Once you have successfully mounted your NFS directories, you can test that they work by creating files on the Client and checking their availability on the Server.

Create a file in each directory to try it out:

touch /mnt/nfs/home/example /mnt/nfs/var/nfs/example

You should then be able to find the files on the Server in the /home and /var/nfs directories.

ls /home
ls /var/nfs/

You can ensure that the mount is always active by adding the directories to the fstab file on the client. This will ensure that the mounts start up after the server reboots.

nano /etc/fstab

Then add the following at the end

192.168.10.10:/home  /mnt/nfs/home   nfs      auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0
192.168.10.10:/var/nfs  /mnt/nfs/var/nfs   nfs     auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0

You can learn more about the fstab options by typing in:

man nfs

Any subsequent restarts will include the NFS mount—although the mount may take a minute to load after the reboot You can check the mounted directories with the two earlier commands:
df -h
mount

Removing the NFS Mount

Should you decide to remove a directory, you can unmount it using the umount command:

cd
sudo umount /directory name

You can see that the mounts were removed by then looking at the filesystem again.

df -h

You should find your selected mounted directory gone.

Resources

History

#1 Updated by Daniel Curtis almost 7 years ago

  • Project changed from 47 to 48
  • Description updated (diff)

#2 Updated by Daniel Curtis over 6 years ago

I encountered a problem with the nfs-kernel-server not starting at boot. To fix this, I just enabled the service to start at boot:

sudo update-rc.d nfs-kernel-server defaults

#3 Updated by Daniel Curtis over 6 years ago

  • Description updated (diff)

#4 Updated by Daniel Curtis over 5 years ago

  • Description updated (diff)

#5 Updated by Daniel Curtis over 5 years ago

  • Project changed from 48 to GNU/Linux Administration
  • Category set to Network Attached Storage

#6 Updated by Daniel Curtis over 5 years ago

  • Subject changed from How To Set Up an NFS Server on Debian/Ubuntu to Installing an NFS Server on Debian/Ubuntu

Also available in: Atom PDF