配置与管理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目录挂载到刚建的目录下
相关推荐
zzzzzz31019 小时前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode19 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220701 天前
如何搭建本地yum源(上)
运维
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
小宇宙Zz4 天前
Maven依赖冲突
java·服务器·maven
Inhand陈工5 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智5 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_5 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化