Linux配置NFS及开机自动挂载

  • 准备步骤
  • 添加yum
  • 安装NFS服务
  • 配置本地yum源
  • **`以上步骤10.4同步操作`**
  • 10.4搭建NFS
  • 配置服务
  • 10.3搭建NFS
  • 配置服务
  • 10.3配置开机自动挂载
  • 10.4配置开机自动挂载


NFS(network file system)网络文件系统
文件共享:将一台服务器中的文件共享到其他服务器

准备步骤

VMware2台虚拟机、操作系统镜像文件
示例操作系统:Red Hat Enterprise Linux 8.2 (Ootpa)
虚拟机:10.10.10.3、10.10.10.4
规划共享目录10.3:/opt/data3、10.4:/opt/data4

创建文件夹,两台都执行

mkdir /opt/data3;mkdir /opt/data4;

添加yum

添加操作系统镜像文件,重启虚拟机,两台都执行

centos nfs 开机自动挂载 linux nfs挂载 开机加载_共享目录


centos nfs 开机自动挂载 linux nfs挂载 开机加载_centos nfs 开机自动挂载_02

安装NFS服务

mkdir /opt/rh8.2

sr0为操作系统镜像文件,挂载至/opt/rh8.2

centos nfs 开机自动挂载 linux nfs挂载 开机加载_linux_03


可看到AppStream、BaseOS文件夹

centos nfs 开机自动挂载 linux nfs挂载 开机加载_linux_04

配置本地yum源

centos nfs 开机自动挂载 linux nfs挂载 开机加载_centos nfs 开机自动挂载_05

vi /etc/yum.repos.d/redhat.repo

添加以下内容

[redhat-local]
name=AppStream
baseurl=file:///opt/rh8.2/AppStream
#注意路径,按照实际填写
gpgcheck=0
enabled=1
[BaseOS]
name=BaseOS
baseurl=file:///opt/rh8.2/BaseOS
#注意路径,按照实际填写
gpgcheck=0
enabled=1

清除 YUM 缓存

yum clean all

centos nfs 开机自动挂载 linux nfs挂载 开机加载_centos nfs 开机自动挂载_06

建立元数据缓存

yum makecache

centos nfs 开机自动挂载 linux nfs挂载 开机加载_共享目录_07


安装NFS相关软件

yum install -y *nfs*

centos nfs 开机自动挂载 linux nfs挂载 开机加载_服务器_08


查看安装后的NFS软件

rpm -ql nfs-utils

centos nfs 开机自动挂载 linux nfs挂载 开机加载_服务器_09

以上步骤10.4同步操作

10.4搭建NFS

配置10.4共享目录

vi /etc/exports

添加以下内容

/opt/data4 *(rw,async,no_root_squash,no_subtree_check)

可按照需求自定义权限
编写规则:每行定义一个共享目录
每行至少包含两段内容:第一段是共享的目录路径,
第二段客户端(文件系统共享属性)
例:

/opt/data4 10.10.10.3(rw,no_root_squash)

表示在这个网段的客户端读和些这个/opt/data4目录,
1) R读,w写,x执行,ro(只读)
2)默认属性root_squash—对客户端root权限进行压缩,设置后客户端的root用户只能下载,不能上传,
默认属性no_root_squash----对客户端root用户不进行权限压缩,设置后客户端的root用户可以对共享目录进行上传下载文件(客户端root权限同服务器端root权限)

/opt/data4 10.10.10.3(rw,no_root_squash) 10.10.10.0/24(rw)

3)只给10.10.10.3这个IP地址不压缩客户端root权限,对共享目录有上传和下载的功能,而其他地址普通用户只有下载的功能

重新导出所有定义的共享目录文件

exportfs -a

修改配置文件

vi /etc/nfs.conf

添加以下内容

LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769
MOUNTD_PORT=892
RQUOTAD_PORT=875
STATD_PORT=662
STATD_OUTGOING_PORT=2020

centos nfs 开机自动挂载 linux nfs挂载 开机加载_centos nfs 开机自动挂载_10

配置服务

关闭防火墙

centos nfs 开机自动挂载 linux nfs挂载 开机加载_服务器_11

systemctl stop firewalld.service
systemctl disable firewalld.service

启用NFS服务并设置开机自启动

systemctl start rpcbind.service
systemctl start nfs-server.service
systemctl enable rpcbind.service
systemctl enable nfs-server.service

centos nfs 开机自动挂载 linux nfs挂载 开机加载_服务器_12

10.3上创建映射

mount 10.10.10.4:/opt/data4 /opt/data4

centos nfs 开机自动挂载 linux nfs挂载 开机加载_运维_13

10.3搭建NFS

配置10.3共享目录

vi /etc/exports

添加以下内容

/opt/data3 *(rw,async,no_root_squash,no_subtree_check)

重新导出所有定义的共享目录文件

exportfs -a

修改配置文件

vi /etc/nfs.conf

添加以下内容

LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769
MOUNTD_PORT=892
RQUOTAD_PORT=875
STATD_PORT=662
STATD_OUTGOING_PORT=2020

centos nfs 开机自动挂载 linux nfs挂载 开机加载_linux_14

配置服务

关闭防火墙

centos nfs 开机自动挂载 linux nfs挂载 开机加载_centos nfs 开机自动挂载_15

systemctl stop firewalld.service
systemctl disable firewalld.service

启用NFS服务并设置开机自启动

systemctl start rpcbind.service
systemctl start nfs-server.service
systemctl enable rpcbind.service
systemctl enable nfs-server.service

centos nfs 开机自动挂载 linux nfs挂载 开机加载_服务器_16

10.4上创建映射

mount 10.10.10.3:/opt/data3 /opt/data3

centos nfs 开机自动挂载 linux nfs挂载 开机加载_centos nfs 开机自动挂载_17

NFS搭建完成

10.3配置开机自动挂载

编辑挂载脚本

vi /opt/nfsmount.sh

添加以下内容

#!/bin/bash
## This is NFS disk automount shell script
echo "NFS启动时间点:$(date +"%F %T")" >>/opt/nfs.log;
val=`df -h|grep website | wc -l`
if [ $val -eq 1 ]
then
          echo  "NFS目录/opt/data4已经挂载,无需再挂" >> /opt/nfs.log;
else 
mount  -o vers=3   10.10.10.4:/opt/data4 /opt/data4
echo  "NFS目录/opt/data4挂载成功" >> /opt/nfs.log;
exit
fi
echo "执行完毕" >> /opt/nfs.log

centos nfs 开机自动挂载 linux nfs挂载 开机加载_共享目录_18


添加至开机自启

echo "/opt/nfsmount.sh"   >>  /etc/rc.d/rc.local;
cat  /etc/rc.d/rc.local;

centos nfs 开机自动挂载 linux nfs挂载 开机加载_linux_19


赋予执行权限

chmod +x /opt/nfsmount.sh;
chmod +x /etc/rc.d/rc.local;

重启验证

centos nfs 开机自动挂载 linux nfs挂载 开机加载_共享目录_20

10.4配置开机自动挂载

编辑挂载脚本

vi /opt/nfsmount.sh

添加以下内容

#!/bin/bash
## This is NFS disk automount shell script
echo "NFS启动时间点:$(date +"%F %T")" >>/opt/nfs.log;
val=`df -h|grep website | wc -l`
if [ $val -eq 1 ]
then
          echo  "NFS目录/opt/data3已经挂载,无需再挂" >> /opt/nfs.log;
else 
mount  -o vers=3   10.10.10.3:/opt/data3 /opt/data3
echo  "NFS目录/opt/data3挂载成功" >> /opt/nfs.log;
exit
fi
echo "执行完毕" >> /opt/nfs.log

centos nfs 开机自动挂载 linux nfs挂载 开机加载_服务器_21

添加至开机自启

echo "/opt/nfsmount.sh"   >>  /etc/rc.d/rc.local;
cat  /etc/rc.d/rc.local;

centos nfs 开机自动挂载 linux nfs挂载 开机加载_linux_22


赋予执行权限

chmod +x /opt/nfsmount.sh;
chmod +x /etc/rc.d/rc.local;

重启验证

centos nfs 开机自动挂载 linux nfs挂载 开机加载_centos nfs 开机自动挂载_23