Hello everyone, I am Internet old xin, this is the 16th day of my participation in wen Challenge, the activity details: Wen Challenge

Today we are going to discuss the use of NFS and the use of NFS to achieve automatic startup mount.

NFS Service Introduction

NFS is short for Network File System. Network file systems are one of the file systems supported by FreeBSD. NFS allows one system to share directories and files with others on the network.

Its main function is to share files or directories between different host systems through the network (generally a local area network)

Using NFS, users and programs can access files on a remote system as if they were accessing local files, as shown:

NFS mode: C/S Mode NFS listening port: 2049 RHEL7 uses NFSv4 as the default version. NFSv4 uses TCP (port 2049) to establish connections with the NFS server.

NFS port numbers are defined in /etc/services

NFS storage service role

  1. Realize shared storage of data
  2. Write data operation management
  3. Save the cost of buying server disk taobao – tens of thousands of electricity costs

NFS:

1) Yum install NFS

[root@gaosh-64 ~]# yum install rpcbind nfs-utils

2) Configuration file location
[root@gaosh-64 ~]# ls /etc/exports
/etc/exports
Copy the code
3) Start the NFS service
[root@gaosh-64 ~]# systemctl restart rpcbind
[root@gaosh-64 ~]# systemctl start nfs-server.service 
[root@gaosh-64 ~]# 
[root@gaosh-64 ~]# netstat -antup |grep 2049TCP 0 0 0.0.0.0:2049 0.0.0.0:* listen-tcp6 0 0: :2049 ::* listen-udp 0 0 0.0.0.0:2049 0.0.0.0:* -UDP6 0 0: :2049 : : : * -Copy the code

Method of use

1) Create a shared directory
[root@gaosh-64 ~]# mkdir -p /share/{dir1,dir2}
[root@gaosh-64 ~]Chmod -r 777 /share/
[root@gaosh-64 ~]# ll /share/Total amount 0 DRWXRWXRWX 2 root root 6 July 18 20:42 dir1 DRWXRWXRWX 2 root root 6 July 18 20:42 dir2Copy the code
2) Set up the profile and view it
[root@gaosh-64 ~]# cat /etc/exports
/share/dir1  *(ro)   # read-only/ share/dir2 192.168.1.0/24 (rw, sync)# to read and write
Copy the code
[root@gaosh-64 ~]# systemctl restart nfs.service
[root@gaosh-64 ~]# exportfs -v # check NFS output on current host/ share/dir2 192.168.1.0/24 (sync, wdelay, hide, no_subtree_check, the SEC = sys, rw, secure and root_squash, no_all_squash)/share/dir1-name <world>(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash) [root@gaosh-64 ~]# 

Copy the code
3) Client test
[root@gaosh-17 ~]Showmount -e 192.168.1.64
Export list for 192.168.1.64:
/share/dir1 *
/share/dir2 192.168.1.0/24
[root@gaosh-17 ~]# 

Copy the code

Mount test:

[root@gaosh-17 ~]# showmount -e 192.168.1.64
[root@gaosh-17 ~]# mkdir /mnt/test1
[root@gaosh-17 ~]# mkdir /mnt/test2
[root@gaosh-17 ~]# mount -t NFS 192.168.1.64:/share/dir1 / MNT /test1
[root@gaosh-17 ~]# mount -t NFS 192.168.1.64:/share/dir2 / MNT /test2
[root@gaosh-17 ~]# df -h192.168.1.64:/share/dir1 17G 4.7G 13G 28% / MNT /test1 192.168.1.64:/share/dir2 17G 4.7G 13G 28% / MNT /test2Copy the code

Test1 is read-only and test2 is read/write

To verify this, just copy a file and look at it

[root@gaosh-17 ~]# cp /etc/passwd/MNT /test1/Cp: common files cannot be created"/mnt/test1/passwd": read-only file system [root@gaosh-17 ~]# cp /etc/passwd /mnt/test2/   
Copy the code

Common NFS share parameters

We have used three parameters ro and RW and sync, and there are many more, as shown in the following table:

parameter role
ro Read-only access.
rw Read and write access.
sync Data is synchronously written to memory and hard disk.
async Data is stored temporarily in memory rather than written directly to hard disk.
secure NFS sends packets over secure TCP/IP ports less than 1024.
insecure NFS sends packets over ports larger than 1024.
wdelay If multiple users want to write to an NFS directory, group write (default).
no_wdelay If multiple users want to write to an NFS directory, write immediately. This setting is not required when async is used.
hide The subdirectory is not shared in an NFS shared directory.
no_hide Share the subdirectory of an NFS directory.
subtree_check If you share a subdirectory such as /usr/bin, force NFS to check permissions on the parent directory (default).
no_subtree_check In contrast to the above, do not check parent directory permissions.
all_squash The Uids and Gids of shared files map to anonymous users and are suitable for public directories.
no_all_squash Retain the UID and GID of the shared file (default).
root_squash All requests from the root user are mapped to the same permissions as anonymous users (default). ,
no_root_squash The root user has full administrative access to the root directory.

Example Set automatic NFS mounting

Directly set this parameter in the /etc/fstab configuration file

[root@gaosh-17 ~]# grep '192.168.1.64'/etc/fstab192.168.1.64:/share/dir1 / MNT /test1 NFS DEAults 0 0Copy the code

conclusion

This paper mainly discusses the installation of NFS, and the use of NFS boot automatic mount. To prevent single point NFS, keepalived is also highly available. When your current server hard disk is not enough, you can use NFS service to share the hard disk.