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
相关实践学习
CentOS 8迁移Anolis OS 8
Anolis OS 8在做出差异性开发同时,在生态上和依赖管理上保持跟CentOS 8.x兼容,本文为您介绍如何通过AOMS迁移工具实现CentOS 8.x到Anolis OS 8的迁移。
相关文章
|
2天前
|
开发框架 Java 关系型数据库
在Linux系统中安装JDK、Tomcat、MySQL以及部署J2EE后端接口
校验时,浏览器输入:http://[your_server_IP]:8080/myapp。如果你看到你的应用的欢迎页面,恭喜你,一切都已就绪。
80 17
|
1月前
|
Linux 定位技术
Linux系统中的cd命令:目录切换技巧
踏过千山,越过万水,人生就是一场不断前行的旅程,总充满了未知与挑战。然而,“cd”命令如同你的旅伴,会带你穿梭在如棋盘一般的文件系统中,探索每一处未知。希望你能从“cd”命令中找到乐趣,像是掌控了一种络新妙的魔法,去向未知进发,开始你的探索之旅。
125 24
|
1月前
|
Linux Shell
Linux系统下快速批量创建和删除文件的方法
总的来说,使用shell脚本来批量处理文件是一种非常强大的工具,只要你愿意花时间学习和实践,你会发现它能大大提高你的工作效率。
87 19
|
1月前
|
Ubuntu Linux 编译器
在Ubuntu Linux系统下如何搭建并安装EDK2
以上就是在Ubuntu Linux系统下搭建并安装EDK2的过程。这个过程可能会有些复杂,但只要按照步骤一步步来,应该不会有太大问题。如果在过程中遇到任何问题,都可以在网上找到相应的解决方案。希望这个指南能对你有所帮助!
93 17
|
开发工具 数据安全/隐私保护 Linux
|
开发工具 数据安全/隐私保护 Linux
Linux_RHEL7_LDAP、Autofs服务
目录 目录 前言 LDAP 加入LDAP用户认证服务器 文件自动挂载服务autofs 前言 LDAP服务器,用作于网络用户的集中管理。在企业中员工的个人帐号一般采用集中管理的方式,在不同的系统平台上也有不同的解决方案。
1300 0
|
8天前
|
缓存 Linux 数据安全/隐私保护
Linux环境下如何通过手动调用drop_caches命令释放内存
总的来说,记录住“drop_caches” 命令并理解其含义,可以让你在日常使用Linux的过程中更加娴熟和自如。
59 23
|
7天前
|
消息中间件 NoSQL Linux
Redis的基本介绍和安装方式(包括Linux和Windows版本),以及常用命令的演示
Redis(Remote Dictionary Server)是一个高性能的开源键值存储数据库。它支持字符串、列表、散列、集合等多种数据类型,具有持久化、发布/订阅等高级功能。由于其出色的性能和广泛的使用场景,Redis在应用程序中常作为高速缓存、消息队列等用途。
94 16
|
2月前
|
Linux
linux命令详细说明以及案例
本文介绍了常用的 Linux 命令及其详细说明和示例,包括:`ls`(列出目录内容)、`cd`(更改目录)、`rm` 和 `mv`(删除与移动文件)、`grep`(搜索文本)、`cat`(显示文件内容)以及 `chmod`(更改文件权限)。每个命令均配有功能描述、选项说明及实际案例,帮助用户更好地掌握 Linux 命令的使用方法。
177 56
|
1月前
|
安全 Shell Linux
Linux系统之su命令的基本使用
Linux系统之su命令的基本使用
109 3
Linux系统之su命令的基本使用