[NFS] File sharing between Linux hosts

Target

Build an NFS server and realize file sharing between Liunx hosts (using AWS environment).

Basic knowledge of NFS

A communication protocol used to share a file system over a network. NFS has been used for a long time mainly for sharing file systems between Unix-like OSs. By mounting the directory published by the NFS server on the NFS client, It is possible to treat the remote file system in the same way as the local file system.

(From Linux textbook LPIC Level 2 Version 4.5 compatible)

Premise

-Two AWS EC2 instances (using Amazon Linux 2 AMI (HVM) and SSD Volume Type in this article) (* 1) have already been built.

※1 For instances used as NFS servers, ** open port 2049 (NFS) **.

Workflow

Item number title
1 Build an NFS server
2 Build NFS client

procedure

1. Build an NFS server

** ① Confirmation of installation of required packages ** Log in to the pre-created EC2 NFS server instance, and after the root switch, check the installation of the NFS package.

It was installed by default on Amazon Linux (*)

[root@ip-172-31-43-189 ~]# rpm -qa | grep nfs
libnfsidmap-0.25-19.amzn2.x86_64
nfs-utils-1.3.0-0.54.amzn2.0.2.x86_64
yum install nfs-utils

** ② NFS public directory setup ** Create a directory to be published using NFS and a test file for sharing confirmation under it.

mkdir -p /export/nfs
touch /export/nfs/share.txt

The NFS public directory is described in / etc / exports.

vi /etc/exports

In / etc / exports, the directory name to be published, the client to be published (host name or IP address), options, Write one line for each directory you want to export.

In this article, the / export / nfs created earlier is open to all hosts (), and as an option, it is open to read / write (rw) and root accessible (no_root_squash) ().

/etc/exports


/export/nfs  *(rw,no_root_squash)

Use the exportfs command to reflect the contents of / etc / exports.

exportfs -ar

It is OK if the export status is displayed and the public directory / export / nfs is output as shown below.

# exportfs -v
/export/nfs     <world>(rw,sync,wdelay,hide,no_subtree_check,sec=sys,secure,no_root_squash,no_all_squash)

** ③ Start NFS server ** NFS server startup → automatic startup → status check

systemctl start nfs-server
systemctl enable nfs-server
systemctl status nfs-server

Use the showmount command to find out which directories are being exported by the NFS server. If you get the following output, the public directory / export / nfs is properly reflected on the NFS server.

# showmount -e
Export list for ip-172-31-43-189.ap-northeast-1.compute.internal:
/export/nfs *

2. Build NFS client

** ① Mount NFS public directory ** After logging in to the EC2 instance used as an NFS client, the root switch. After that, execute the mount with the following file system type as nfs, and mount the public directory / export / nfs of the NFS server created earlier to / mnt. Refill the private IP address part of the NFS server.

mount -t nfs NFS server private IP address:/export/nfs/ /mnt

Use the df command to check if the public directory / export / nfs was successfully mounted on / mnt.

# df -h /mnt
Filesystem                  Size  Used Avail Use% Mounted on
13.114.211.230:/export/nfs  8.0G  1.4G  6.7G  17% /mnt

Also, make sure that the share confirmation test file share.txt that was created in advance exists under / mnt.

# ls -l /mnt
total 0
-rw-r--r-- 1 root root 0 Nov 20 05:52 share.txt

** ② Automatic mount at startup of NFS public directory ** Then use / etc / fstab to enable automatic mounting at boot time.

echo 'NFS server private IP address:/export/nfs /mnt nfs rw 0 0' >> /etc/fstab

Reboot execution

reboot

After rebooting, it is OK if the NFS public directory is mounted automatically.

Books and sites that I referred to

・ Reference books Linux textbook LPIC Level 2 Version 4.5 compatible ・ Reference site [CentOS 7] memorandum of NFS server construction

Recommended Posts

[NFS] File sharing between Linux hosts
[Samba] File sharing between Linux and Windows machines
[Linux] File search
Linux command [File operation]
Hack Linux file descriptors
Device and Linux file system
Linux file and directory permissions