File mount - Linux folder

Posted by Brandito520 on Fri, 31 Dec 2021 11:35:42 +0100

1, Overview

Folders are often attached to each other between different servers and different operating systems, such as file server or data backup server.
Generally speaking, there are four types of Mount:
Operating system of the same type
a. linux mount linux folder
b. windows mount windows folder
Different types of operating systems
c. linux mount windows folder
d. windows mount linux folder

nfs(Network File System) is used to mount linux folders in linux. This article will introduce this type.

2, Build

The experimental environment is two Linux: centos7 three
Server side: 192.168 56.77 server where files are actually stored
Client side: 192.168 56.88 mount the server, that is, mount the shared folder on the server side to the client side

nfs services are installed on both ends

# yum install -y nfs-utils

[Server side]
Turn off the firewall and Selinux

# systemctl stop firewalld
# systemctl disable firewalld
# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

Create shared directory

# mkdir /opt/test  
# chmod 777 /opt/test  

Modify nfs configuration

# vi /etc/exports
/opt/test 192.168.56.88(rw,all_squash,anonuid=1001,anongid=1002,async)  # anonuid and anongid represent all files created on the Client side. The last users and groups are 1001 and 1002, not the anonymous user nfsnobody.

Start nfs service

# service nfs start

[Client side]
Create a mount directory and mount remote folders

# mkdir /u01  
# mount -t nfs 192.168.56.77:/opt/test /u01

View the usage of this directory

# df -h /u01
Filesystem               Size  Used Avail Use% Mounted on
192.168.56.77:/opt/test   25G   16G  8.1G  66% /u01

When creating a file, you can see that the user and group of the file are 1001 and 1002 respectively. This is actually the corresponding server side / etc/exports setting

# touch abc
# ll
-rw-r--r--. 1 1001 1002 0 Dec 22 05:41 abc

If you want the system to mount automatically, you can set crontab to boot and mount, but it is not recommended to add it to / etc/fstab, because if there is a problem with nfs on the server side, the server on the client side will restart for a long time.

Configuration file / etc/exports considerations

  1. 192.168.56.88(rw...) Indicates that it is only shared with 192.168 56.88 this ip can also be written as 192.168 56. * means shared to this network segment, written as * (RW...) Indicates shared to all network segments.
  2. If there are no anonuid and anongid in the configuration file / etc/exports, the new file, user and group will become nfsnobody, specifically all_ For the explanation of the squash parameter, please see the content later in the article.

3, Problem summary

Problem 1: the nfs service on the server side is shut down, so that the client side cannot access the shared directory

[Server side]
Turn off the nfs service

# service nfs stop

[Client side]
ls / is stuck, df is also stuck, that is, all operations involving / u01 will be stuck. Execute the following command to view all nfs mounts

# mount -l |grep nfs
192.168.56.77:/opt/test on /u01 type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.56.88,local_lock=none,addr=192.168.56.77)

At the same time, check the operating system background log / var/log/message. You can locate 192.168 56.77 the nfs of this server has failed. Restart the nfs service on the server side, and the client side can operate normally after waiting for a period of time.

Dec 30 05:01:42 localhost kernel: nfs: server 192.168.56.77 not responding, still trying

It is not very friendly if the execution of commands is stuck due to the problem of nfs service. When nfs is mounted, the hard mount method is adopted by default. If the soft mount method is adopted, the front end will be prompted with an error when the command times out, rather than being blocked all the time.
Test:
Restart the NFS service on the server side service nfs start
client remount mount - t NFS - O soft, Timeo = 10, retry = 2 192.168 56.77:/opt/test /u01
Close the nfs service service service nfs stop on the Server side

The client side will not always block the execution of commands, but will report an error.

# df -h
df: '/u01': Input/output error
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/vg-lvroot   25G  1.2G   23G   6% /
devtmpfs               910M     0  910M   0% /dev
tmpfs                  920M     0  920M   0% /dev/shm
tmpfs                  920M  8.4M  912M   1% /run
tmpfs                  920M     0  920M   0% /sys/fs/cgroup
/dev/sda1              988M  110M  812M  12% /boot
tmpfs                  184M     0  184M   0% /run/user/0

Question 2: permission issues

In the configuration file / etc/exports
all_ Square: map all ordinary users and groups of remote access to anonymous users or user groups (nfsnobody), unless uid and gid are specified in the configuration
no_ all_ Square: do not map all ordinary users and groups of remote access to anonymous users or user groups, that is, use the uid and gid numbers of the file creator (the default setting)
root_ Square: map the root user and group to anonymous user or user group (default setting)
no_ root_ Square: do not map the root user and group to anonymous users or user groups

Different parameter settings will affect the permissions of files. Sometimes you will find that even the root user does not have permission to delete files. You can see whether these parameters map the root permission to an anonymous user. Based on space, there will be no demonstration here.

[root@localhost /u01]# rm -rf yangdir/yangsub 
rm: cannot remove 'yangdir/yangsub': Permission denied

Question 3: nfs port problem

When we set up nfs, we directly closed the firewall on the server side, but if the firewall is open, can it be mounted normally? If not, what port should we open?
Open firewall on server side

# systemctl start firewalld

When the client side is mounted, the command will be stuck all the time.

# mount -t nfs 192.168.56.77:/opt/test /u01

Open port 2049 on server side
Port opening command: firewall CMD -- zone = public -- add port = 2049 / TCP -- permanent
Restart firewall: systemctl restart firewalld
View opened ports: firewall CMD -- List ports

PS: I see that some articles say that port 111 needs to be opened, but the actual test shows that only port 2049 can be installed normally.

4, Summary

  1. nfs mount can be soft mount
  2. nfs file permissions can be set through / etc/exports
  3. The nfs port is 2049

Topics: Linux