Skip to content

How to share a directory over a network using NFS

NFS or Network File System is a quick and easy way to share files between Linux hosts on a network. Client machines are able to mount specific directories on a server machine and access the files as if they are on the client's local filesystem.

Configuring an NFS share is as easy as installing a package and editing a config file. For the steps below I'm using a Debian client and a Ubuntu based server.

Configuring the server

Begin by installing the nfs-kernel-server package if it isn't already installed:

sudo apt-get install nfs-kernel-server

Next create a directory on the server for sharing:

sudo mkdir -p /home/user/public

The next step is important to allow any user access. We change the owner to nobody in no group:

sudo chown nobody:nogroup /home/user/public

sudo chmod 777 /home/user/public

Now edit the config file:

sudo vi /etc/exports

Add a line to the end as shown. The IP address provided specifies the permitted clients, in this case 192.168.1.0/24 allows the entire 192.168.1.X subnet access.

# /etc/exports: the access control list for filesystems which may be exported
#        to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)
#
/home/user/public    192.168.1.0/24(rw,sync,no_subtree_check)

Finally, export the shares and restart the NFS server:

sudo exportfs -a

sudo systemctl restart nfs-kernel-server

Mounting the NFS share on the client

Install the nfs-common package if it isn't already installed:

sudo apt-get install nfs-common

To mount the share on the server at IP address 192.168.1.199:

sudo mount 192.168.1.199:/home/user/public /mnt/mount_dir

The directory /mnt/mount_dir can be anywhere on the local filesystem. Reading or writing to this location will result in a read or write to the server.

Remember to unmount when you're done:

sudo umount /mnt/mount_dir

That's it. Now you can share a directory over a network with NFS!

Further reading

Thanks to !robot for this clear and informative guide:

https://websiteforstudents.com/setup-nfs-mounts-on-ubuntu-16-04-lts-servers-for-client-computers-to-access/

Wikipedia article:

https://en.wikipedia.org/wiki/Network_File_System

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

Sam on :

Nice article

The author does not allow comments to this entry

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA 1CAPTCHA 2CAPTCHA 3CAPTCHA 4CAPTCHA 5


Form options