Article Directory
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.
- Purpose: Share directories and files with others on the network
- Advantages: save disk space
- 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
/anongid
and/etc/exports
enable inall_squash
, manually change the UID/GID to solve this problem
2. nfs installation and activation
dnf search nfs
Inquirednf install nfs-utils.x86_64 -y
:installationsystemctl enable --now nfs-server
: Start service- 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
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 |
3. nfs configuration
- Configuration file:/etc/exports
#文件格式
共享目录 共享给谁(共享参数)
exportfs -rv
: After refreshing /etc/exports takes effect and outputs detailed informationshowmount -e 服务器主机IP
: View NFS server sharing informationmount 服务器主机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
mkdir /lwh
: Create a shared directorychmod 777 /lwh/
: Modify directory permissionstouch /lwh/lwh_file{1…3}
: Create a shared filevim /etc/exports
: Edit the configuration file
/lwh *(ro) #共享/lwh目录,共享给所有人,只读共享
共享目录 共享给谁(共享参数)
exportfs -rv
: After refreshing /etc/exports takes effect and outputs detailed information
3.2 Client test
showmount -e 192.168.43.101
: View NFS server sharing informationmount 192.168.43.101:/lwh /mnt/
: Mount
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
- Client mount test
4.2 User Settings 1
- Server-side configuration
- Client mount test
- Server-side control to establish file users and user groups
4.3 User Settings 2
- Server-side configuration
- Client mount test
4.4 Add version number to mount
mount -o vers=3 服务器端IP:共享目录 挂载目录
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
vim /etc/auto.master
: Edit the configuration file
/mnt /etc/auto.nfs
最终挂载点的上层目录 自动定义子策略文件
vim /etc/auto.nfs
: Edit the automatically defined sub-policy file
nfs -vers=3 192.168.43.101:/westosdir
最终挂载点 版本 挂载资源
vim /etc/autofs.conf
: Modify the default uninstall time
timeout = 3 #自动卸载时间默认为300秒
systemctl restart autofs.service
: Restart service- test
5.2 Auto-mounting experiment 2
5.2.1 Client autofs configuration
vim /etc/auto.master
: Edit the configuration file
/home /etc/auto.nfs
最终挂载点的上层目录 自动定义子策略文件
vim /etc/auto.nfs
: Edit the automatically defined sub-policy file
* -vers=3 192.168.43.101:/lwh/&
最终挂载点 版本 挂载资源
vim /etc/autofs.conf
: Modify the default uninstall time
timeout = 3 #自动卸载时间默认为300秒
systemctl restart autofs.service
: Restart service
5.2.2 Server settings
mkdir /lwh/sdsnzy1 /lwh/sdsnzy2
: Create directories and create files