12月7日任务

14.1 NFS介绍

14.2 NFS服务端安装配置

14.3 NFS配置选项

NFS介绍

NFS是网络文件系统(Network File System)的缩写。需要借助网络,实现数据的同步。

NFS最早由Sun公司进行开发,分2,3,4三个版本,2和3版本有Sun公司起草开发,4.0开始由Netapp公司参与主导开发,最新版本为4.1版本。

NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。

NFS的应用场景是:A,B,C三台机器上需要保证被访问到的文件时一样的,A共享数据出来,B和C分别取挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。


NFS服务端安装配置

  • 软件安装
# 服务端安装
[root@server etc]# yum install -y nfs-utils rpcbind

# 客户端安装
[root@client etc]# yum install -y nfs-utils
  • 编辑/etc/exports文件
[root@server etc]
# 格式:分享的目录 共享的ip(挂载选项)
/home/nfstestdir 192.168.65.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
  • 创建共享目录并赋予权限
[root@server etc]# mkdir /home/nfstestdir

# 这里为了测试方便给予了777权限,实际工作中需要按需求修改
[root@server etc]# chmod 777 /home/nfstestdir/
  • 服务端启动nfs服务
[root@server etc]# systemctl start nfs
[root@server etc]# ps aux | grep nfs
root       2878  0.0  0.0      0     0 ?        S<   19:50   0:00 [nfsd4_callbacks]
root       2884  0.0  0.0      0     0 ?        S    19:50   0:00 [nfsd]
root       2885  0.0  0.0      0     0 ?        S    19:50   0:00 [nfsd]
root       2886  0.0  0.0      0     0 ?        S    19:50   0:00 [nfsd]
root       2887  0.0  0.0      0     0 ?        S    19:50   0:00 [nfsd]
root       2888  0.0  0.0      0     0 ?        S    19:50   0:00 [nfsd]
root       2889  0.0  0.0      0     0 ?        S    19:50   0:00 [nfsd]
root       2890  0.0  0.0      0     0 ?        S    19:50   0:00 [nfsd]
root       2891  0.0  0.0      0     0 ?        S    19:50   0:00 [nfsd]
root       2896  0.0  0.0 112680   976 pts/0    S+   19:50   0:00 grep --color=auto nfs

# 启动了nfs时会自动启动rpc服务,两者是关联的
[root@server etc]# ps aux | grep rpc
rpc        2644  0.0  0.0  64964  1412 ?        Ss   19:28   0:00 /sbin/rpcbind -w
rpcuser    2849  0.2  0.1  42380  1748 ?        Ss   19:50   0:00 /usr/sbin/rpc.statd
root       2850  0.0  0.0      0     0 ?        S<   19:50   0:00 [rpciod]
root       2862  0.0  0.0  19324   396 ?        Ss   19:50   0:00 /usr/sbin/rpc.idmapd
root       2868  0.0  0.0  42564   944 ?        Ss   19:50   0:00 /usr/sbin/rpc.mountd
root       2898  0.0  0.0 112680   976 pts/0    S+   19:50   0:00 grep --color=auto rpc
  • 设置服务端nfs服务开机启动
[root@server etc]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

NFS配置选项

/home/nfstestdir 192.168.65.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
  • rw 可读可写

  • ro 只读

  • sync 同步模式 内存数据实时写入磁盘,可以很快的将数据写入磁盘,相应的会降低磁盘的效率。

  • async 非同步模式 每个一段时间再将数据写入磁盘,缺点是当某一时刻断电,将会导致部分数据丢失。

  • no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大。当客户端需要共享目录的数据时,将该目录挂载到本机的某个目录,客户端的root权限可以随意对共享目录内的数据进行操作。

  • root_squash 相对于no_root_squash参数而言,客户端上的root用户在访问时被转换为某个普通用户,权限被限制。

  • all_squash 客户端上所有用户在使用NFS共享目录时都会被限制以某个普通用户的身份进行数据的操作。

  • anonuid/anongid 配合squash的参数,定义限制后的用户身份。

客户端挂载远程目录

  • showmount -e命令查看远程的共享目录
# 防火墙未放开rpc服务的111端口
[root@client ~]# showmount -e 192.168.65.133
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

# 服务端关闭防火墙和selinux
[root@server etc]# systemctl stop firewalld
[root@server etc]# setenforce 0

# 客户端成功访问
[root@client ~]# showmount -e 192.168.65.133
Export list for 192.168.65.133:
/home/nfstestdir 192.168.65.0/24
  • 挂载远程共享目录
[root@client ~]# mount -t nfs 192.168.65.133:/home/nfstestdir /mnt
[root@client ~]# df -h
文件系统                         容量  已用  可用 已用% 挂载点
/dev/sda3                         18G  3.6G   15G   20% /
devtmpfs                         479M     0  479M    0% /dev
tmpfs                            489M     0  489M    0% /dev/shm
tmpfs                            489M  6.7M  482M    2% /run
tmpfs                            489M     0  489M    0% /sys/fs/cgroup
/dev/sda1                        197M  109M   88M   56% /boot
tmpfs                             98M     0   98M    0% /run/user/0
192.168.65.133:/home/nfstestdir   18G  3.4G   15G   20% /mnt
  • 客户端/服务端文件说明
# 客户端挂载共享目录后,在该目录下创建一个新用户,其权限如下
[root@client ~]# cd /mnt/
[root@client mnt]# touch test
[root@client mnt]# ls -l
总用量 0
-rw-r--r--. 1 mysql mysql 0 1月   9 20:45 test

# 服务器也生成一个文件,其权限如下:
[root@localhost etc]# ls -l /home/nfstestdir/
总用量 0
-rw-r--r--. 1 mysql mysql 0 1月   9 20:45 test

[root@localhost etc]# id mysql
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)
12-10 00:47