Linux系统之使用autofs自动挂载nfs共享

简介: Linux系统之使用autofs自动挂载nfs共享

一、autofs介绍

Autofs 是一项可自动挂载相应文件系统的客户端服务,可用于nfs共享的自动挂载,无需手动mount挂载,方便日常日常使用。

二、检查本地系统环境

1.检查系统版本

[root@jeven ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

2.检查系统内核版本

[root@jeven ~]# uname -r 
6.1.8-1.el7.elrepo.x86_64

三、部署nfs服务

1.检查系统nfs软件包

检查系统nfs软件包是否安装


[root@jeven ~]#  yum list installed |grep nfs
libnfsidmap.x86_64                      0.25-19.el7                    @anaconda
nfs-utils.x86_64                        1:1.3.0-0.61.el7               @anaconda
nfs4-acl-tools.x86_64                   0.3.3-19.el7                   @anaconda

2.安装nfs

[root@jeven ~]# yum install nfs-utils rpcbind -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * elrepo: mirrors.tuna.tsinghua.edu.cn
base                                                                                                                                         | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                             | 3.5 kB  00:00:00     
elrepo                                                                                                                                       | 3.0 kB  00:00:00     
epel                                                                                                                                         | 4.7 kB  00:00:00     
extras                                                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                                                      | 2.9 kB  00:00:00     
(1/3): docker-ce-stable/7/x86_64/primary_db                                                                                                  |  95 kB  00:00:00     
(2/3): epel/x86_64/updateinfo                                                                                                                | 1.0 MB  00:00:01     
(3/3): epel/x86_64/primary_db                                                                                                                | 7.0 MB  00:00:10     
Resolving Dependencies
--> Running transaction check
---> Package nfs-utils.x86_64 1:1.3.0-0.61.el7 will be updated
---> Package nfs-utils.x86_64 1:1.3.0-0.68.el7.2 will be an update
---> Package rpcbind.x86_64 0:0.2.0-47.el7 will be updated
---> Package rpcbind.x86_64 0:0.2.0-49.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================
 Package                               Arch                               Version                                         Repository                           Size
====================================================================================================================================================================
Updating:
 nfs-utils                             x86_64                             1:1.3.0-0.68.el7.2                              updates                             413 k
 rpcbind                               x86_64                             0.2.0-49.el7                                    base                                 60 k

Transaction Summary
====================================================================================================================================================================
Upgrade  2 Packages

Total download size: 473 k
Downloading packages:
No Presto metadata available for base
No Presto metadata available for updates
(1/2): rpcbind-0.2.0-49.el7.x86_64.rpm                                                                                                       |  60 kB  00:00:00     
(2/2): nfs-utils-1.3.0-0.68.el7.2.x86_64.rpm                                                                                                 | 413 kB  00:00:00     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                               607 kB/s | 473 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : rpcbind-0.2.0-49.el7.x86_64                                                                                                                      1/4 
  Updating   : 1:nfs-utils-1.3.0-0.68.el7.2.x86_64                                                                                                              2/4 
  Cleanup    : 1:nfs-utils-1.3.0-0.61.el7.x86_64                                                                                                                3/4 
  Cleanup    : rpcbind-0.2.0-47.el7.x86_64                                                                                                                      4/4 
  Verifying  : 1:nfs-utils-1.3.0-0.68.el7.2.x86_64                                                                                                              1/4 
  Verifying  : rpcbind-0.2.0-49.el7.x86_64                                                                                                                      2/4 
  Verifying  : rpcbind-0.2.0-47.el7.x86_64                                                                                                                      3/4 
  Verifying  : 1:nfs-utils-1.3.0-0.61.el7.x86_64                                                                                                                4/4 

Updated:
  nfs-utils.x86_64 1:1.3.0-0.68.el7.2                                                 rpcbind.x86_64 0:0.2.0-49.el7                                                

Complete!

3.创建共享目录

mkdir -p /nfs   #创建共享目录

4.编辑配置文件


echo "/nfs *(insecure,rw,sync,no_root_squash)" > /etc/exports

5.重启nfs相关服务

 systemctl enable --now rpcbind
 systemctl enable --now  nfs-server

6.查看nfs服务状态


[root@jeven ~]# systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
  Drop-In: /run/systemd/generator/nfs-server.service.d
           └─order-with-mounts.conf
   Active: active (exited) since Sat 2023-02-04 11:00:17 CST; 1min 1s ago
  Process: 63991 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 63969 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 63967 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 63969 (code=exited, status=0/SUCCESS)
    Tasks: 0
   Memory: 0B
   CGroup: /system.slice/nfs-server.service

Feb 04 11:00:15 jeven systemd[1]: Starting NFS server and services...
Feb 04 11:00:17 jeven systemd[1]: Started NFS server and services.

四、远程客户端测试nfs共享

[root@server001 ~]# showmount -e 192.168.3.166
Export list for 192.168.3.166:
/nfs *

五、配置autofs自动挂载

1.安装autofs软件包

[root@server001 ~]# yum -y install autofs
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Determining fastest mirrors
base                                                                                                                                             | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                                 | 3.5 kB  00:00:00     
extras                                                                                                                                           | 2.9 kB  00:00:00     
updates                                                                                                                                          | 2.9 kB  00:00:00     
(1/2): docker-ce-stable/7/x86_64/primary_db                                                                                                      |  95 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                                                                                               |  19 MB  00:00:22     
Resolving Dependencies
--> Running transaction check
---> Package autofs.x86_64 1:5.0.7-116.el7_9 will be installed
--> Processing Dependency: libhesiod.so.0()(64bit) for package: 1:autofs-5.0.7-116.el7_9.x86_64
--> Running transaction check
---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
 Package                              Arch                                 Version                                          Repository                             Size
========================================================================================================================================================================
Installing:
 autofs                               x86_64                               1:5.0.7-116.el7_9                                updates                               834 k
Installing for dependencies:
 hesiod                               x86_64                               3.2.1-3.el7                                      base                                   30 k

Transaction Summary
========================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 864 k
Installed size: 5.2 M
Downloading packages:
(1/2): hesiod-3.2.1-3.el7.x86_64.rpm                                                                                                             |  30 kB  00:00:00     
(2/2): autofs-5.0.7-116.el7_9.x86_64.rpm                                                                                                         | 834 kB  00:00:01     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                   718 kB/s | 864 kB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : hesiod-3.2.1-3.el7.x86_64                                                                                                                            1/2 
  Installing : 1:autofs-5.0.7-116.el7_9.x86_64                                                                                                                      2/2 
  Verifying  : hesiod-3.2.1-3.el7.x86_64                                                                                                                            1/2 
  Verifying  : 1:autofs-5.0.7-116.el7_9.x86_64                                                                                                                      2/2 

Installed:
  autofs.x86_64 1:5.0.7-116.el7_9                                                                                                                                       

Dependency Installed:
  hesiod.x86_64 0:3.2.1-3.el7                                                                                                                                           

Complete!

2.设置服务开机自启


[root@server001 ~]# systemctl enable --now autofs
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.

3.配置autofs自动挂载

指定 zhangsan用户挂载的本地目录为/home/zhangsan,挂载
用户家目录配置文件为/etc/auto.nfs

[root@server001 ~]# grep home /etc/auto.master
/home /etc/auto.nfs
[root@server001 ~]# cat  /etc/auto.nfs
zhangsan -fstype=nfs,rw 192.168.3.166:/nfs

4.重启autofs服务


[root@server001 ~]# systemctl restart autofs
[root@server001 ~]#

六、测试客户端autofs自动挂载

1.进入/home/zhangsan目录

进入/home/zhangsan目录,autofs自动将/nfs共享目录挂载到本地/home/zhangsan目录。


[root@server001 ~]# cd /home/zhangsan
[root@server001 zhangsan]# df -h |grep zhangsan
192.168.3.166:/nfs       106G   49G   58G  47% /home/zhangsan

2.测试共享目录创建文件

  • 客户端写入文件
    [root@server001 zhangsan]# touch aa.txt
    [root@server001 zhangsan]# ls
    aa.txt
    
  • nfs服务端查看文件
[root@jeven ~]# cd /nfs/
[root@jeven nfs]# ls
aa.txt
相关文章
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
1301 3
Linux系统禁用swap
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
1892 3
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
1370 3
Linux系统初始化脚本
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
1456 1
|
开发工具 数据安全/隐私保护 Linux
|
开发工具 数据安全/隐私保护 Linux
Linux_RHEL7_LDAP、Autofs服务
目录 目录 前言 LDAP 加入LDAP用户认证服务器 文件自动挂载服务autofs 前言 LDAP服务器,用作于网络用户的集中管理。在企业中员工的个人帐号一般采用集中管理的方式,在不同的系统平台上也有不同的解决方案。
1469 0
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
1237 1
二、Linux文本处理与文件操作核心命令
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
976 137
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
1958 58
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
1895 2