飞道的博客

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

416人阅读  评论(0)

一、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



转载:https://blog.csdn.net/jks212454/article/details/128880594
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场