How to share files via NFS

This guide explains how to share files between servers via a network. NFS stands for Network File System and can be used to access data of the remote server locally via the attached filesystem. This example consists of 2 servers, one of them will act as a host (storage1) and the second will be a client (storage2) to which the file system will be attached from the host.

Connect to both host and client and install the nfs-utils package.

NFS at CentOS:

[root@storage1 ~]# yum -y install nfs-utils
[root@storage2 ~]# yum -y install nfs-utils

NFS at Ubuntu/Debian:

[root@storage1 ~]# yum -y install nfs-common
[root@storage2 ~]# yum -y install nfs-common

Create a directory that will be shared with the client-server. If it’s already created then skip this step.

[root@storage1 ~]# mkdir -p /home/data

Assign permissions and ownership to the shared folder.

[root@storage1 ~]# chmod -R 755 /home/data/
[root@storage1 ~]# chown nfsnobody:nfsnobody /home/data/

Start NFS services

[root@storage1 ~]# systemctl enable rpcbind
[root@storage1 ~]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@storage1 ~]# systemctl enable nfs-lock
[root@storage1 ~]# systemctl enable nfs-idmap
[root@storage1 ~]# systemctl start rpcbind
[root@storage1 ~]# systemctl start nfs-server
[root@storage1 ~]# systemctl start nfs-lock
[root@storage1 ~]# systemctl start nfs-idmap

Define which folder to which client will be shared by editing file /etc/exports

[root@storage1 ~]# vi /etc/exports

In this case, the content of the file will be as follows where 181.215.247.22 is the client-server public IP address.

/home/data 181.215.247.22(rw,sync,no_root_squash)

Restart NFS service.

[root@storage1 ~]# systemctl restart nfs-server

In the client-server choose the directory in which the shared folder will be mounted or create a new one.

[root@storage2 ~]# mkdir -p /home/mystorage/

Attach the file system from the host to the client.

[root@storage2 ~]# mount -t nfs 2.58.28.45:/home/data /home/mystorage/

Check if the file system was attached successfully.

[root@storage2 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               185M     0  185M   0% /dev
tmpfs                  209M     0  209M   0% /dev/shm
tmpfs                  209M  8.8M  200M   5% /run
tmpfs                  209M     0  209M   0% /sys/fs/cgroup
/dev/sda1               10G  2.0G  8.1G  20% /
tmpfs                   42M     0   42M   0% /run/user/0
2.58.28.45:/home/data   10G  2.1G  8.0G  21% /home/mystorage

There we have a text file in the host server:

[root@storage1 ~]# cat /home/data/file.txt
This text file exists in storage1

Now it can be accessed locally in the client-server:

[root@storage2 ~]# cat /home/mystorage/file.txt
This text file exists in storage1

To keep the file system mounted after client-server reboot adds the following line at the bottom of the file /etc/fstab where 2.58.28.45 is the host server public IP address.

2.58.28.45:/home/data /home/mystorage nfs defaults 0 0

Related articles:

Was this article helpful?

Need support?

If you need any further help, don't hesitate to send a support request to our support team.