centos中安装nfs实现服务器文件共享

如果一个服务使用集群方式部署,在文件读写时就需要考虑多个服务之间文件共享的问题,在共享文件时可以考虑部署一个单独的文件服务器,比如hdfs、minio、fastdfs等,也可以使用简单的文件目录共享方式实现,比如nfs。下面就介绍nfs简单安装使用:

一、nfs服务端安装
  1. nfs服务端需要安装两个服务rpcbind、nfs-utils
shell 复制代码
[root@localhost ~]# yum -y install rpcbind nfs-utils
  1. 创建共享共享目录
shell 复制代码
[root@localhost ~]# mkdir -p /root/nfs-storage/
  1. 启动服务并设置开机自启动
shell 复制代码
# 启动服务
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl start nfs

# 查看服务状态
[root@localhost ~]# systemctl status nfs
● 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 Wed 2024-01-17 17:12:17 CST; 35min ago
 Main PID: 13752 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

Jan 17 17:12:17 localhost.localdomain systemd[1]: Starting NFS server and services...
Jan 17 17:12:17 localhost.localdomain systemd[1]: Started NFS server and services.

# 设置开机自启动
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl enable nfs
  1. 添加配置文件,指定共享目录和目录权限
shell 复制代码
[root@localhost ~]# vim /etc/exports

/root/nfs-storage *(rw,no_root_squash,no_all_squash,sync)

配置说明:

* 表示所有ip的客户端都可以访问,如果要指定网段或ip进行访问,替换*号就可以。

rw 表示设置目录可读写。

sync 表示数据会同步写入到内存和硬盘中,相反 rsync 表示数据会先暂存于内存中,而非直接写入到硬盘中。

no_root_squash NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。

no_all_squash 不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都不会拥有匿名用户权限。

  1. 让配置文件生效
shell 复制代码
[root@localhost ~]# exportfs -r
  1. 查看服务器目录挂载情况
shell 复制代码
[root@localhost ~]# showmount -e localhost
二、nfs客户端安装
  1. 客户端只需要安装rpcbind
shell 复制代码
[root@localhost ~]# yum -y install rpcbind
  1. 启动rpcbind并设置开机自启动
shell 复制代码
[root@localhost ~]# systemctl start rpcbind

# 设置开机自启动
[root@localhost ~]# systemctl enable rpcbind
  1. 客户端挂载服务器的共享目录
shell 复制代码
[root@localhost ~]# mount -t nfs -o nolock,nfsvers=3,vers=3 192.168.56.101:/root/nfs-storage /root/nfs-storage
  1. 查看是否挂载成功,如果有最后一行内容就表示挂载成功了
shell 复制代码
[root@localhost ~]# df -h
Filesystem                          Size  Used Avail Use% Mounted on
/dev/mapper/centos-root             184G  4.1G  180G   3% /
devtmpfs                            3.9G     0  3.9G   0% /dev
tmpfs                               3.9G     0  3.9G   0% /dev/shm
tmpfs                               3.9G  177M  3.7G   5% /run
tmpfs                               3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1                           4.0G  148M  3.9G   4% /boot
tmpfs                               783M     0  783M   0% /run/user/0
tmpfs                                60M     0   60M   0% /var/log/rtlog
192.168.56.101:/root/nfs-storage    184G  7.1G  177G   4% /root/nfs-storage
  1. 客户端配置启动时自动挂载共享目录
    (不要把挂载项写到/etc/fstab文件中,因为开机时先挂载本机磁盘再启动网络,而NFS是需要网络启动后才能挂载的,所以我们把挂载命令写入到/etc/rc.d/rc.local文件中即可)
shell 复制代码
[root@localhost ~]# vim /etc/rc.d/rc.local

mount -t nfs -o nolock,nfsvers=3,vers=3 192.168.56.101:/root/nfs-storage /root/nfs-storage
  1. 其他相关命令
shell 复制代码
# 客户端显示挂载信息
[root@localhost ~]# showmount -e 192.168.56.101

# 取消挂载
[root@localhost ~]# umount /root/nfs-storage
相关推荐
A小辣椒2 天前
TShark:Wireshark CLI 功能
linux
A小辣椒2 天前
TShark:基础知识
linux
AlfredZhao2 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao3 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334663 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪3 天前
linux 拷贝文件或目录到指定的位置
linux
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
bush44 天前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5204 天前
Linux 11 动态监控指令top
linux