linux 搭建nfs

零 修订记录

序号修订内容修订时间
1新增20220225

一 摘要

本文主要介绍在linux 系统上部署共享存储,本次使用nfs 实现。

二 环境信息

2.1 操作系统

[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[root@localhost ~]#

2.2 nfs 版本

三 实施方案

3.1 部署视图

IP功能安装服务备注
10.3.147.32nfs-server
10.3.147.33nfs-client

3.2 部署方案

可以用rpm 安装,也可以用docker 安装。

3.2.1 rpm 方式部署

3.2.1.1 关闭selinux
3.2.1.2 关闭防火墙(或者放行相关端口)所有机器执行
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@localhost ~]#

3.2.1.3 安装nfs及rpc服务 (所有机器执行)

检查是否已安装nfs,若已经安装就不需要安装了。

[root@localhost ~]# rpm -qa nfs-utils rpcbind
[root@localhost ~]#

安装nfs

[root@localhost ~]# yum install nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors

验证

[root@localhost ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-47.el7.x86_64
nfs-utils-1.3.0-0.61.el7.x86_64
[root@localhost ~]#

3.2.1.4 启动服务并配置开机启动(所有机器执行)

配置开机启动

[root@localhost etc]# systemctl enable nfs.service

[root@localhost etc]# systemctl enable rpcbind.service

检查

[root@localhost etc]# systemctl list-unit-files|grep enabled|grep nfs
nfs-server.service                            enabled
nfs.service                                   enabled
nfs-client.target                             enabled
[root@localhost etc]#

[root@localhost etc]# systemctl list-unit-files|grep enabled|grep rpcbind
rpcbind.service                               enabled
rpcbind.socket                                enabled
[root@localhost etc]#

启动

[root@localhost etc]# systemctl restart rpcbind
[root@localhost etc]# systemctl status rpcbind
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2022-02-25 11:35:32 CST; 7s ago
  Process: 13881 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 13882 (rpcbind)
   CGroup: /system.slice/rpcbind.service
           └─13882 /sbin/rpcbind -w

225 11:35:32 localhost.localdomain systemd[1]: Starting RPC bind service...
225 11:35:32 localhost.localdomain systemd[1]: Started RPC bind service.
[root@localhost etc]# systemctl restart nfs
[root@localhost etc]# systemctl status nfs
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
   Active: active (exited) since 五 2022-02-25 11:35:50 CST; 5s ago
  Process: 13953 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl restart gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 13935 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 13933 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 13935 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

225 11:35:50 localhost.localdomain systemd[1]: Starting NFS server and services...
225 11:35:50 localhost.localdomain systemd[1]: Started NFS server and services.
[root@localhost etc]#

3.2.1.5 nfs 服务端配置

此处仅配置服务端 10.3.147.32,此处共享目录是/iflytek/test,为了安全起见 将该目录权限控制为nfsnobody,该用户nfs 安装时会默认建立。

3.2.1.5.1 修改共享目录权限(服务端执行)
[root@localhost test]# chown -R nfsnobody:nfsnobody /iflytek/test
3.2.1.5.2 修改/etc/exports

修改任何配置文件 先备份

[root@localhost test]# cp /etc/exports /etc/exports.bak.orig
[root@localhost test]# cp /etc/exports{,.bak`date +"%Y%m%d%H%m%S"`}
[root@localhost test]# ll /etc/ex*
-rw-r--r--. 1 root root 0 67 2013 /etc/exports
-rw-r--r--  1 root root 0 225 14:23 /etc/exports.bak20220225140253
-rw-r--r--  1 root root 0 225 14:22 /etc/exports.bak.orig

/etc/exports.d:
总用量 0
[root@localhost test]#

[root@localhost test]# echo "/iflytek/test/ 10.3.147.0/24(rw,sync,root_squash)">>/etc/exports

参数说明:

rw                # 客户端对共享的目录可读写
ro                # 客户端对共享的目录只读不可写
sync              # 同步模式,也就是把内存的数据实时写入硬盘,但这样会降低磁盘效率
async             # 非同步模式,也就是每隔一段时间才会把内存的数据写入硬盘,能保证磁盘效率,但当异常宕机/断电时,会丢失内存里的数据
no_root_squash    # 客户端挂载NFS共享目录后,客户端上的root用户不受这些挂载选项的限制,权限很大
root_squash       # 跟no_root_squash相反,客户端上的root用户受到这些挂载选项的限制,被当成普通用户
all_squash        # 客户端上的所有用户在使用NFS共享目录时都被限定为一个普通用户
anonuid           # 上面的几个squash用于把客户端的用户限定为普通用户,而anouid用于限定这个普通用户的uid,这个uid与服务端的/etc/passwd文件相对应,如:anouid=1000
                  # 比如我客户端用xiaoming这个用户去创建文件,那么服务端同步这个文件的时候,文件的属主会变成服务端的uid(1000)所对应的用户
anongid           # 同上,用于限定这个普通用户的gid

加载配置文件

[root@localhost test]# exportfs -rv
exporting 10.3.147.0/24:/iflytek/test
[root@localhost test]#

exportfs 命令参数说明
-a :全部挂载或者卸载;
-r :重新挂载;
-u :卸载某一个目录;
-v :显示共享的目录;

3.2.1.6 nfs 客户端配置

现在客户机建立挂载目录,然后把服务端目录挂上去。

3.2.1.6.1 建立挂载目录
[root@localhost ~]# mkdir -p /iflytek/test
[root@localhost ~]# cd /iflytek/test/
[root@localhost test]# pwd
/iflytek/test
[root@localhost test]#

3.2.1.6.2 挂载
[root@localhost test]# mount -t nfs -o noexec,nosuid,nodev,rw,rsize=65536,wsize=65536 10.3.147.32:/iflytek/test /iflytek/test

参数说明:

wsize和rsize写和读缓存

async     数据不同步写到磁盘,提高性参,但降低数据安全,不推荐使用
noatime和nodiratime  这两个选是说在读写磁盘时,不更新文件和目录的时间戳,而更新文件时间戳对于工作数据必要性不大,增加了磁盘IO的次数,拖慢系统性参,
defaults   这个缺省值包括rw,suid,dev,exec,auto,nouser,and async  cat /etc/fstab的结果默认大部人都是缺省值

noauto    不会自动挂载文件系统

noexec    不允许安装的直接执行任何二进制文件

ro         挂载一个只读文件系统

rw        挂载一个可写的文件系统

sync       把数据同步写入硬盘

nosuid     不允许设置用户标识或设置组标识符位

nodev      不解释字符或文件块特殊设备

 intr       表示可以中断

3.2.2 验证

在客户端用root 用户建文件,会显示为nfsnobody,服务器端root 用户建文件则不会。

[root@localhost test]# ls -al
总用量 16
drwxr-xr-x 2 nfsnobody nfsnobody 81 225 14:42 .
drwxr-xr-x 3 root      root      18 225 14:36 ..
-rw-r--r-- 1 root      root      13 225 14:41 1111from32.txt
-rw-r--r-- 1 nfsnobody nfsnobody  5 225 14:39 222.txt
-rw-r--r-- 1 nfsnobody nfsnobody 14 225 14:42 3333from33.txt
-rw-r--r-- 1 nfsnobody nfsnobody  7 225 10:55 test.txt
[root@localhost test]#

3.2.3 客户端取消挂载

 umount /iflytek/test/

有时需要加强版

[root@localhost /]# fuser -m -v -i -k /iflytek/test/
-bash: fuser: 未找到命令
[root@localhost /]# yum install psmisc

[root@localhost /]# fuser -m -v -i -k /iflytek/test/
                     用户     进程号 权限   命令
/iflytek/test:       root     kernel mount /iflytek/test
[root@localhost /]# df -h
文件系统                   容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root     76G  1.2G   74G    2% /
devtmpfs                   1.9G     0  1.9G    0% /dev
tmpfs                      1.9G     0  1.9G    0% /dev/shm
tmpfs                      1.9G  8.6M  1.9G    1% /run
tmpfs                      1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                 1014M  145M  870M   15% /boot
tmpfs                      379M     0  379M    0% /run/user/0
10.3.147.32:/iflytek/test  100G   33M  100G    1% /iflytek/test
[root@localhost /]# umount /iflytek/test/
[root@localhost /]#

3.2.4 客户端开机自动挂载 (暂未验证成功)

客户端开机自动挂载方案很多,比如修改/etc/fstab 或mount 命令写到local 里,或者用autofs 服务

此处使用autofs方案

3.2.4.1 安装autofs 并配置为开机启动
[root@localhost test]#  yum install   autofs
[root@localhost test]# systemctl  enable  autofs

[root@localhost test]# systemctl  restart  autofs
[root@localhost test]# systemctl  status  autofs
● autofs.service - Automounts filesystems on demand
   Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2022-02-25 14:50:24 CST; 5s ago
 Main PID: 7563 (automount)
   CGroup: /system.slice/autofs.service
           └─7563 /usr/sbin/automount --foreground --dont-check-daemon
3.2.4.2 配置autofs

备份原配置文件

[root@localhost etc]# cp /etc/auto.master{,.bak`date +"%Y%m%d%H%m%S"`}

配置文件最后一行添加该行

/iflytek /etc/auto.iflytek

说明 /iflytek 是挂载的上级目录
具体挂载目录及配置看/etc/auto.iflytek
这个文件我们新建

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NFS(Network File System)是一种基于网络的文件系统协议,它可以让不同的计算机在网络上共享文件和目录。在Linux系统中,我们可以使用NFS协议来实现文件共享。 以下是在Linux系统中搭建NFS的步骤: 1. 安装NFS软件包 在Linux系统中,我们需要安装NFS软件包来支持NFS服务。不同的Linux发行版安装方式略有不同,以Ubuntu为例,可以使用以下命令进行安装: sudo apt-get update sudo apt-get install nfs-kernel-server 2. 配置NFS服务 配置NFS服务需要编辑/etc/exports文件。在该文件中,我们可以指定要共享的目录以及允许哪些客户端访问该目录。例如,如果我们想共享目录“/home/share”并允许192.168.1.100客户端访问该目录,可以将以下内容添加到/etc/exports文件中: /home/share 192.168.1.100(rw,sync,no_subtree_check) 其中,/home/share是要共享的目录,192.168.1.100是允许访问该目录的客户端IP地址,rw表示允许读写访问,sync表示同步写入数据,no_subtree_check表示不检查子目录。 3. 重启NFS服务 在修改了/etc/exports文件后,我们需要重启NFS服务才能使配置生效。可以使用以下命令重启NFS服务: sudo systemctl restart nfs-kernel-server 4. 在客户端上挂载共享目录 在客户端上可以使用mount命令挂载NFS共享目录。例如,在192.168.1.100客户端上,可以使用以下命令挂载/home/share目录: sudo mount 192.168.1.200:/home/share /mnt 其中,192.168.1.200是NFS服务器的IP地址,/home/share是要挂载的共享目录,/mnt是客户端本地的挂载点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值