Chapter 14 NFS Service Setup and Configuration
14.1 Introduction to NFS
NFS (Network File System) is a network file system, which is one of the file systems supported by FreeBSD. It allows computers in the network to share resources through the TCP/IP network. In NFS applications, local NFS client applications can transparently read and write files located on remote NFS servers, just like accessing local files. The data transmission of NFS is based on the RPC (remote procedure call) protocol.
Application scenarios
The files that need to be accessed on the three machines A, B, and C are the same. A shares the data, and B and C respectively mount the data directory shared by A, so that the data accessed by B and C is the same as that on A. .
14.2 NFS server installation configuration
Prepare two virtual machines, one as the server and one as the client.
Server
IP:192.168.8.130
Install NFS tools:
[root@1 ~]# yum install -y nfs-utils rpcbind
configure
[root@1 ~]# vim /etc/exports
/home/nfstestdir 192.168.8.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
#指定要进行分享的目录;指定要共享该目录的机器
创建分享目录并制定权限:
[root@1 ~]# mkdir /home/nfstestdir
[root@1 ~]# chmod 777 /home/nfstestdir
Start the NFS service
After the yum installation is complete, the system will automatically start the rpcbind service (the server process name is systemd), and the default listening port is port 111.
[root@1 ~]# systemctl start nfs
将NFS服务加入开机启动项:
[root@1 ~]# systemctl enable nfs
client
IP:192.168.8.132
Install NFS tools:
[root@1 ~]# yum install -y nfs-utils
client mount
- Check if the client has permission to access the server file:
[root@1 ~]# showmount -e 192.168.8.130
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
Error: Unable to connect to the server machine (network failure)!
Solution:
- Check whether the server NFS service is enabled (listening on port 111)
- If it is confirmed that the server NFS service is enabled, then check the firewall status, close the server and client firewalld and SELinux firewall
After solving the above error, execute the command again:
[root@1 ~]# showmount -e 192.168.8.130
Export list for 192.168.8.130:
/home/nfstestdir 192.168.8.0/24
That is, the client can normally access the server machine.
- start to mount
[root@1 ~]# mount -t nfs 192.168.8.130:/home/nfstestdir /mnt/
[root@1 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
192.168.8.130:/home/nfstestdir 18G 7.5G 11G 42% /mnt
test
Create a file in the client mount directory:
[root@1 ~]# cd /mnt/
[root@1 mnt]# ll
总用量 0
-rw-r--r-- 1 mysql mysql 0 8月 23 19:50 test123
View the server shared directory:
[root@1 ~]# ll /home/nfstestdir/
总用量 0
-rw-r--r--. 1 mysql mysql 0 8月 23 19:50 test123
That is, synchronous sharing is achieved!
14.3 NFS configuration options
[root[@localhost](https://my.oschina.net/u/570656) ~]# vim /etc/exports
/home/nfstestdir 192.168.8.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
#指定要进行分享的目录;指定要共享该目录的机器
#rw 读写;ro 只读
#sync 同步模式,内存数据实时写入磁盘
#async 非同步模式
#no_root_squash 客户端挂载NFS服务后,root用户不受约束,权限很大
#root_squash 与上面选项相对,客户端上的root用户受到约束,被限定成某个普通用户
#all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
#anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid