NFS服务器简介、在Linux上搭建NFS服务器和客户端,使用autofs进行NFS客户端自动挂载和卸载详解

这篇具有很好参考价值的文章主要介绍了NFS服务器简介、在Linux上搭建NFS服务器和客户端,使用autofs进行NFS客户端自动挂载和卸载详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

一.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作为客户端。

两台虚拟机都下载rpcbindnfs-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客户端,Linux,服务器,linux,运维,centos,NFS

 需要在服务器端更改共享目录权限,如若没生效可以选择试试重启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 

nfs客户端,Linux,服务器,linux,运维,centos,NFS

2.windows客户端挂载测试

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

[root@localhost ~]# showmount -e    #160主机上搭建了nfs服务端
Export list for localhost.localdomain:
/root/data *

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

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

nfs客户端,Linux,服务器,linux,运维,centos,NFS

nfs客户端,Linux,服务器,linux,运维,centos,NFS 右键此电脑,点击映射网络驱动器

nfs客户端,Linux,服务器,linux,运维,centos,NFS

如下书写

nfs客户端,Linux,服务器,linux,运维,centos,NFS 等待连接完成即可

nfs客户端,Linux,服务器,linux,运维,centos,NFS

 nfs客户端,Linux,服务器,linux,运维,centos,NFS

(3)方法2

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

C:\Users\24107>mount 192.168.2.160:/root/data Z:
Z: 现已成功连接到 192.168.2.160:/root/data

命令已成功完成。

 nfs客户端,Linux,服务器,linux,运维,centos,NFS

 (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

//取消挂载成功

 文章来源地址https://www.toymoban.com/news/detail-761471.html

到了这里,关于NFS服务器简介、在Linux上搭建NFS服务器和客户端,使用autofs进行NFS客户端自动挂载和卸载详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • NFS客户端挂载nfs服务器共享目录时的报错及解决办法:mount.nfs: access denied by server while mounting

    客户端挂载nfs共享目录时的报错信息:mount.nfs: access denied by server while mounting 192.168.41.191:/nfsfile NFS服务器 1、我原来nfs配置文件exports里面的内容如下 2、重启rpcbind和nfs-server服务;查看了一下nfs-server服务,上面显示“Active: active (exited) \\\",意思是“活动:活动(已退出)”,这是

    2024年02月05日
    浏览(33)
  • 20230904 QT客户端服务器搭建聊天室

    Ser Cli

    2024年02月09日
    浏览(29)
  • Nginx HTTP/3服务器-客户端环境搭建

    一、 NGINX 服务器介绍: NGINX是一个高性能的开源Web服务器,也可用作反向代理服务器、负载均衡器和HTTP缓存。它由俄罗斯的程序员Igor Sysoev创建,并于2004年首次公开发布。NGINX的设计重点是高性能、高并发和低内存消耗,使其成为了现代Web架构中的关键组件之一。 NGINX的一些

    2024年03月11日
    浏览(47)
  • 用C语言搭建TCP服务器/客户端

    1.TCP流程图 2.TCP编程 服务器 客户端         以上就是用c语言搭建的tcp服务器和客户端,IP地址的地方可以根据自己本机的IP地址去修改(在命令提示符中可以使用ifconfig命令查看本机IP地址),端口号用的是6666,也可自己修改,但是IP地址和端口号服务器和客户端必须一至。

    2024年02月09日
    浏览(34)
  • QT实现TCP通信(服务器与客户端搭建)

    创建一个QTcpServer类对象,该类对象就是一个服务器 调用listen函数将该对象设置为被动监听状态,监听时,可以监听指定的ip地址,也可以监听所有主机地址,可以通过指定端口号,也可以让服务器自动选择 当有客户端发来连接请求时,该服务器会自动发射一个newConnection信号

    2024年02月09日
    浏览(39)
  • 【Android】MQTT入门——服务器部署与客户端搭建

    MQTT(Message Queuing Telemetry Transport) 是一种基于发布/订阅模式的轻量级消息传输协议,专门针对低带宽、和不稳定网络环境的物联网应用而设计,它可以用极少的代码为互联网设备提供实时可靠的消息服务。 MQTT 协议主要用于物联网和移动设备等资源有限的场景中,其中包括

    2024年02月04日
    浏览(52)
  • 简单FTP客户端软件开发——搭建FTP服务器

    计网课程设计的要求是: 1) 该FTP客户端程序具有以下基本功能: 2) 开发美观易用的图形界面 FTP使用客户服务器方式,因为开发的是客户端,所以需要一个FTP服务器,客户端进行连接访问,进行文件操作。 前面我们已经安装好了Linux虚拟机,在Linux搭建FTP服务器原参考博客

    2024年02月03日
    浏览(42)
  • mqtt服务器搭建与qt下的mqtt客户端实现

      MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一个基于客户端-服务器的消息发布/订阅传输协议。MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛。在很多情况下,包括受限的环境中,如:机器与机器(M2M)通信和物联网(Io

    2024年02月06日
    浏览(70)
  • ESP8266 ArduinoIDE 搭建web服务器与客户端开发

      此模式中,esp8266 会连接到指定 wifi 进行工作。    wifi 连接成功,esp8266 通过串口返回当前局域网 IP 此模式中,esp8266 会开启一个指定名称和密码的热点进行工作。      在这里改写了根目录,他会向客户端发送一个按钮的页面,点击即向 \\\"/LED\\\" 路由下发送 POST 请求。 由于

    2024年02月04日
    浏览(33)
  • 用Python搭建非常简易的TCP客户端和服务器

    以下属于Python Socket网络编程的基础,单纯为了测试学习 只要运行就会将内容发送至服务器 只要运行就会开始监听客户端请求 我们先运行TCP服务器(打开cmd,使用netstat命令可查看对应端口),然后运行TCP客户端,结果如下: 服务端: 客户端: 过几天写一篇用 C语言 搭建TCP服务

    2024年02月16日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包