Linux-NFS总结

NFS服务

  • NFS:Network File System 网络文件系统,基于内核的文件系统。通过使用 NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件
  • 基于RPC(Remote Procedure CallProtocol 远程过程调用)实现RPC采用C/S模式,客户机请求程序调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。
  • 在服务器端,进程保持睡眠状态直到调用信息到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息,获得进程结果,然后调用执行继续进行

NFS优势:节省本地存储空间,将常用的数据,如:/home目录,存放在NFS服务器上且可以通过网络访 问,本地终端将可减少自身存储空间的使用

NFS软件介绍

  • 软件包:nfs-utils
  • 相关软件包:rpcbind(必须),tcp_wrappers
  • Kernel支持:nfs.ko
  • 端口:2049(nfsd), 其它端口由portmap(111)分配

NFS服务主要进程:

  • rpc.nfsd 最主要的NFS进程,管理客户端是否可登录
  • rpc.mountd 挂载和卸载NFS文件系统,包括权限管理
  • rpc.lockd 非必要,管理文件锁,避免同时写出错
  • rpc.statd 非必要,检查文件一致性,可修复文件

日志:/var/lib/nfs/

NFS配置文件:

/etc/exports
/etc/exports.d/*.exports

NFS共享配置文件格式

/dir 主机1(opt1,opt2) 主机2(opt1,opt2)...

格式说明:

  • 以#开始的行为注释
  • 主机格式:
anonymous:表示使用*通配所有客户端
单个主机:ipv4,ipv6,FQDN

IP networks:两种掩码格式均支持
172.18.0.0/255.255.0.0
172.18.0.0/16

wildcards:主机名通配,例如:*.magedu.com,IP不可以

netgroups:NIS域的主机组,@group_name
  • 每个条目指定目录导出到的哪些主机,及相关的权限和选项
默认选项:(ro,sync,root_squash,no_all_squash)
ro,rw 只读和读写
async 异步,数据变化后不立即写磁盘,先写入到缓冲区中,过一段时间再写入磁盘,性能高,安全性低
sync(
1.0.0后为默认)同步,数据在请求时立即写入共享存储磁盘,性能低,安全性高 root_squash (默认)远程root映射为nfsnobody,UID为65534,CentOS8 为nobody,CentOS
7以前的版本为nfsnobody
no_root_squash 远程root映射成NFS服务器的root用户 all_squash 所有远程用户(包括root)都变成nfsnobody,CentOS8 为nobody
no_all_squash (默认)保留共享文件的UID和GID anonuid和anongid 指明匿名用户映射为特定用户UID和组GID,而非nobody,可配合all_squash使 用

 NFS工具

rpcinfo

rpcinfo 工具可以查看RPC相关信息

查看注册在指定主机的RPC程序

rpcinfo -p hostname
[root@centos7-liyj ~]#rpcinfo -p 10.0.0.27
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper

查看RPC注册程序

rpcinfo -s hostname 
[root@centos7-liyj ~]#rpcinfo -s 10.0.0.27
   program version(s) netid(s)                         service     owner
    100000  2,3,4     local,udp,tcp,udp6,tcp6          portmapper  superuser

exportfs

exportfs:可用于管理NFS导出的文件系统

常见选项:

-v #查看本机所有NFS共享
-r #重读配置文件,并共享目录
-a #输出本机所有共享
-au #停止本机所有共享

showmount

常见用法:

#查看远程主机的NFS共享
showmount -e hostname

mount.nfs

客户端NFS挂载

NFS相关的挂载选项:man 5 nfs

fg     #(默认)前台挂载
bg     #后台挂载
hard     #(默认)持续请求
soft      #非持续请求
intr      #和hard配合,请求可中断
rsize    #和wsize 一次读和写数据最大字节数,rsize=32768
_netdev   #无网络服务时不挂载NFS资源
vers      #指定版本,客户端centos8默认4.2 ,centos7默认4.1 centos6默认4.0

提示:基于安全考虑,建议使用 nosuid,_netdev,noexec 挂载选项

示例

临时挂载NFS共享

mount -o rw,nosuid,fg,hard,intr 172.16.0.1:/testdir /mnt/nfs/

开机挂载

vim /etc/fstab  
172.16.0.1:/public   /mnt/nfs   nfs   defaults,_netdev  0  0

自动挂载

可使用 autofs 服务按需要挂载外围设备,NFS共享等,并在空闲5分钟后后自动卸载

 

相关包和文件

  • 软件包:autofs
  • 服务文件:/usr/lib/systemd/system/autofs.service
  • 配置文件:/etc/auto.master

配置文件格式

参看帮助:man 5 autofs

所有导出到网络中的NFS启动特殊匹配-host至“brows”

[root@centos7-liyj ~]#cat /etc/auto.master
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
/misc    /etc/auto.misc
#
# NOTE: mounts done from a hosts map will be mounted with the
#    "nosuid" and "nodev" options unless the "suid" and "dev"
#    options are explicitly given.
#
/net    -hosts
#
# Include /etc/auto.master.d/*.autofs
# The included files must conform to the format of this file.
#
+dir:/etc/auto.master.d
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master
auto.master

自动挂载资源有两种格式:

  • 相对路径法:将mount point 路径分成 dirname 和 basename 分别配置,可能会影响现有的目录结构
  • 绝对路径法:直接匹配全部的绝对路径名称,都写入到指定的配置文件里,不会影响本地目录结构

相对路径法

1. /etc/auto.master 格式

挂载点的dirname     指定目录的配置文件路径,如:/etc/test.auto

2. 指定目录的配置文件格式

#/etc/test.auto
挂载点的basename     挂载选项     挂载的设备

范例:相对路径法

[root@centos7-liyj ~]#cat /etc/auto.master
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
/misc    /etc/auto.misc

 

[root@centos7-liyj ~]#cat /etc/auto.misc 
#
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage

cd        -fstype=iso9660,ro,nosuid,nodev    :/dev/cdrom

# the following entries are samples to pique your imagination
#linux        -ro,soft,intr        ftp.example.org:/pub/linux
#boot        -fstype=ext2        :/dev/hda1
#floppy        -fstype=auto        :/dev/fd0
#floppy        -fstype=ext2        :/dev/fd0
#e2floppy    -fstype=ext2        :/dev/fd0
#jaz        -fstype=ext2        :/dev/sdc1
#removable    -fstype=ext2        :/dev/hdd

绝对路径法

1. /etc/auto.master 格式

/-         指定配置文件路径

2. 指定配置文件格式

挂载点完整的绝对路径       挂载选项     挂载设备

绝对路径法

vim /etc/auto.master:
/-     /etc/auto.direct
vim
/etc/auto.direct: /foo          -fstype=nfs server1:/export/foo /user/local/      -fstype=nfs,vers=3 server1:/usr/local /mnt/cdrom        -fstype=iso9660 :/dev/cdrom

 

posted @ 2022-05-28 12:35  goodbay说拜拜  阅读(105)  评论(0编辑  收藏  举报