挂载硬盘

  1. # 查看硬盘
  2. fdisk -l
  3. fdisk /dev/sdb
  4. # 输入n,然后一路默认最后输入w,完成分区
  5. # 然后格式化
  6. mkfs.ext4 /dev/sdb1
  7. # 然后挂载到目录
  8. mkdir -p /nfs-data;
  9. mount /dev/sdb1 /nfs-data
  10. 将下面信息写入/etc/fstab文件,让系统开启自动挂载
  11. /dev/sdb1 /nfs-data ext4 defaults 0 0
  12. # 查看是否挂载成功
  13. df -h

安装NFS组件

当做NFS服务端和客户端的主机上都需要安装

  1. yum -y install nfs-utils
  2. # 其实还需要rpcbind组件的,不过安装nfs-utols的时候回自动连带安装了
  3. # 设置开机启动,关闭防火墙
  4. systemctl enable rpcbind.service;
  5. systemctl enable nfs-server.service
  6. systemctl stop firewalld

配置NFS共享目录

  1. # 编辑/etc/exports文件,配置哪些主机可以使用
  2. /nfs-data 10.110.30.0/24(rw,sync,no_root_squash,no_wdelay)

第一部分/nfs-data是要共享的目录 第二部分10.110.30.0/24是有权限使用共享目录的地址段,也可以直接写一些IP地址,例如10.110.30.2(rw,sync,no_root_squash,no_wdelay) 10.110.30.3(rw,sync,no_root_squash,no_wdelay) 第三部分是具体权限: rw表示可读写,ro只读

sync :同步模式,内存中数据实时写入磁盘;

async :不同步,数据在内存中,定期写入磁盘

no_root_squash :加上这个选项后,root用户就会对共享的目录像是对本机的目录一样拥有最高权限。

root_squash:和上面的选项对应,root用户对共享目录的权限不高,只有普通用户的权限

all_squash:不管使用NFS的用户是谁,他的身份都会被限定成为一个指定的普通用户身份

anonuid=xxx/anongid=xxx :要和root_squash 以及all_squash一同使用,用于指定使用NFS的用户限定后的uid和gid,前提是本机的/etc/passwd中存在这个uid和gid

启动服务

  1. # 服务端两个都需要启动,客户端只需要启动rpcbind即可
  2. systemctl start rpcbind;
  3. systemctl start nfs;
  4. # 确认NFS服务器启动成功(能看到有名叫nfs的服务):
  5. rpcinfo -p

客户端挂载NFS共享文件夹

  1. # 创建共享目录
  2. mkdir /nfs-data;
  3. # 挂在文件夹
  4. mount -t nfs 10.110.30.216:/nfs-data /nfs-data