How To Set Up an NFS Server on Ubuntu 14
NFS, which stands for Network File System, is a distributed filesystem protocol that allows for the mounting of remote directories from an NFS server onto a client server. These directories can then be accessed from the client much like local storage, and the mounting process can even be automated for greater convenience. Using NFS mounts is a useful ability in situations where space usage needs to be optimized and multiple clients need to access the same server space, with the main advantage of NFS being its allowance for central management of files. This decreases administrator workload as well as enhancing the sharing possibilities of individual files and repositories.
In this tutorial, we will show you how configure NFS mounting on an Ubuntu 14.04 server and demonstrate its usage in a typical host-client scenario.
Getting Started
Confirm that you have the following before you follow this guide:
• 2 Node (Cloud Server or Dedicated Server) running Ubuntu 14.
• Root access to the node.
Tutorial
The following two servers are used in our tutorial as our client and NFS servers as well as their respective IP addresses. You should replace these addresses with your own server values.
Server 1 : client – 10.10.0.134
Server 2 : nfs-server – 10.10.0.135
Initial Setup
In order for our two servers to be able to communicate in this tutorial, we need to add them to each other’s respective hosts files. In both the servers to be used, open the /etc/hosts file:
sudo nano /etc/hosts
Add the following entires to this file:
10.10.0.134 client
10.10.0.135 nfs-server
Make sure that this is done for both of the Ubuntu servers before proceeding. This allows us to use the names for the machines instead of their IP addresses in the rest of the tutorial. You should replace the IP addresses we used for the IPs of your own machines.
Setup the NFS Server
We begin by setting up the server to be used as our NFS host, so all steps in this category must be executed on that server. Before we install any new packages required for our NFS host setup, we first need to refresh our local package information using the following command:
apt-get update
Now we are ready to install the nfs-kernel-server package.
apt-get install nfs-kernel-server
The package installed in the above step allows for the sharing of repositories. We will create one such repository on the NFS host in /sharednfss to be later shared with this command:
mkdir /sharednfs
The directory we just created is owned by the root. Note that if you want to however create a directory specifically for sharing that is not owned by the root user, you must change its ownership using sudo chown nobody:nogroup /sharednfs. This is unnecessary for our case and we will proceed with the root ownership.
With our directory for sharing in place and the NFS package install, continue on the NFS server to configure how we will share the directory. First, create the exports file:
sudo nano /etc/exports
The exports file contains information on the configuration of our NFS mount and will look something like the following, with each line representing the rules for each directory to share:
/first/directory client(option1,option2,...)
/second/directory client(option1,option2,...)
This means that we want to modify the /etc/exports file to contain the information of our created directory and our client server. Don’t forget to replace our client address from this tutorial with the address of your own client server in your hosts file! Create a line like the following in the file using the text editor nano:
/sharednfs client(rw,sync,no_root_squash,no_subtree_check)
The specific NFS options we enabled are used to control how the files are accessed. To begin, rw gives a client both read and write access to the repository. This means that the shared repository can be edited by both the NFS host server and the client server. The next option, sync, makes it possible for NFS to store changes to disk before replying to the client, ensuring consistency in the files the client can see. Finally, no_subtree_check ensures that no problems occur if files are renamed while the client has them open by preventing subtree checking (host checks whether a file is actually available). If the repository being shared is owned by a root user, as in our case, you can use the option no_root_squash to circumvent a built-in NFS security feature that restricts root accounts on clients to use host directories as root.
After making your changes, save and close the exports file. To restart the NFS service and load the new modified export file, execute the following two commands:
exportfs -a
service nfs-kernel-server start
Setup the Client Server
With our NFS host setup, the client server must also be configured to have access to the shared repository. Much like for the host NFS server, we also need to refresh our local package information before installing any packages. Use the following command to update local information:
apt-get update
After updating our package repository information, we need to install the nfs-common package to allow NFS functionality without the server components.
apt-get install nfs-common
Because NFS shared repositories are “mounted” on the client system, we need to create a mount point for the desired shared repository. Such mount points are typically located in /mnt on a filesystem so we will create ours there as well. Create the shared directory where the nfs server repository will be mounted:
mkdir -p /mnt/sharednfs
After creating this mount point, we can finally mount the repository from the NFS host onto the directory we just created on the client. Don’t forget to replace the host IP address with your host’s IP address.
mount 10.10.0.135:/sharednfs /mnt/sharednfs
To recap, our directory /sharednfs from the NFS host server with the IP address 10.10.0.135 should now also be accessible from /mnt/sharednfs on the client server with the IP address 10.10.0.34. To check that the NFS shared directory is in fact mounted, execute the following command to show available disk space on our machine:
df -h
The output of the previous command should show something like the following lines if the repository is properly mounted (the IP address may show as nfs-server instead):
Filesystem Size Used Avail Use% Mounted on
10.10.0.135:/sharednfs 20G 0G 20G 0% /mnt/sharednfs
You can also see only what NFS shares are mounted with the following command. This command will print line by line the shared repositories and their settings.
mount -t nfs
When using the second command to check NFS mounting, the output should be similar to this if correctly executed (the IP address may show as nfs-server instead):
10.10.0.135:/sharednfs on /mnt/sharednfs type nfs (rw,vers=4,addr=10.10.0.135,clientaddr=10.10.0.134)
As an additional step, we can add the NFS mounting to fstab to be done on boot for convenience. Open the fstab file:
nano /etc/fstab
Add the following line to the bottom of the above file, then save and close. You can choose your own options from the nfs man page using man nfs in order to customize how you want the mounting done on boot when specified in the fstab file.
nfs-server:/sharednfs /mnt/sharednfs nfs auto,noatime,nolock,bg,nfsvers=4,sec=krb5p,intr,tcp,actimeo=1800 0 0
Test Your NFS Setup
To ensure that our setup is not just mounted correctly, but also the files are sharing as expected between the host and client servers, create a file to test the process. On your NFS server, go to the NFS shared directory with cd /sharednfs and execute the following to create a typical “Hello world” file:
echo Hello world > hello.txt
Now, go to your client server, navigate to the shared directory, and check if the file is there:
cd /mnt/sharednfs
ls
You should see the “hello.txt” file in the repository list. You can even check its contents for fun:
cat hello.txt
Conclusion
After completing this guide, you are now able to utilize the power of NFS for your own setups and benefit from its central management advantages. However, you should note that while using NFS is quick and easy for accessing remote network systems, the protocol NFS uses is not encrypted and thus considerations should be taken such as routing over SSH or VPN in order to enhance security. If you found this tutorial for using NFS useful, feel free to share it with others who may be interested.