如何使用NFS配置linux文件共享

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用NFS配置linux文件共享相关的知识,希望对你有一定的参考价值。

参考技术A 方法/步骤
首先确认自己的服务器上面是否有portmap和nfs包,一般情况下都会有.
查询命令:
rpm
-qa
|
grep
portmap
rpm
-qa
|
grep
nfs
如果有就接着向下走
启动portmap和nfs,但是portmap的先于nfs启动,因为portmap为nfs动态分配端口
查看是有启动正常用如下命令
prcinfo
-p
如果看到portmap和nfs表示启动正常
设置你要共享的文件和目录
编辑vi
/etc/exports
格式如下:
共享目录
允许访问的主机ip(权限)
输出共享目录和文件
1.要不重启nfs服务
2.使用exportfs
-rv命令
接着到另一台服务器上面去挂载nfs服务器输出的文件和目录
1.新建你要挂载的目录
2.查看nfs服务器的输出目录
showmount
-e
nfs的ip
挂载目录或文件
格式如下:
mount
nfs服务器地址:/usr/local/web
/mnt/websites
查看挂载结果
df
-h
测试共享时候成功,在web1上面下面的挂载目录下面新建一个文件然后随便写点东西看web2上面时候同步
卸载目录的命令:
umount
/mnt/websites

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


以上是关于如何使用NFS配置linux文件共享的主要内容,如果未能解决你的问题,请参考以下文章

Linux 服务器之间如何进行文件目录共享

配置NFS共享, Linux与Linux的共享

如何加固linux NFS 服务安全的方法

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

Linux网络——NFS共享服务原理与配置

Vagrant:如何禁用 Windows 主机的 NFS 同步文件夹?