配置与管理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目录挂载到刚建的目录下
相关推荐
05大叔1 小时前
网络基础知识 域名,JSON格式,AI基础
运维·服务器·网络
安当加密1 小时前
无需改 PAM!轻量级 RADIUS + ASP身份认证系统 实现 Linux 登录双因子认证
linux·运维·服务器
dashizhi20151 小时前
服务器共享禁止保存到本地磁盘、共享文件禁止另存为本地磁盘、移动硬盘等
运维·网络·stm32·安全·电脑
卷福同学2 小时前
【养虾日记】QClaw操作浏览器自动化发文
运维·人工智能·程序人生·自动化
woho7788993 小时前
不同网段IP的网络打印机,打印、扫描设置
运维·服务器·网络
耗子会飞3 小时前
小白学习固定VM虚拟机的centos服务器的IP
运维·服务器·centos
门豪杰3 小时前
Ubuntu下安装Claude Code
linux·运维·ubuntu·claude·claude code
新新学长搞科研4 小时前
第五届电子、集成电路与通信技术国际学术会议(EICCT 2026)
运维·人工智能·自动化·集成测试·信号处理·集成学习·电气自动化
桌面运维家4 小时前
Windows/Linux双启动:BIOS/UEFI多配置桌面创建指南
linux·运维·windows
無法複制4 小时前
debian安装Postgresql-14.x
运维·postgresql·debian