NFS文件系统共享服务器实战

架设一台NFS服务器,并按照以下要求配置

准备

两台Linux虚拟机一台作为服务端server,一台作为客户端client

server IPV4:192.168.110.136/24 client IPV4:192.168.110.134/24

两台服务器都需要关闭防火墙和seLinux

服务端

root@server \~\]# systemctl stop firewalld \[root@server \~\]# setenforce 0 \[root@server \~\]# getenforce Permissive

客户端

root@client \~\]# systemctl stop firewalld \[root@client \~\]# setenforce 0 \[root@client \~\]# getenforce Permissive

服务端下载nfs-utils, rpcbind

root@server \~\]# yum install -y nfs-utils rpcbind

客户端下载nfs-utils

root@client \~\]# yum install -y nfs-utils

1、开放/nfs/shared目录,供所有用户查询资料

服务端 IPV4:192.168.110.136/24

root@server \~\]# mkdir -pv /nfs/shared #创建目录 mkdir: 已创建目录 '/nfs' mkdir: 已创建目录 '/nfs/shared' \[root@server \~\]# touch /nfs/shared/{1..5} #创建文件 \[root@server \~\]# cd /nfs/shared/ \[root@server shared\]# ll 总用量 0 -rw-r--r--. 1 root root 0 11月 8 23:32 1 -rw-r--r--. 1 root root 0 11月 8 23:32 2 -rw-r--r--. 1 root root 0 11月 8 23:32 3 -rw-r--r--. 1 root root 0 11月 8 23:32 4 -rw-r--r--. 1 root root 0 11月 8 23:32 5 \[root@server shared\]# cd \[root@server \~\]# vim /etc/exports #定义NFS共享的配置文件 /nfs/shared 192.168.110.134(ro,sync) # ro为只读,sync为数据会同步写入到硬盘中 ![](https://file.jishuzhan.net/article/1723631708256866306/c0fcb963801c1145486551db91e0b6ae.webp) \[root@server \~\]# systemctl start nfs-server #启动服务,也可以enable设置开机自启 \[root@server \~\]# exportfs -r #读取信息直接生效 \[root@server \~\]# showmount -e #查看服务器端共享的目录 Export list for server: /nfs/shared 192.168.110.134 ![](https://file.jishuzhan.net/article/1723631708256866306/b09b35776debd48f61223d710de39141.webp) 客户端 IPV4:192.168.110.134/24 \[root@client \~\]# showmount -e 192.168.110.136 #查看服务器端共享的目录 Export list for 192.168.110.136: /nfs/shared 192.168.110.134 \[root@client \~\]# mkdir -pv /nfs/test #创建挂载目录 mkdir: 已创建目录 '/nfs' mkdir: 已创建目录 '/nfs/test' \[root@client \~\]# mount -t nfs 192.168.110.136:/nfs/shared /nfs/test/ #挂载 \[root@client \~\]# cd /nfs/test/ \[root@client test\]# ll #查看 总用量 0 -rw-r--r--. 1 root root 0 11月 8 23:32 1 -rw-r--r--. 1 root root 0 11月 8 23:32 2 -rw-r--r--. 1 root root 0 11月 8 23:32 3 -rw-r--r--. 1 root root 0 11月 8 23:32 4 -rw-r--r--. 1 root root 0 11月 8 23:32 5 \[root@client test\]# touch 6 touch: 无法创建 '6': 只读文件系统 ![](https://file.jishuzhan.net/article/1723631708256866306/b053efa5ac9efbb6cab17b43bd961abb.webp)

2、开放/nfs/upload目录,为192.168.110.0/24网段主机可以上传目录,并将所有用户及所属的组映射为nfs-upload,其UID和GID均为210

root@server \~\]# mkdir /nfs/upload/ -v #创建目录 mkdir: 已创建目录 '/nfs/upload/' \[root@server \~\]# touch /nfs/upload/{1..5} \[root@server \~\]# vim /etc/exports #定义NFS共享的配置文件 /nfs/upload 192.168.110.0/24(rw,all_squash,anonuid=210,anongid=210) ![](https://file.jishuzhan.net/article/1723631708256866306/8ec0e5dd45448ac29fe6d4f2e615a86d.webp) # rw为可读写,all_squash:客户端所有用户创建文件时,客户端会将文件的用户 和组映射为匿名用户和组,anonuid=210,anongid=210将文件的用户和组映射为指定的UID和GID \[root@server \~\]# exportfs -r \[root@server \~\]# showmount -e Export list for server: /nfs/upload 192.168.110.0/24 ![](https://file.jishuzhan.net/article/1723631708256866306/1264863d583d6b73fbadf6db7df5ffd0.webp) \[root@server \~\]# useradd -r -u 210 nfs-upload \[root@server \~\]# id nfs-upload 用户id=210(nfs-upload) 组id=210(nfs-ipload) 组=210(nfs-ipload) \[root@server upload\]# chmod o+w /nfs/upload/ #赋权 客户端 \[root@client \~\]# showmount -e 192.168.110.136 Export list for 192.168.110.136: /nfs/upload 192.168.110.0/24 \[root@client \~\]# mount -t nfs 192.168.110.136:/nfs/upload /nfs/test2/ \[root@client \~\]# ll /nfs/test2/ 总用量 0 -rw-r--r--. 1 root root 0 11月 9 00:15 1 -rw-r--r--. 1 root root 0 11月 9 00:15 2 -rw-r--r--. 1 root root 0 11月 9 00:15 3 -rw-r--r--. 1 root root 0 11月 9 00:15 4 -rw-r--r--. 1 root root 0 11月 9 00:15 5 ![](https://file.jishuzhan.net/article/1723631708256866306/d9b60e2768852dd368ba9320823b7d6e.webp) \[root@client test2\]# touch 6 \[root@client test2\]# ll 总用量 0 -rw-r--rw-. 1 root root 0 11月 9 00:15 1 -rw-r--rw-. 1 root root 0 11月 9 00:15 2 -rw-r--rw-. 1 root root 0 11月 9 00:15 3 -rw-r--rw-. 1 root root 0 11月 9 00:15 4 -rw-r--rw-. 1 root root 0 11月 9 00:15 5 -rw-r--r--. 1 nfs-upload nfs-ipload 0 11月 9 15:44 6 ![](https://file.jishuzhan.net/article/1723631708256866306/75be5edf6d608650e7114084428ccd81.webp)

3、将/home/tom目录仅共享给192.168.110.136这台主机,并只有用户tom可以完全访问该目录

root@server \~\]# useradd tom \[root@server \~\]# touch /home/tom/{1..5} \[root@server \~\]# ll /home/tom/ 总用量 0 -rw-r--r--. 1 root root 0 11月 9 15:48 1 -rw-r--r--. 1 root root 0 11月 9 15:48 2 -rw-r--r--. 1 root root 0 11月 9 15:48 3 -rw-r--r--. 1 root root 0 11月 9 15:48 4 -rw-r--r--. 1 root root 0 11月 9 15:48 5 \[root@server \~\]# vim /etc/exports /home/tom 192.168.110.134(rw) \[root@server \~\]# chown tom /home/tom/ \[root@server \~\]# chgrp tom /home/tom/ \[root@server \~\]# cd /home/ \[root@server home\]# ll 总用量 4 drwx------. 14 fox fox 4096 10月 22 14:09 fox drwx------. 3 nfs-upload nfs-ipload 78 11月 9 00:21 nfs-upload drwx------. 3 tom tom 123 11月 9 15:48 tom ![](https://file.jishuzhan.net/article/1723631708256866306/0f4b10c58e6d71048e181ed23c12b6c4.webp) \[root@server \~\]# showmount -e Export list for server: /home/tom 192.168.110.134 客户端 \[root@client \~\]# mount -t nfs 192.168.110.136:/home/tom /nfs/test/ \[root@client \~\]# ll /nfs/test ls: 无法打开目录 '/nfs/test': 权限不够 ![](https://file.jishuzhan.net/article/1723631708256866306/261785c2d4bd10af8f8417f8f4bb75aa.webp) \[root@client \~\]# useradd tom \[root@client \~\]# su - tom \[tom@client \~\]$ ll /nfs/test/ 总用量 0 -rw-r--r--. 1 root root 0 11月 9 15:48 1 -rw-r--r--. 1 root root 0 11月 9 15:48 2 -rw-r--r--. 1 root root 0 11月 9 15:48 3 -rw-r--r--. 1 root root 0 11月 9 15:48 4 -rw-r--r--. 1 root root 0 11月 9 15:48 5 ![](https://file.jishuzhan.net/article/1723631708256866306/d26f63315d18d3444b097c211dce55cc.webp) 这里的的tom用户是指UID和GID都相同的用户 ![](https://file.jishuzhan.net/article/1723631708256866306/2bc4971e1788cefcd833924093b1ef1f.webp) ![](https://file.jishuzhan.net/article/1723631708256866306/f9e1f5d45f42a3e3ace8420746ee2934.webp)

相关推荐
地衣君7 小时前
RISC-V 开发板 + Ubuntu 23.04 部署 open_vins 过程
linux·ubuntu·risc-v
5:007 小时前
云备份项目
linux·开发语言·c++
码农101号8 小时前
Linux中shell编程表达式和数组讲解
linux·运维·服务器
云道轩8 小时前
升级centos 7.9内核到 5.4.x
linux·运维·centos
是小满满满满吗8 小时前
传输层:udp与tcp协议
linux·服务器·网络
爱学习的小道长9 小时前
Ubuntu Cursor升级成v1.0
linux·运维·ubuntu
EelBarb9 小时前
seafile:ubuntu搭建社区版seafile12.0
linux·运维·ubuntu
Xam_d_LM9 小时前
【Latex】Windows/Ubuntu 绘制 eps 矢量图通用方法(drawio),支持插入 Latex 数学公式
linux·ubuntu·科研·矢量图·drawio
小刘同学++9 小时前
ECB(电子密码本,Electronic Codebook) 和 CBC(密码分组链接,Cipher Block Chaining)区分于用途
网络·ssl
Mintimate9 小时前
云服务器 Linux 手动 DD 安装第三方 Linux 发行版:原理与实战
linux·运维·服务器