linux文件服务器NFS的安装与配置


一、几个概念
NFS 就是 Network File System 的缩写,最早是由 Sun 这家公司所发展出来的。 它最大的功能就是可以透过网络,让不同的机器、不同的操作系统、可以彼此分享个别的文件 (share files)。我们可以简单的将他看做是一个文件服务器 (file server) !这个 NFS 服务器可以让你的 PC 来将网络远程的 NFS 服务器分享的目录,挂载到本地端的机器当中, 在本地端的机器看起来,那个远程主机的目录就好像是自己的一个磁盘分区一样 (partition)!
RPC(Remote Procedure Call)——远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。NFS 服务器在启动的时候就得要向 RPC 注册,所以 NFS 服务器也是一种 RPC server 。
二、准备工作
安装好CentOS7.4Min的具有独立IP的两台或多台服务器。本例以2台机器为例,服务端(提供NFS服务的一端,即数据实际存放端):192.168.237.21,客户端:192.168.237.22。 架构图。
这里写图片描述 三、安装配置过程
1、服务端和客户端必须软件nfs-utils,事实上在安装nfs-utils的同时,rpcbind作为依赖包被安装,因此无需再单独安装rpcbind。单独安装rpcbind时不会同时安装nfs-utils。
[root@Geeklp-NFS-Server ~]# yum -y install nfs-utils
2、启动服务端的nfs和rpcbind服务,并将其设置为开机启动(作为服务器,应保证服务在每次重启之后都能迅速提供服务)

              [root@Geeklp-NFS-Server ~]# systemctl start nfs
              [root@Geeklp-NFS-Server ~]# systemctl enable nfs
              [root@Geeklp-NFS-Server ~]# systemctl status nfs
              ● nfs-server.service - NFS server and services
              Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
              Active: active (exited) since 四 2017-12-07 17:01:25 CST; 58s ago
              [root@Geeklp-NFS-Server ~]# systemctl start rpcbind
              [root@Geeklp-NFS-Server ~]# systemctl enable rpcbind
              [root@Geeklp-NFS-Server ~]# systemctl status rpcbind
              ● rpcbind.service - RPC bind service
              Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
              Active: active (running) since 四 2017-12-07 17:01:25 CST; 3min 26s ago
              Main PID: 35630 (rpcbind)
              CGroup: /system.slice/rpcbind.service
              └─35630 /sbin/rpcbind -w
              
3、在服务端创建共享目录。
[root@Geeklp-NFS-Server ~]# mkdir /nfs
4、修改服务端配置文件
修改服务端配置文件/etc/exports,增加如下一行:
/nfs 192.168.237.22/24(rw,sync)
5、依次重启服务端rpcbind、nfs服务
[root@Geeklp-NFS-Server ~]# systemctl restart rpcbind
[root@Geeklp-NFS-Server ~]# systemctl restart nfs
6、 配置服务端防火墙
[root@Geeklp-NFS-Server ~]# firewall-cmd --add-service=nfs --permanent
success
[root@Geeklp-NFS-Server ~]# firewall-cmd --reload
success
7、启动客户端nfs服务(客户端可以不启动rpcbind服务),挂载服务端nfs目录。
[root@Geeklp-NFS-Client ~]# systemctl start nfs
[root@Geeklp-NFS-Client ~]# mount -t nfs 192.168.237.21:/nfs /mnt
8、测试。
在客户端查看一下挂载情况。
              [root@Geeklp-NFS-Client mnt]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 8.3G 966M 7.4G 12% /
devtmpfs devtmpfs 478M 0 478M 0% /dev
tmpfs tmpfs 489M 0 489M 0% /dev/shm
tmpfs tmpfs 489M 6.8M 482M 2% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 xfs 247M 105M 142M 43% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
192.168.237.21:/nfs nfs4 14G 1.2G 13G 9% /mnt
正常挂载,无异常。
在服务端及客户端分别写入数据,发现只能在服务端对/nfs目录进行读写,而在客户端只能读取。 因为都是使用root账户进行操作的,在客户端看到的root是指服务端的root而并非客户端的root。 为了尽快使用、体验nfs服务,在服务端将/nfs目录的权限设置为7,此时就能愉快的体验nfs带来的乐趣了。但这并不严谨,不是我们想要达到的目标。
四、高级拓展部分
今天不是特别忙,一下子更新了好几篇博文,难免疏漏,还望诸位多多提出意见或建议。
一到公司,做完日常巡检,就继续来完成昨天未完成的任务了。发现了2个问题:
(1)客户端NFS挂载不上了,报错:mount.nfs: access denied by server while mounting 192.168.237.21:/nfs 我仔细检查了一遍防火墙、服务端nfs、rpcbind服务状态、网络等,都没有发现任何问题。 问题出在哪呢?忽然回忆起来,昨天关机之前改过/etc/exports文件,把括号内的内容改为了(rwx,sync),去掉x之后依次重启服务端rpcbind、nfs服务,挂载成功!
(2)客户端端无需启动rpcbind及nfs服务即可挂载NFS,但是需要安装nfs服务。
              [root@Geeklp-NFS-Client ~]# systemctl status rpcbind
              ● rpcbind.service - RPC bind service
              Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
              Active: inactive (dead)
              [root@Geeklp-NFS-Client ~]# systemctl status nfs
              ● nfs-server.service - NFS server and services
              Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
              Active: inactive (dead)
              [root@Geeklp-NFS-Client ~]# umount /mnt
              [root@Geeklp-NFS-Client ~]# mount -t nfs 192.168.237.21:/nfs /mnt
              [root@Geeklp-NFS-Client ~]# df -Th
              文件系统 类型 容量 已用 可用 已用% 挂载点
              /dev/mapper/centos-root xfs 8.3G 966M 7.4G 12% /
              devtmpfs devtmpfs 478M 0 478M 0% /dev
              tmpfs tmpfs 489M 0 489M 0% /dev/shm
              tmpfs tmpfs 489M 6.7M 482M 2% /run
              tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
              /dev/sda1 xfs 247M 105M 142M 43% /boot
              tmpfs tmpfs 98M 0 98M 0% /run/user/0
              192.168.237.21:/nfs nfs4 14G 1.1G 13G 8% /mnt
              
在客户端设置开机自动挂载NFS服务:
[root@Geeklp-NFS-Client ~]# echo “/nfs 192.168.237.21:/nfs /mnt nfs defaults 0 0”>>/etc/fstab
移除rpcbind安装包的时候,nfs也会作为依赖被删除。单独删除nfs-utils时并不会同时删除rpcbind。nfs-utils不能脱离rpcbind而单独存在。
[root@Geeklp-NFS-Client ~]# yum -y remove rpcbind
作为依赖被删除: nfs-utils.x86_64 1:1.3.0-0.48.el7_4 quota.x86_64 1:4.01-14.el7
移除rpcbind及nfs安装包时如果没有卸载NFS,那么之前挂载的NFS目录依然可以进行操作。但无法再次被挂载。要想挂载,必须安装nfs-utils。
扯远了,言归正传,说好在这一节咱们要来聊一聊NFS的高级配置的。以示区分,本次使用的NFS共享目录为:/NFS-Server。
(一)在服务端新建NFS共享目录/NFS-Server,在客户端新建挂载点目录/NFS-Client。
[root@Geeklp-NFS-Server ~]# mkdir /NFS-Server
[root@Geeklp-NFS-Client ~]# mkdir /NFS-Client
在此,我们先熟悉一下 /etc/exports 配置文件的语法与参数,为接下来的内容做准备。
[root@Geeklp-NFS-Server ~]# vim /etc/exports
/NFS-Server 192.168.237.22/24(ro) localhost(rw) *.geeklp.com(ro,sync)
/NFS-Server:共享目录
192.168.237.22/24(ro):共享给指定主机的IP,括号内为权限
localhost(rw):可使用主机名
*.geeklp.com(ro,sync):可使用域名,并且可以使用通配符
对于括号内的权限:
参数值 内容说明
              rw
              ro 该目录分享的权限是可擦写 (read-write) 或只读 (read-only),但最终能不能读写,还是与文件系统的 rwx及身份有关。
              sync
              async sync 代表数据会同步写入到内存与硬盘中,async 则代表数据会先暂存于内存当中,而非直接写入硬盘!
              no_root_squash
              root_squash 客户端使用 NFS 文件系统的账号若为 root 时,系统该如何判断这个账号的身份?预设的情况下,客户端 root 的身份会由 root_squash 的设定压缩成
              nfsnobody,如此对服务器的系统会较有保障。但如果你想要开放客户端使用 root 身份来操作服务器的文件系统,那么这里就得要开 no_root_squash 才行!
              all_squash 不论登入 NFS 的使用者身份为何, 他的身份都会被压缩成为匿名用户,通常也就是 nobody(nfsnobody)
              anonuid
              anongid anon 意指 anonymous (匿名者) 前面关于 *_squash 提到的匿名用户的 UID 设定值,通常为 nobody(nfsnobody),但是你可以自行设定这个 UID
              的值!当然,这个 UID 必需要存在于你的 /etc/passwd 当中!anonuid 指的是 UID 而 anongid 则是群组的 GID。
              
(二)几个应用案例。
1、全面共享NFS目录,让谁都能以root权限来操作共享目录里面的内容。
[root@Geeklp-NFS-Server /]echo "/NFS-Server *(rw,no_root_squash)">>/etc/exports
[root@Geeklp-NFS-Server /]# systemctl restart rpcbind
[root@Geeklp-NFS-Server /]# systemctl restart nfs
在客户端挂载此共享目录:
              [root@Geeklp-NFS-Client ~]# mount -t nfs 192.168.237.21:/NFS-Server /NFS-Client/
[root@Geeklp-NFS-Client /]# echo "Geeklp NFS test">/NFS-Client/a.txt
[root@Geeklp-NFS-Client /]# ls -l NFS-Client/
总用量 4 -rw-r--r--. 1 root root 16 12月 8 14:15 a.txt [root@Geeklp-NFS-Client /]# cat NFS-Client/a.txt Geeklp NFS test
回去服务端看一下:
              [root@Geeklp-NFS-Server /]# ll /NFS-Server/
              总用量 4
              -rw-r--r--. 1 root root 16 12月 8 14:15 a.txt
              [root@Geeklp-NFS-Server /]# cat /NFS-Server/a.txt
              Geeklp NFS test
              
不用在服务端修改权限咯!
2、在客户端卸载共享目录。再将/NFS-Server限定网段为192.168.237.0/24 且加入geeklpgroup的用户才能够读写,其他来源则只能读取。 此部分涉及到ACL权限管理的内容。
[root@Geeklp-NFS-Server /]# groupadd geeklpgroup
[root@Geeklp-NFS-Server /]# setfacl -m g:geeklpgroup:rwx /NFS-Server/
此时在客户端挂载此NFS共享目录,能够正常读,但是无法写入数据。在客户端也添加组geeklpgroup,创建用户geeklp并将其添加至geeklpgroup内。切换到geeklp用户,写入数据成功!
              [root@Geeklp-NFS-Client /]# groupadd geeklpgroup
              [root@Geeklp-NFS-Client NFS-Client]# useradd geeklp -g geeklpgroup
              [root@Geeklp-NFS-Server /]# echo "/NFS-Server 192.168.237.0/24(rw) *(ro)">/etc/exports
              [root@Geeklp-NFS-Server /]# systemctl restart rpcbind
              [root@Geeklp-NFS-Server /]# systemctl restart nfs
              [root@Geeklp-NFS-Client /]# mount -t nfs 192.168.237.21:/NFS-Server /NFS-Client
              [root@Geeklp-NFS-Client NFS-Client]# su geeklp
              [geeklp@Geeklp-NFS-Client NFS-Client]$ touch d.txt
              [geeklp@Geeklp-NFS-Client NFS-Client]$ ll
              总用量 0
              -rw-r--r--. 1 geeklp geeklpgroup 0 12月 8 15:44 d.txt
              
如果要指定特定用户对NFS共享目录有读写执行的权限,使用ACL权限管理一样能实现。
3、让主机192.168.237.22/24,登录NFS 主机时,可以读写NFS共享目录,并且在读写数据的时候都以指定的UID 与 GID来操作。例如指定uid为1001,gid为1002。
              [root@Geeklp-NFS-Server /]# groupdel geeklpgroup
              [root@Geeklp-NFS-Server /]# setfacl -b /NFS-Server
              [root@Geeklp-NFS-Client /]# umount /NFS-Client/
              [root@Geeklp-NFS-Client /]# userdel -r geeklp
              [root@Geeklp-NFS-Client /]# groupdel geeklpgroup
              [root@Geeklp-NFS-Server /]# useradd -u 1001 geeklp-nfs
              [root@Geeklp-NFS-Server /]# groupadd -g 1002 geeklp-nfs-g
              [root@Geeklp-NFS-Server /]# useradd -u 1001 geeklp-nfs
              [root@Geeklp-NFS-Server /]# id geeklp-nfs
              uid=1001(geeklp-nfs) gid=1001(geeklp-nfs) 组=1001(geeklp-nfs)
              [root@Geeklp-NFS-Server /]# groupadd -g 1002 geeklp-nfs-g
              [root@Geeklp-NFS-Server /]# echo "/NFS-Server
              192.168.237.22/24(rw,all_squash_root,anonuid=1001,anongid=1002)">/etc/exports
              [root@Geeklp-NFS-Server /]# cat /etc/exports
              /NFS-Server 192.168.237.22/24(rw,all_squash_root,anonuid=1001,anongid=1002)
              [root@Geeklp-NFS-Server ~]# chown geeklp-nfs /NFS-Server/
              [root@Geeklp-NFS-Server ~]# chgrp geeklp-nfs-g /NFS-Server/
              [root@Geeklp-NFS-Server ~]# systemctl restart rpcbind
              [root@Geeklp-NFS-Server ~]# systemctl restart nfs
              [root@Geeklp-NFS-Client /]# umount /NFS-Client/
              [root@Geeklp-NFS-Client /]# mount -t nfs 192.168.237.21:/NFS-Server /NFS-Client
              [root@Geeklp-NFS-Client NFS-Client]# touch geeklp.txt
              [root@Geeklp-NFS-Client NFS-Client]# ll
              总用量 0
              -rw-r--r--. 1 1001 1002 0 12月 8 18:23 geeklp.txt
              
在本例中需要注意的要点是:NFS的共享目录/NFS-Server的用户与组必须与/etc/exports中指定的用户uid和组gid相匹配,否则是无法进行写操作的。 用户可以属于exports中指定组,也可以不属于。
4、一些相关命令详解
(1)exportfs
如果我们在启动了NFS之后又修改了/etc/exports,是不是还要重新启动nfs呢?这个时候我们就可以用exportfs 命令来使改动立刻生效,该命令格式如下:
                 # exportfs [-aruv]
                -a 全部挂载或卸载 /etc/exports中的内容
                -r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab
                -u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)
                -v 在export的时候,将详细的信息输出到屏幕上。
              
具体例子:
  # exportfs -au 卸载所有共享目录
  # exportfs -rv 重新共享所有目录并输出详细信息
(2)nfsstat
查看NFS的运行状态。
(3)rpcinfo
查看rpc执行信息,可以用于检测rpc运行情况的工具,利用rpcinfo -p 可以查看出RPC开启的端口所提供的程序有哪些。
(4)showmount
  -a 显示已经于客户端连接上的目录信息
  -e IP或者hostname 显示此IP地址分享出来的目录
(参考资料:http://jlk.fjfi.cvut.cz/arch/manpages/listing/core/nfs-utils/)
————————————————
版权声明:本文为CSDN博主「MRIVANDU」的编辑整理,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/solaraceboy/article/details/78743563

100次点赞 100次阅读