目录

一.NFS服务器简介

1.含义简介:

2.工作原理简介:

3.RPC服务与NFS服务配合使用

二.NFS配置文件参数命令介绍

1.主配置文件/etc/exports

2.日志文件/var/lib/nfs/

3.showmount命令

三.主配置文件/etc/exports挂载写法

1.配置nfs服务端和客户端

2.windows客户端挂载测试

3.权限介绍

(1)rw/ro,服务器目录的共享权限是可读写/只读,但起决定作用的还是该目录的rwx权限

(2)sync/async,sync指数据会同步写入到内存与硬盘中,async则指数据会先暂存于内存当中,不直接写入硬盘

(3)no_root_squash/root_squash,若客户端在共享目录里创建的文件的所属者和所属组是root用户和root组,那么查看显示文件的属主和属组时有两种情况:

(4)all_squash/no_all_squash

(5)anonuid=,anongid=

四.在客户端配置auto自动挂载和卸载

1.安装autofs

2.auto建立客户端数据文件/etc/auto.master时配置参数(可以省略)

3.配置自动挂载/卸载过程

 

一.NFS服务器简介

1.含义简介:

NFS,网络文件系统,是FreeBSD支持的文件系统之一,允许网络中的不同的计算机、不同的操作系统之间通过TCP/IP网络共享资源。

2.工作原理简介:

本地NFS客户端应用可以读写位于远端NFS服务器(网络)上的文件,NFS服务器可以让计算机将网络中的NFS服务器共享的目录挂载到本地端的文件系统中,而在本地端的系统中看来,那个远程主机的目录类似于自己本地文件系统的一个磁盘分区。

3.RPC服务与NFS服务配合使用

 NFS的功能很多,每启动一个功能就会启用一些端口来传输数据,所以NFS的功能对应的端口并不固定,而是随机取用一些未被使用的小于1024的端口用于传输。需要借助RPC(远程过程调用)解决客户端连接服务器(端口)的问题。当启动NFS时会随机选取几个端口号,并主动向RPC注册,此时RPC知道每个NFS功能所对应的端口号,RPC将端口号下发给客户端,使得客户端可以连接到正确的端口上去。RPC采用固定端口号Port 111来监听客户端需求并向客户端下发正确的端口号。

 注意:RPC要在启动NFS之前启动才能实现NFS向RPC注册,如果重启RPC服务,NFS原来注册的数据会清楚,那么它管理的所有服务都需要重新启动重新注册。

二.NFS配置文件参数命令介绍

1.主配置文件/etc/exports

主配置文件如果不存在需要手动创建

编辑了主配置文件不需要重新启动服务,使用exportfs (-r)命令进行(全部)加载配置文件即可(同步更新/etc/exports,/var/lib/nfs/xtab)

exportfs其他选项:

-a    全部挂载或卸载/etc/exports中的内容 -u    卸载单一目录,可以和-a一起使用为卸载所有/etc/exports文件中的目录 -v    将详细的信息输出到屏幕上

2.日志文件/var/lib/nfs/

etab和xtab比较重要,etab主要记录NFS共享出来的目录的完整权限设置值,xtab主要记录以往链接到这个NFS服务器的相关客户端数据。

[root@sulibao ~]# ll /var/lib/nfs/

total 0

-rw-r--r-- 1 root root 0 Oct 14 2021 etab

-rw-r--r-- 1 root root 0 Jan 20 11:17 export-lock

drwx------ 2 root root 25 Jan 20 11:17 nfsdcltrack

-rw-r--r-- 1 root root 0 Oct 14 2021 rmtab

dr-xr-xr-x 11 root root 0 Dec 31 17:46 rpc_pipefs

drwx------. 4 rpcuser rpcuser 43 Jan 20 11:17 statd

-rw-r--r-- 1 rpcuser rpcuser 0 Oct 14 2021 state

drwxr-xr-x. 2 root root 6 Oct 14 2021 v4recovery

-rw-r--r-- 1 root root 0 Oct 14 2021 xtab

3.showmount命令

查看NFS共享出来的目录资源

showmount -e 主机名/ip表示显示NFS服务器的导出列表

三.主配置文件/etc/exports挂载写法

1.配置nfs服务端和客户端

如下是在Linux上创建服务端,并以Linux作为客户端进行测试

(1)准备两台虚拟机,关闭SElinux和防火墙,保证网络可用,有yum源,虚拟机1作为服务器端,虚拟机2作为客户端。

两台虚拟机都下载rpcbind和nfs-utils

[root@sulibao ~]# yum install -y rpcbind

[root@sulibao ~]# yum install -y nfs-utils

(2)通过“共享目录  主机名/IP/网段(权限)”编写配置进行挂载,权限可以为多个,写在括号里面以逗号隔开

服务器端写主配置文件

[root@sulibao ~]# vim /etc/exports

/nfsdir 192.168.2.0/24(rw)

注意:这里经常出现问题, (权限)与前面的字段之间多大了空格,此时权限不会生效!

(3)服务器端创建共享目录和文件并写入内容

[root@sulibao ~]# mkdir /nfsdir

[root@sulibao ~]# cd /nfsdir/

[root@sulibao nfsdir]# touch aaa.txt

[root@sulibao nfsdir]# vim aaa.txt

nfs-server

(4)先开启rpcbind服务再开启nfs-server服务

[root@sulibao nfsdir]# systemctl restart rpcbind

[root@sulibao nfsdir]# systemctl restart nfs-server

(5)在服务器端和客户端分别进行测试是否能够连接到共享目录

服务器端

[root@sulibao nfsdir]# showmount -e 192.168.2.160

Export list for 192.168.2.160

/nfsdir 192.168.2.0/24

[root@sulibao nfsdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 11:44 aaa.txt

客户端

[root@sulibao ~]# showmount -e 192.168.2.160

Export list for 192.168.2.160:

/nfsdir 192.168.2.0/24

(6)在客户端进行测试是否能够实现挂载

在客户端创建一个挂载目录,使用“mount 服务器IP:共享目录  挂载目录”进行挂载

[root@sulibao ~]# mkdir /mountdir

[root@sulibao ~]# mount 192.168.2.160:/nfsdir /mountdir/

[root@sulibao ~]# mount //通过mount可以查看有没有挂载记录

192.168.2.160:/nfsdir on /mountdir type nfs4 (rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.2.170,local_lock=none,addr=192.168.2.160)

[root@sulibao ~]# df -h 也可以通过df -h查看目录挂载情况

Filesystem Size Used Avail Use% Mounted on

devtmpfs 5.1G 0 5.1G 0% /dev

tmpfs 5.1G 0 5.1G 0% /dev/shm

tmpfs 5.1G 9.8M 5.1G 1% /run

tmpfs 5.1G 0 5.1G 0% /sys/fs/cgroup

/dev/mapper/rhel-root 35G 5.2G 30G 15% /

/dev/nvme0n1p1 1014M 257M 758M 26% /boot

tmpfs 1.1G 36K 1.1G 1% /run/user/0

/dev/sr0 11G 11G 0 100% /media/cdrom

192.168.2.160:/nfsdir 35G 5.1G 30G 15% /mountdir

在客户端也能查看到在服务器端上写的内容

[root@sulibao ~]# cd /mountdir/

[root@sulibao mountdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 11:44 aaa.txt

[root@sulibao mountdir]# cat aaa.txt

nfs-server

(7)注意:

这里主配置文件里设置的权限是可读可写但实际上其他用户共享这个目录时并不能新建或编辑文件

[root@sulibao mountdir]# touch bbb.txt

touch: cannot touch 'bbb.txt': Read-only file system

 需要在服务器端更改共享目录权限,如若没生效可以选择试试重启nfs-server

[root@sulibao nfsdir]# chmod 777 /nfsdir/

[root@sulibao nfsdir]# systemctl restart nfs-server.service

在客户端就可以修改共享目录内文件或者新建文件了

[root@sulibao mountdir]# touch bbb.txt

[root@sulibao mountdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

[root@sulibao mountdir]# vim aaa.txt 

2.windows客户端挂载测试

(1)这里以一个新例子做介绍

[root@localhost ~]# showmount -e #160主机上搭建了nfs服务端

Export list for localhost.localdomain:

/root/data *

(2)到windows上进行挂载,方法1

到控制面板开启NFS客户端和管理工具功能

 右键此电脑,点击映射网络驱动器

如下书写

 等待连接完成即可

 

(3)方法2

开启NFS客户端和管理工具后直接在cmd内进行挂载

C:\Users\24107>mount 192.168.2.160:/root/data Z:

Z: 现已成功连接到 192.168.2.160:/root/data

命令已成功完成。

 

 (4)如上通过驱动器映射和命令行的挂载若需要卸载,建议都使用命令行进行卸载

C:\Users\24107>umount Z:

正在断开连接 Z: \\192.168.2.160\root\data

命令已成功完成。

3.权限介绍

(1)rw/ro,服务器目录的共享权限是可读写/只读,但起决定作用的还是该目录的rwx权限

(2)sync/async,sync指数据会同步写入到内存与硬盘中,async则指数据会先暂存于内存当中,不直接写入硬盘

(3)no_root_squash/root_squash,若客户端在共享目录里创建的文件的所属者和所属组是root用户和root组,那么查看显示文件的属主和属组时有两种情况:

no_root_squash指将root属主和组映射为root属主和组(默认的)

服务器端修改主配置文件/etc/exports,重新加载配置文件并测试

[root@sulibao nfsdir]# vim /etc/exports

/nfsdir 192.168.2.0/24(rw,no_root_squash)

[root@sulibao nfsdir]# exportfs -r

[root@sulibao nfsdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

客户端测试

[root@sulibao mountdir]# touch ccc.txt

[root@sulibao mountdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

root_squash指将root属主和组映射为匿名用户和组(默认的)

 服务器端修改主配置文件/etc/exports,重新加载主配置文件并测试

[root@sulibao nfsdir]# vim /etc/exports

/nfsdir 192.168.2.0/24(rw,root_squash)

[root@sulibao nfsdir]# exportfs -r

[root@sulibao nfsdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt

 客户端测试

[root@sulibao mountdir]# touch ddd.txt

[root@sulibao mountdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt

(4)all_squash/no_all_squash

(1)all_squash指客户端普通用户创建文件时,会将文件的用户和组映射为匿名用户和匿名组

服务器端修改朱培志文件,重新加载配置文件并测试

[root@sulibao nfsdir]# vim /etc/exports

/nfsdir 192.168.2.0/24(rw,no_root_squash,all_squash)

[root@sulibao nfsdir]# exportfs -r

[root@sulibao nfsdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt

客户端普通用户下测试

[root@sulibao mountdir]# su - xiaosu

[xiaosu@sulibao ~]$ cd /mountdir/

[xiaosu@sulibao mountdir]$ ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt

[xiaosu@sulibao mountdir]$ touch ggg.txt

[xiaosu@sulibao mountdir]$ ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt

(2)no_all_squash指客户端普通用户创建的文件的UID和GID是多少服务器端就映射为UID和GID对应的用户

服务器端修改主配置,重新加载配置文件

[root@sulibao nfsdir]# vim /etc/exports

/nfsdir 192.168.2.0/24(rw,no_root_squash,no_all_squash)

[root@sulibao nfsdir]# exportfs -r

[root@sulibao nfsdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt

客户端普通用户下对应服务器端进行测试,这里普通用户的UID和GID是1000,那么服务器上就会映射出服务器上UID和GID为1000的用户或者UID和GID值

[root@sulibao mountdir]# su - xiaosu

[xiaosu@sulibao ~]$ cd /mountdir/

[xiaosu@sulibao mountdir]$ ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt

-rw-r--r-- 1 root root 0 Jan 20 15:41 hhh.txt

[xiaosu@sulibao mountdir]$ touch iii.txt

[xiaosu@sulibao mountdir]$ ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt

-rw-r--r-- 1 root root 0 Jan 20 15:41 hhh.txt

-rw-rw-r-- 1 xiaosu xiaosu 0 Jan 20 15:42 iii.txt

[xiaosu@sulibao mountdir]$ cat /etc/passwd |grep xiaosu

xiaosu:x:1000:1000:xiaosu:/home/xiaosu:/bin/bash

//客户端普通用户xiaosu的UID和GID为1000

[root@sulibao nfsdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt

-rw-r--r-- 1 root root 0 Jan 20 15:41 hhh.txt

-rw-rw-r-- 1 sulibao sulibao 0 Jan 20 15:42 iii.txt

[root@sulibao nfsdir]# cat /etc/passwd | grep 1000

sulibao:x:1000:1000:sulibao:/home/sulibao:/bin/bash

//服务器端上UID=GID=1000匹配的是普通用户sulibao,就映射sulibao

[root@sulibao mountdir]# useradd -u 6666 susu

[root@sulibao mountdir]# cat /etc/passwd | grep 6666

susu:x:6666:6666::/home/susu:/bin/bash

[root@sulibao mountdir]# chown susu:susu hhh.txt

[root@sulibao mountdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt

-rw-r--r-- 1 susu susu 0 Jan 20 15:41 hhh.txt

-rw-rw-r-- 1 xiaosu xiaosu 0 Jan 20 15:42 iii.txt

//客户端普通用户susu的UID和GID为6666,将hhh.txt属主和组改为susu

[root@sulibao nfsdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt

-rw-r--r-- 1 6666 6666 0 Jan 20 15:41 hhh.txt

-rw-rw-r-- 1 sulibao sulibao 0 Jan 20 15:42 iii.txt

//服务器端没有与UID=GID=6666的用户和组,就直接映射UID和GID

(5)anonuid=,anongid=

将文件的用户和组映射为指定的UID和GID或相匹配的用户,若不指定默认为65534(nfsnobody)

服务器端修改主修改配置文件并重新加载配置文件

[root@sulibao nfsdir]# vim /etc/exports

/nfsdir 192.168.2.0/24(rw,no_root_squash,all_squash,anonuid=5555,anongid=5555)

[root@sulibao nfsdir]# exportfs -r

[root@sulibao nfsdir]# useradd -u 5555 slb

客户端测试,客户端没有此UID和GID的用户就会显示UID和GID值,有就显示值相匹配的用户

[root@sulibao mountdir]# touch qqq.txt

[root@sulibao mountdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt

-rw-r--r-- 1 susu susu 0 Jan 20 15:41 hhh.txt

-rw-rw-r-- 1 xiaosu xiaosu 0 Jan 20 15:42 iii.txt

-rw-r--r-- 1 5555 5555 0 Jan 20 16:06 qqq.txt

//客户端上没有

[root@sulibao nfsdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt

-rw-r--r-- 1 6666 6666 0 Jan 20 15:41 hhh.txt

-rw-rw-r-- 1 sulibao sulibao 0 Jan 20 15:42 iii.txt

-rw-r--r-- 1 slb slb 0 Jan 20 16:06 qqq.txt

//服务器端上刚创建了slb用户,UID和GID相匹配,映射slb用户

四.在客户端配置auto自动挂载和卸载

auto自动挂载主要是用来实现当客户端有挂载需求时才进行挂载,基于上文的服务器虚拟机和客户端虚拟机进行演示

1.安装autofs

[root@sulibao nfsdir]# yum install -y autofs

2.auto的配置文件介绍

(1)/etc/auto.master

用于auto挂载的设置,auto挂载信息的映射关系

格式:客户端挂载目录   (参数)  挂载auto配置文件的目录

# 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 //表示/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

(2)/etc/autofs.conf

autofs服务的配置,主要关注timeout,表示切除目录后多少时间自动解除挂载

timeout = 300 //默认是300秒

(3)/etc/auto.misc

auto挂载的信息

# 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

2.auto建立客户端数据文件/etc/auto.master时配置参数(可以省略)

参数实现功能fg/bg fg表示挂载行为在前台执行,bg表示挂载行为在后台执行。前台执行,则mount会持续尝试挂载,直到成功或超时为止。后台执行,mount会在后台持续多次进行mount,不会影响到前台的程序运行。 soft/hard hard表示当两者之间的任何一台主机脱机,则RPC会持续地呼叫,直到对方恢复连接为止。如果是soft的话,那RPC会在超时后重复呼叫,非持续呼叫 intr 当使用hard方式挂载时,若加上intr这个参数,则当RPC持续呼叫时,该次的呼叫是可以被中断的 rsize/wsize 读出(rsize)与写入(wsize)的区块大小,设置值可以影响客户端与服务器端传输数据的缓冲记忆容量

3.配置自动挂载/卸载过程

服务器端不动,autofs主要配置在客户端

(1)安装好autofs后编辑配置文件/etc/auto.master

找到/misc   /etc/auto.misc这一行,在其后或前写一行,指定/etc/auto.nfs的自动挂载信息在/nfscli下

[root@sulibao ~]# vim /etc/auto.master

/nfscli  /etc/auto.nfs

/misc   /etc/auto.misc

//nfscli不需要手动创建,稍后重启autofs服务会自动创建

(2)编辑新建刚才指定的文件/etc/auto.nfs

指定客户端挂载地址和服务器端共享目录地址

[root@sulibao ~]# vim /etc/auto.nfs

mountdir 192.168.2.160:/nfsdir

(3)重启autofs服务,查看/nfscli目录是否自动创建

[root@sulibao ~]# systemctl restart autofs.service

[root@sulibao ~]# ls -ld /nfscli

drwxr-xr-x 2 root root 0 Jan 20 17:49 /nfscli

(4)触发自动挂载(切入客户端挂载目录mountdir即触发)

切入前先使用mount命令查看挂载记录方便查看是否生效(如果上文配置的临时挂载仍处于挂载状态,可以使用“umount -lf 目录”取消挂载)

[root@sulibao ~]# mount | tail -5

/dev/sr0 on /media/cdrom type iso9660 (ro,relatime,nojoliet,check=s,map=n,blocksize=2048,uid=0,gid=0,dmode=500,fmode=400)

nfsd on /proc/fs/nfsd type nfsd (rw,relatime)

/etc/auto.nfs on /nfscli type autofs (rw,relatime,fd=5,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=243315)

/etc/auto.misc on /misc type autofs (rw,relatime,fd=11,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=241423)

-hosts on /net type autofs (rw,relatime,fd=17,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=243320)

切入目录/nfscli,此时查看该目录内并没有内容,但是仍然可以进入客户端挂载目录mountdir,进入可以查看到服务器端共享目录内容,通过mount查看,自动挂载成功

[root@sulibao ~]# cd /nfscli

[root@sulibao nfscli]# ll

total 0

[root@sulibao nfscli]# cd mountdir

[root@sulibao mountdir]# ll

total 4

-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt

-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt

-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt

-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt

-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt

-rw-r--r-- 1 6666 6666 0 Jan 20 15:41 hhh.txt

-rw-rw-r-- 1 xiaosu xiaosu 0 Jan 20 15:42 iii.txt

-rw-r--r-- 1 5555 5555 0 Jan 20 16:06 qqq.txt

[root@sulibao mountdir]# mount | tail -5

nfsd on /proc/fs/nfsd type nfsd (rw,relatime)

/etc/auto.nfs on /nfscli type autofs (rw,relatime,fd=5,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=243315)

/etc/auto.misc on /misc type autofs (rw,relatime,fd=11,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=241423)

-hosts on /net type autofs (rw,relatime,fd=17,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=243320)

192.168.2.160:/nfsdir on /nfscli/mountdir type nfs4 (rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.2.170,local_lock=none,addr=192.168.2.160)

(5)设置自动取消挂载

编辑/etc/autofs.conf,方便测试将timeout时间改短一点

[root@sulibao mountdir]# vim /etc/autofs.conf

# Define default options for autofs.

#

[ autofs ]

#

# master_map_name - default map name for the master map.

#

#master_map_name = auto.master

#

# timeout - set the default mount timeout in secons. The internal

# program default is 10 minutes, but the default installed

# configuration overrides this and sets the timeout to 5

# minutes to be consistent with earlier autofs releases.

#

timeout = 10

[root@sulibao mountdir]# systemctl restart autofs.service

[root@sulibao mountdir]# cd /nfscli/mountdir/ //重启测试能够自动挂载

[root@sulibao mountdir]# df -h

Filesystem Size Used Avail Use% Mounted on

devtmpfs 5.1G 0 5.1G 0% /dev

tmpfs 5.1G 0 5.1G 0% /dev/shm

tmpfs 5.1G 9.8M 5.1G 1% /run

tmpfs 5.1G 0 5.1G 0% /sys/fs/cgroup

/dev/mapper/rhel-root 35G 5.2G 30G 15% /

/dev/nvme0n1p1 1014M 257M 758M 26% /boot

tmpfs 1.1G 36K 1.1G 1% /run/user/0

/dev/sr0 11G 11G 0 100% /media/cdrom

192.168.2.160:/nfsdir 35G 5.1G 30G 15% /nfscli/mountdir

切出挂载目录触发取消挂载再等10秒再查看挂载情况

[root@sulibao mountdir]# cd

[root@sulibao ~]# df -h

Filesystem Size Used Avail Use% Mounted on

devtmpfs 5.1G 0 5.1G 0% /dev

tmpfs 5.1G 0 5.1G 0% /dev/shm

tmpfs 5.1G 9.8M 5.1G 1% /run

tmpfs 5.1G 0 5.1G 0% /sys/fs/cgroup

/dev/mapper/rhel-root 35G 5.2G 30G 15% /

/dev/nvme0n1p1 1014M 257M 758M 26% /boot

tmpfs 1.1G 36K 1.1G 1% /run/user/0

/dev/sr0 11G 11G 0 100% /media/cdrom

//取消挂载成功

 

精彩链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。