[Linux study notes 24-2] network file system nfs + auto mount and uninstall autofs

1. Introduction to nfs

NFS (Network File System) is a network file system, one of the file systems supported by FreeBSD, which allows computers on the network to share resources through a TCP/IP network. In NFS applications, the local NFS client application can transparently read and write files on the remote NFS server, just like accessing local files.

  1. Purpose: Share directories and files with others on the network
  2. Advantages: save disk space
  3. Purpose: share files

note

  • NFS does not provide encryption. When handling sensitive data, please use protocols like Kerberos or a secure VPN to transmit NFS traffic
  • Unlike Samba , NFS does not provide any method of authenticating users by default, and client access restrictions are implemented through IP addresses and/or hostnames.
  • NFS expects the ID of user and/or user group to be the same on the client and server. Use to enable NFSv4 ID mapping , or use anonuid/ anongidand /etc/exportsenable in all_squash, manually change the UID/GID to solve this problem

2. nfs installation and activation

  1. dnf search nfsInquire
  2. dnf install nfs-utils.x86_64 -y:installation
  3. systemctl enable --now nfs-server: Start service
  4. Add firewall settings:
    • firewall-cmd --permanent --add-service=rpc-bind
    • firewall-cmd --permanent --add-service=mountd
    • firewall-cmd --permanent --add-service=nfs
    • firewall-cmd --reload
  5. showmount -e 服务器主机IP: View NFS server sharing information
showmount parameters
-e Display the share list of the NFS server
-a Display the status of file resources mounted on the machine (in the case of NFS resources)
-v Show version number

Insert picture description here
Insert picture description here

3. nfs configuration

  • Configuration file:/etc/exports
#文件格式
共享目录		共享给谁(共享参数)
  • exportfs -rv: After refreshing /etc/exports takes effect and outputs detailed information
  • showmount -e 服务器主机IP: View NFS server sharing information
  • mount 服务器主机IP:共享目录 挂载点: Mount
exportfs parameters
-r Re-read and synchronize /etc/exports and /etc/exports.d
-v Output detailed information
-a Mount or unmount all directories in /etc/exports
-u Uninstall a single directory (with a to uninstall all)
-s Display a list of current export directories applicable to /etc/exports

3.1 Server configuration

  1. mkdir /lwh: Create a shared directory
  2. chmod 777 /lwh/: Modify directory permissions
  3. touch /lwh/lwh_file{1…3}: Create a shared file
  4. vim /etc/exports: Edit the configuration file
/lwh	*(ro)	#共享/lwh目录,共享给所有人,只读共享
共享目录		共享给谁(共享参数)
  1. exportfs -rv: After refreshing /etc/exports takes effect and outputs detailed information

Insert picture description here

3.2 Client test

  1. showmount -e 192.168.43.101: View NFS server sharing information
  2. mount 192.168.43.101:/lwh /mnt/: Mount

Insert picture description here

4. nfs shared parameters

ro Read only
rw Read and write
sync Synchronize data to the server in real time (low efficiency, but ensure data consistency)
async Synchronize data to the server after the change is generated
root_squash The root user and its group are mapped to anonymous users or group nobody (default)
no_root_squash Root user mount does not change identity
all_squash No matter what identity the client accesses, it is mapped to the anonymous user nobody
anonuid = 1000 Specify user identity
anongid = 1000 Specify user group identity

4.1 Access rights

  • Server-side configuration

Insert picture description here

  • Client mount test

Insert picture description here

4.2 User Settings 1

  • Server-side configuration

Insert picture description here

  • Client mount test

Insert picture description here

  • Server-side control to establish file users and user groups

Insert picture description here

4.3 User Settings 2

  • Server-side configuration

Insert picture description here

  • Client mount test

Insert picture description here

4.4 Add version number to mount

mount -o vers=3 服务器端IP:共享目录 挂载目录

Insert picture description here

5. nfs+autofs automatically mount and uninstall

autofs: software that realizes automatic mounting and unloading on the client;

Mount is used to mount the file system. It can be mounted when the system is started or after the system is started. For local fixed devices, such as hard disks, you can use mount to mount; while file systems such as CDs, floppy disks, NFS, and SMB are dynamic, that is, they are only necessary to mount when needed. Optical drives and floppy disks are generally known when they need to be mounted, but NFS and SMB shares are not necessarily known, that is, we generally cannot know in time when NFS shares and SMB can be mounted. The autofs service provides this function, like the automatic opening function of the optical drive in windows, which can mount a dynamically loaded file system in time. Eliminate the trouble of manual mounting. To realize dynamic automatic mounting of CD-ROM, floppy disk, etc., relevant configuration is required.

dnf install autofs -y: Client install autofs

5.1 Auto-mounting experiment 1

  1. vim /etc/auto.master: Edit the configuration file
/mnt    /etc/auto.nfs
最终挂载点的上层目录	自动定义子策略文件

Insert picture description here

  1. vim /etc/auto.nfs: Edit the automatically defined sub-policy file
 nfs     -vers=3 192.168.43.101:/westosdir
 最终挂载点	版本	挂载资源

Insert picture description here

  1. vim /etc/autofs.conf: Modify the default uninstall time
timeout = 3	#自动卸载时间默认为300秒
  1. systemctl restart autofs.service: Restart service
  2. test

Insert picture description here

5.2 Auto-mounting experiment 2

5.2.1 Client autofs configuration

  1. vim /etc/auto.master: Edit the configuration file
/home   /etc/auto.nfs
最终挂载点的上层目录	自动定义子策略文件

Insert picture description here

  1. vim /etc/auto.nfs: Edit the automatically defined sub-policy file
*       -vers=3 192.168.43.101:/lwh/&
最终挂载点	版本	挂载资源

Insert picture description here

  1. vim /etc/autofs.conf: Modify the default uninstall time
timeout = 3	#自动卸载时间默认为300秒
  1. systemctl restart autofs.service: Restart service

5.2.2 Server settings

  1. mkdir /lwh/sdsnzy1 /lwh/sdsnzy2: Create directories and create files

Insert picture description here

5.2.3 Client Test

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46069582/article/details/110460706