Linux学习--NFS和Autofs自动挂载
作者:互联网
Linux学习–NFS和Autofs自动挂载
NFS服务(network file sharing)
一、安装服务
[root@localhost ~]# dnf install nfs-utils
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 0:00:24 ago on Tue 12 Oct 2021 05:16:20 AM CST.
Package nfs-utils-1:2.3.3-14.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost ~]# service nfs-server status
Redirecting to /bin/systemctl status nfs-server.service
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@localhost ~]# rpm -qf /usr/lib/systemd/system/nfs-server.service
nfs-utils-2.3.3-14.el8.x86_64
二、配置文件
配置文件 /etc/exprots
/nfsfile 192.168.31.11(rw,no_root_squash,async)
共享目录 可以访问这个目录的IP地址 (可以实IP地址可以是网段,:所有人可访问;网段:192.168.31. * ;192.168.31.0/24 )
可以在/var/lib/nfs/etab中查看相关的参数
参数 | 作用 |
---|---|
ro | 只读 |
rw | 读写 |
root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户 |
no_root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员 |
all_squash | 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户 |
sync | 同时将数据写入到内存与硬盘中,保证不丢失数据 |
async | 优先将数据保存到内存,然后再写入硬盘;这样效率更高,同时可能会丢失数据 |
三、启动RPC
NFS服务进程文件共享时需要启动RPC(Remote Procedure Call,远程过程调用)服务将NFS服务器的IP地址和端口号信息发送给客户端
[root@localhost init.d]# systemctl restart rpcbind
[root@localhost init.d]# systemctl enable rpcbind
[root@localhost init.d]# systemctl restart nfs-server
[root@localhost init.d]# systemctl enable nfs-server
NFS服务默认端口使用的是2049
[root@localhost yum.repos.d]# grep -i nfs /etc/services
nfs 2049/tcp nfsd shilp # Network File System
nfs 2049/udp nfsd shilp # Network File System
nfs 2049/sctp nfsd shilp # Network File System
RPC的默认端口为111
[root@localhost ~]# grep -i rpcbind /etc/services
sunrpc 111/tcp portmapper rpcbind # RPC 4.0 portmapper TCP
sunrpc 111/udp portmapper rpcbind # RPC 4.0 portmapper UDP
四、挂载
在客户端可以通过showmount命令来查看NFS服务器上共享了哪些文件夹
参数 | 作用 |
---|---|
-e | 显示NFS服务器的共享列表 |
-a | 显示本机挂载的文件资源的情况NFS资源的情况 |
-v | 显示版本号 |
[root@localhost ~]# showmount -e 192.168.31.10
Export list for 192.168.31.10:
/nfsfile 192.168.31.11
挂载NFS服务器共享出来的资源
[root@localhost ~]# mkdir /nfsfile
[root@localhost ~]# mount -t nfs 192.168.31.10:/nfsfile/ /nfsfile/
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 4.1G 13G 24% /
devtmpfs 894M 0 894M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 11M 900M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 182M 0 182M 0% /run/user/0
tmpfs 182M 24K 182M 1% /run/user/1000
/dev/sr0 4.3G 4.3G 0 100% /run/media/linux/CentOS 7 x86_64
192.168.31.10:/nfsfile 17G 3.9G 14G 23% /nfsfile
五、排错
启动nfs时报错
[root@localhost ~]# systemctl start nfs-server.service
Job for nfs-server.service canceled.
打开日志,然后再启动nfs看日志内容,从日志中看到,/etc/exports文件的配置有错误,无法试别sysnc这个参数
[root@localhost ~]# tail -f /var/log/messages
Oct 22 23:34:19 localhost systemd[1]: Starting NFS server and services...
Oct 22 23:34:19 localhost exportfs[4190]: exportfs: /etc/exports:1: unknown keyword "sysnc"
Oct 22 23:34:19 localhost systemd[1]: nfs-server.service: Control process exited, code=exited status=1
Oct 22 23:34:19 localhost systemd[1]: nfs-server.service: Failed with result 'exit-code'.
Oct 22 23:34:19 localhost systemd[1]: Stopped NFS server and services.
Oct 22 23:34:19 localhost systemd[1]: Stopping NFSv4 ID-name mapping service...
Oct 22 23:34:19 localhost systemd[1]: Stopping NFS Mount Daemon...
Oct 22 23:34:19 localhost rpc.mountd[4189]: Caught signal 15, un-registering and exiting.
Oct 22 23:34:19 localhost systemd[1]: Stopped NFSv4 ID-name mapping service.
Oct 22 23:34:19 localhost systemd[1]: Stopped NFS Mount Daemon.
修改参数,并重启nfs服务,启动成功
[root@localhost ~]# vim /etc/exports
1 /nfsfile 192.168.31.11(rw,sync,root_squash)
[root@localhost ~]# systemctl restart nfs-server.service
autofs自动挂载服务
一、安装服务
[root@localhost ~]# yum install autofs
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 1:autofs-5.0.7-116.el7_9.x86_64 已安装并且是最新版本
无须任何处理
[root@localhost ~]# yum remove autofs
已加载插件:fastestmirror, langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 autofs.x86_64.1.5.0.7-116.el7_9 将被 删除
--> 正在处理依赖关系 autofs,它被软件包 ipa-client-4.6.8-5.el7.centos.9.x86_64 需要
--> 正在检查事务
---> 软件包 ipa-client.x86_64.0.4.6.8-5.el7.centos.9 将被 删除
--> 解决依赖关系完成
依赖关系解决
===============================================================================================================
Package 架构 版本 源 大小
===============================================================================================================
正在删除:
autofs x86_64 1:5.0.7-116.el7_9 @updates 5.2 M
为依赖而移除:
ipa-client x86_64 4.6.8-5.el7.centos.9 @updates 255 k
事务概要
===============================================================================================================
移除 1 软件包 (+1 依赖软件包)
安装大小:5.4 M
是否继续?[y/N]:y
。。。。。。。。。。。。。。
二、主配置文件
主配置文件/etc/auto.master,参数编写格式“挂载目录 子配置文件”如:/misc /etc/auto.misc ;其中挂载目录是设备挂载位置的上一级目录,如我们把192.168.30.10:/nfsfile 挂载到本机的/misc/nfsfile目录下,那么挂载目录就写为/misc。
备注:配置好主配置文件后重启autofs服务,系统会自动创建填写的挂载目录,此处为/misc
[root@localhost nfsfile]# vim /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
三、子配置文件
子配置文件可以自定义也可以使用主配置文件中的自带参数,格式“挂载目录 挂载文件类型及权限 :设备名称”;
参数 | 作用 |
---|---|
挂载目录 | 指要将共享文件挂载到本机的哪个目录下 |
挂载文件类型及权限 | 即文件的类型如iso,权限就是读写等权限 |
设备名称 | 要挂载过来的文件或目录,可以是本地的文件,也可以是NFS服务器共享过来的资源 |
这里我们使用著配置文件自带的自动挂载文件/etc/auto.misc
[root@localhost nfsfile]# vim /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
nfsfile 192.168.31.10:/nfsfile
# 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
四、自动挂载的时间等信息配置
在 /etc/autofs.conf 文件中可查看autofs的相关配置参数
默认五分钟内不对nfs自动挂载的目录进行操作,系统将自动卸载autofs挂载过来的目录
[root@localhost misc]# 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 = 300
五、查看是否挂载成功
发现在进入/misc/nfsfile目录前无法看到挂载信息,进入到该目录下之后就可以查看到挂载信息了
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 4.1G 13G 24% /
devtmpfs 894M 0 894M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 11M 900M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 182M 0 182M 0% /run/user/0
tmpfs 182M 24K 182M 1% /run/user/1000
/dev/sr0 4.3G 4.3G 0 100% /run/media/linux/CentOS 7 x86_64
[root@localhost ~]# cd /misc/
[root@localhost misc]# ll
总用量 0
[root@localhost misc]# cd nfsfile
[root@localhost nfsfile]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 4.1G 13G 24% /
devtmpfs 894M 0 894M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 11M 900M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 182M 0 182M 0% /run/user/0
tmpfs 182M 24K 182M 1% /run/user/1000
/dev/sr0 4.3G 4.3G 0 100% /run/media/linux/CentOS 7 x86_64
192.168.31.10:/nfsfile 17G 3.9G 14G 23% /misc/nfsfile
标签:NFS,--,dev,nfs,Autofs,Linux,挂载,root,localhost 来源: https://blog.csdn.net/fengxiwutong369/article/details/120911183