What is NFS ( Network File System ) ? This is a distributed file system protocol which is basically shares files/folders in between Linux servers. Keep it in mind, this is sharing in between only with Linux systems, not with Windows. Through NFS, we can allow a system to share it’s folders and files with other Linux servers ( Linux clients ) over the network. From this article we will discuss how to configure NFS in Linux server side and client side briefly with the commands.
NFS is operating in a client-server environment and NFS server is deciding the files and folders which should be shared and nfs client is configured with whom these files are shared.
How to set up NFS server
here, i have created two EC2 instances in AWS. First instance is for NFS client and the second one is NFS server. So, Let’s log into the NFS server.
I have attached a separate disk for the NFS share ( Partition is /dev/xvdb1 ) and mounted to the directory named /nfs-share. So now we are going to make this location as an shared location.
Change the ownership and file permissions as below.
chown nobody:nogroup /nfs-share
chmod 777 /nfs-share
Before everything, we will get the latest update for all applications from ubuntu repositories.
sudo apt-get update
Above command will update your all applications with the latest updates.
sudo apt-get update
Then, Follow the steps as below.
Installing NFS Kernel server in my system.
sudo apt-get install nfs-kernel-server -y
* We can see at the end of the package installation, it creates a path named “/etc/exports“. That is the place where we mention our files and folders which want to be shared with the selected client machines.
According to our scenario, we are going to share /nfs-share folder as i mentioned in the above.
Simply what we have to do is, just mention the folder which we have to share inside of our server and the client machine who needs to access our shared folder over the network.
We have to edit /etc/exports file.
Inside of the exports file we have to mention our shared location ( /nfs-share folder in our server ) and the client IP ( 172.31.19.131 which is our client server ip ) with some of options as below.
<shared location> <client IP> ( options )
rw – Allow both read and write requests on the nfs shared volume. We can make it as “ro” if it needs Read Only.
sync – Write the changes to the disk before replying.
no_subtree_check – This disables subtree checking. This has a middle security implications, but can improve reliability in some circumstances.
If we want to add multiple client servers to get the same shared folder, just add the next server ip and the options having a space.
eg – /nfs-share1 172.31.19.131(options) 172.31.19.132(options) 172.31.19.133(options)
So, after editing /etc/exports file, save it and close.
exportfs -a command exports all shared folders and later we need to restart nfs-kernel-server service in the nfs-server.
Now we have configured NFS server side completely.
Accidently my two servers got corrupted. so i re created two servers from aws console with two different ips. Now i created my nfs share named as “myshare” in nfs server.
To verify if your exports have been successfully exported, we can use the below command.
How to set up NFS client
Get the client server and give a apt-get update to update all the applications to the latest from the repository.
Before doing anything, we can see the mounts as below in the client machine.
What we are expecting to do is, mounting the folder which we exported in nfs server inside of this nfs client.
Follow the below steps.
Install nfs-common client in your machine.
command – apt-get install nfs-common -y
Create a mount point for the nfs shared folder. Then only we can access the shared content in nfs server from our nfs client machine.
Now we are going to mount our nfs share from nfs server to our client machine. After that the files/folders in nfs server share can be accessed from our client machine ( from path /mnt/myshare ).
Firstly we will try to mount temporally.
mount -t nfs <nfs-server-ip>:<nfs-share-folder-path> <client-server-mountpath>
In order to make the mount persistent with the boots, we need to update file /etc/fstab as below.
Now you can try creating new files inside of the /mnt/myshare path, editing files and removing file from the client machine. So everything can be seen when you check from the server side too. Everything is synced in between the nfs server and nfs client.
Important ( This has to be considered only if you are using AWS EC2 instances ) –
When you are trying to mount the nfs-share in your client machine, i mean when you try to run the mount -t nfs command, you have to enable nfs from the AWS security groups. Without enabling nfs, you can’t mount the nfs share. Because nfs is blocked by default from the security group.
To enable nfs from security group
Go to security groups under EC2 dashboard
Edit the Inbound rules
Add nfs as a inbound rule. Select the source as Anywhere.
Save the changes.
So, We learned how to configure nfs in Linux. It’s a very easy setup. One useful application is these are being used when mounting external large disk partitions to the servers like databases for backups.