Introduction
NFS (Network File System) is used for sharing files and folders between Linux servers. It allows you to mount your local file systems over a network and remote hosts to interact with them as if they were mounted locally on the same server. In this tutorial, you will learn how to set up NFS on a Linux-based server.
Installing and Configuring
1. First, you have to configure the NFS server
1.1. Install NFS packages with the following commands:
apt-get update
apt-get install nfs-kernel-server
1.2 Start the service and check its status:
service nfs-kernel-server start
service nfs-kernel-server status
1.3 We would recommend making the configuration so that this service would start after a reboot:
nano /etc/rc.local
Add this line at the bottom:
/etc/init.d/nfs-kernel-server restart
1.4 Create a directory you want to share and edit the following file to set rules for sharing:
mkdir /home/test
nano /etc/exports
Paste this line, which sets the specific client to be able to access the directory, with certain rules:
/home/test NFS_client_IP_or_hostname(rw,sync,no_root_squash,no_subtree_check)
Note.
βHere are some possible rules you can configure:
rw - the filesystem is writable;
ro - the filesystem is exported read-only; this is the default;
root_squash - map root UID/GID to anonymous UID/GID (nobody/nogroup); this is the default;
all_squash - map all UIDs/GIDs to anonymous UID/GID (nobody/nogroup);
no_root_squash - do not map root (nor any other) UID/GID to anonymous UID/GID (nobody/nogroup);
sync - reply clients after data have been stored to stable storage; this is the default;
async - reply clients before data have been stored to stable storage; improves performance, but should only be used on ro filesystems
Export directory(-ies) with command:
exportfs -a
2. Steps to configure the NFS Client side:
apt-get update
apt-get install nfs-common
2.1. Create a directory and mount the remotely shared directory on it:
mkdir -p /nfs/home
mount NFS_server_IP_or_hostname:/home/test /nfs/home
NFS configuration is done. You should now be able to see the mounted directory:
df -h
Results should be like this: