Complete steps in CentOS 7 to set up the NFS file sharing storage service

  • 2020-12-20 03:52:12
  • OfStack

preface

NFS(Network File System) stands for network file system. Its biggest function is to let different machines and different operating systems share files with each other through the network. Simply speaking, you can mount the shared directory of the remote host to the local, just like the operation of the local disk 1, very convenient operation of remote files.

This article shows you how to install and configure the NFS server on CentOS7.

Without further ado, let's take a look at the details

To prepare

We need two CentOS7 machines. We use the virtual machine for testing and do NFS server and client respectively. The configuration is as follows:

NFS server ip: 192.168.11.31

Client ip: 192.168.11.34.

The goal is to share 1 directory on the NFS server and to operate directly on the client with files under this shared directory on the NFS server.

NFS server configuration

1. Install NFS services

First use yum to install nfs services:


yum -y install rpcbind nfs-utils

2. Create a shared directory

Create a shared directory on the server and set permissions.


mkdir /data/share/
chmod 755 -R /data/share/

3. The configuration NFS

The configuration file of nfs is /etc/exports. Add 1 line to the configuration file:


/data/share/ 192.168.11.34(rw,no_root_squash,no_all_squash,sync)

This line of code means that the shared directory /data/share/ is shared to the client ip 192.168.11.34. The following parenthesis is the permission parameter, where:

rw means that the Settings directory is read-write.

sync means that the data will be written to memory and hard disk synchronously, while rsync means that the data will be temporarily stored in memory instead of written directly to the hard disk.

no_root_squash NFS also has root permissions for directories shared by the server if the client is using root when connecting to the server.

no_all_squash No matter what user the NFS client uses when connecting to the server, it does not have anonymous user privileges for directories shared by the server.

If you have more than one shared directory configuration, use multiple rows, one for each configuration. After saving the configuration file, you need to execute the following command for the configuration to take effect immediately:


exportfs -r

4. Set up a firewall

If your system does not have a firewall on, this step can be omitted.

The FIREWALL of NFS is particularly difficult because in addition to the fixed port111, 2049, there are other services such as ES98en.mounted open the fixed port, which is more difficult for the firewall. To solve this problem, we can set the port configuration file for the NFS service.

Modify the /etc/sysconfig/nfs file to remove the following comments, or add if not:


RQUOTAD_PORT=1001
LOCKD_TCPPORT=30001
LOCKD_UDPPORT=30002
MOUNTD_PORT=1002

After saving, add the port to the firewall permission policy. Perform:


firewall-cmd --zone=public --add-port=111/tcp --add-port=111/udp --add-port=2049/tcp --add-port=2049/udp --add-port=1001/tcp --add-port=1001/udp --add-port=1002/tcp --add-port=1002/udp --add-port=30001/tcp --add-port=30002/udp --permanent
firewall-cmd --reload

5. Start the service

Start rpcbind and nfs services in order:


systemctl start rpcbind
systemctl start nfs

Add startup:


systemctl enable rpcbind 
systemctl enable nfs

After the nfs service is started, you can use the command rpcinfo-ES125en to see if the port is in effect.

After the server, we can use the showmount command to see whether the server (native) can connect:


[root@localhost ~]# showmount -e localhost
Export list for localhost:
/data/share 192.168.11.34

The above results indicate that the NFS server configuration is normal.

Client configuration

1. Install rpcbind services

The client only needs to install the rpcbind service without installing nfs or starting the nfs service.


yum -y install rpcbind

2. Mount remote nfs file system

View the directory shared by the server:


mkdir /data/share/
chmod 755 -R /data/share/
0

Set up the mount directory and execute the mount command:


mkdir /data/share/
chmod 755 -R /data/share/
1

If -ES158en is not added and nfsvers=3, the file in the mount directory is nobody for both owner and group, and root is displayed if nfsvers=3 is specified.

To unmount, execute the command:


mkdir /data/share/
chmod 755 -R /data/share/
2

3. Start up and mount automatically

If configured according to the above part of this article, NFS is ready to be deployed. However, if you restart the client system and find that it cannot be mounted with machine 1, you need to manually operate mount again, which is troublesome, so we need to set automatic mount on boot. We don't put the item mount wrote/etc fstab file, because the boot to mount the unit disk first restart the network, and NFS is need to network started to mount, so we put the mount command to write to/etc rc. d/rc local file.


[root@localhost ~]# vim /etc/rc.d/rc.local
# Add at the end of the file 1 Line: 
mount -t nfs 192.168.11.34:/data/share /mnt/share/ -o nolock,nfsvers=3,vers=3

Save and restart the machine.

The validation test

To view the mount results, type ES188en-h on the client side


mkdir /data/share/
chmod 755 -R /data/share/
4

If you see the last line, it's mounted successfully. Then you can go to the directory /mnt/share on the client side, create/delete the file, and then check the effect in the directory /data/share on the server side, and see the effect in the corresponding directory on the client side in reverse.

conclusion


Related articles: