配置与管理NFS服务器

配置与管理NFS服务器

NFS:即网络文件系统,只提供网络文件共享,不提供数据传输

作用:可以是用户在异构网络操作系统之间进行文件系统共享

概述:客户机与服务器之间可以共享文件,但不可数据传输功能;需要用到RPC(是一种进程间通过网络进行交互通信的机制)

工作原理

安装NFS服务器

shell 复制代码
#查看必要软件包
[root@localhost ~]# rpm -qa | grep nfs-utils
[root@localhost ~]# rpm -qa | grep rpcbind
#yum安装NFS相关软件包
[root@localhost ~]# yum clean all
[root@localhost ~]# yum -y install nfs-utils rpcbind
#先启动rpc服务,再启动nfs服务
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl start nfs

配置NFS服务器

shell 复制代码
#改NFS配置文件/etc/exports,centos默认是没有exports文件需要手动添加
格式:共享目录 [客户端n(参数)]

(1)共享目录:nfs服务器需共享目录的实际路径
(2)客户端:指可以访问nfs服务器共享目录的计算机

3,配置NFS客户端

showmount命令:查看NFS服务器上的共享目录

shell 复制代码
#查看nfs服务器信息;关闭slinux和防火墙
showmount [选项] (参数)
-d:仅显示已被nfs服务器加载共享目录
-e:显示nfs服务器上所有的共享目录
参数:指定NFS服务器的IP地址或主机名

#挂载nfs服务器的共享目录
mount -t 服务器名或IP地址: 输出目录 本地挂载目录
当客户端上的用户无权访问nfs服务器上的共享目录,就无法进行挂载。
挂载成功后对目录的操作就等于在NFS服务器上的目录中进行操作。

#卸载nfs服务器
umount 挂载点

#启动时自动挂载nfs的共享目录
nfs服务器名或IP地址: 输出目录 本地挂载目录 nfs default 0 0

综合案列

shell 复制代码
`	#配置一台NFS服务器,IP地址为192.168.100.254.共享条件:
`(1)共享目录/mnt/share目录允许192.168.100.0/24网段访问,可读写
`(2)共享/mnt/managerdata目录允许zhang用户用IP地址192.168.100.0主机访问,可读写
`(3)共享/mnt/upload目录允许192.168.100.0/24网段主机作为上传目录,/mnt/upload的用户和所属组nfsupload,UID=GID=123
`(4)共享/home/nfs目录除允许192.168.100.0/24网段用户访问外,也向Internet提供数据,可读

服务器

shell 复制代码
#在nfs服务器端安装nfs服务软件包
[root@localhost ~]# yum -y install nfs-uils rpcbind

#创建相应目录和测试文件
[root@localhost ~]# mkdir -p /mnt/share
[root@localhost ~]# mkdir -p /mnt/managerdata
[root@localhost ~]# mkdir -p /mnt/upload
[root@localhost ~]# mkdir -p /mnt/nfs
[root@localhost ~]# touch /mnt/share/share1.txt
[root@localhost ~]# touch /mnt/share/share2.txt
[root@localhost ~]# touch /mnt/managerdata/managerdata1.txt
[root@localhost ~]# touch /mnt/managerdata/managerdata2.txt
[root@localhost ~]# touch /mnt/upload/upload.txt
[root@localhost ~]# touch /mnt/nfs1.txt
[root@localhost ~]# touch /mnt/nfs2.txt

#设置共享目录的权限属性
//要求1
[root@localhost ~]# chmod 1777 /mnt/share
[root@localhost ~]# ll -d /mnt/share/
drwxrwxrwt. 2 root root 42 May  1 08:58 /mnt/share
//要求2
[root@localhost ~]# useradd zhang
[root@localhost ~]# passwd zhang
Changing password for user zhang.
New password: 			#密码是:123
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@localhost mnt]# echo redhat | passwd --stdin zhang			#更新令牌
[root@localhost ~]# cat /etc/passwd |grep zhang
zhang:x:1001:1001::/home/zhang:/bin/bash
[root@localhost ~]# chmod 700 /mnt/managerdata/
[root@localhost ~]# chown -R zhang:zhang /mnt/managerdata/
[root@localhost ~]# ll -d /mnt/managerdata/
drwx------. 2 zhang zhang 54 May  2 20:40 /mnt/managerdata/

//要求3
[root@localhost ~]# groupadd -g 123 nfsupload
[root@localhost ~]# useradd -g 123 -u 123 -M nfsupload
[root@localhost ~]# cat /etc/passwd |grep nfs
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
nfsup:x:123:123::/home/nfsup:/bin/bash
[root@localhost ~]# chown -R nfsupload:nfsupload /mnt/upload
[root@localhost ~]# ll -d /mnt/upload
drwxr-xr-x. 2 nfsup nfsup 20 May  1 08:59 /mnt/up/
//要求4
[root@localhost ~]# ll -d /mnt/nfs
drwxr-xr-x. 2 root root 6 May  1 08:57 /mnt/nfs

#编辑/etc/exports文件
[root@localhost ~]# vim /etc/exports
/mnt/share      192.168.100.0/24(rw,no_root_squash)
/mnt/managerdata        192.168.100.10(rw)
/mnt/upload     192.168.100.0/24(rw,all_squash,anonuid=123,anongid=123)
/mnt/nfs        192.168.100.0/24 *(rw,sync,root_squash)

#关闭防火墙,设置Selinux为允许,并重启nfs服务
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive
[root@localhost ~]# systemctl restart nfs

客户端

shell 复制代码
#安装客户端软件
[root@localhost ~]# yum -y install nfs-utils
#连接服务器
[root@localhost mnt]# showmount -e 192.168.88.121
Export list for 192.168.88.121:
/mnt/nfs         (everyone)
/mnt/upload      192.168.100.0/24
/mnt/share       192.168.100.0/24
/mnt/managerdata 192.168.100.10
shell 复制代码
#在客户端上建立/mnt/CilentNFS目录,将NFS服务器上的/mnt/nfs目录挂载到刚建的目录下
[root@localhost mnt]# mkdir /mnt/CilentNFS/
[root@localhost mnt]# mount -t nfs 192.168.88.121:/mnt/nfs /mnt/ClientNFs/
[root@localhost mnt]# cd /mnt/ClientNFs/
[root@localhost ClientNFs]# ls
nfs1.txt  nfs2.txt
[root@localhost ClientNFs]# touch nfs3.txt
touch: cannot touch 'nfs3.txt': Permission denied

##在客户端上建立/mnt/CilentUpload目录,将NFS服务器上的/mnt/ClientUpload目录挂载到刚建的目录下
相关推荐
Lalolander24 分钟前
设备制造行业如何避免项目管理混乱?
运维·制造·工程项目管理·四算一控·epc·环保设备工程·设备制造
LucianaiB43 分钟前
【金仓数据库征文】_AI 赋能数据库运维:金仓KES的智能化未来
运维·数据库·人工智能·金仓数据库 2025 征文·数据库平替用金仓
prinrf('千寻)1 小时前
nacos设置权重进行负载均衡不生效
运维·负载均衡
Lary_Rock1 小时前
Android 编译问题 prebuilts/clang/host/linux-x86
android·linux·运维
子非衣1 小时前
Windows云主机远程连接提示“出现了内部错误”
服务器·windows
绵绵细雨中的乡音1 小时前
Linux进程学习【基本认知】
linux·运维·学习
lLinkl2 小时前
项目笔记2:post请求是什么,还有什么请求
服务器·网络协议·http
珹洺3 小时前
Linux操作系统从入门到实战(三)Linux基础指令(上)
linux·运维·服务器
再睡一夏就好3 小时前
Linux常见工具如yum、vim、gcc、gdb的基本使用,以及编译过程和动静态链接的区别
linux·服务器·c语言·c++·笔记
剁椒排骨3 小时前
win11什么都不动之后一段时间黑屏桌面无法显示,但鼠标仍可移动,得要熄屏之后才能进入的四种解决方法
运维·windows·经验分享·计算机外设·win11·win10