重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1169|回复: 0

[系统管理] NFS配置

[复制链接]
发表于 2022-5-26 11:26:12 | 显示全部楼层 |阅读模式
服务器端IP:192.168.133.161
客户端IP:192.168.133.150
服务器端配置
安装软件包
[root@hisdb2 ~]# rpm -qa|grep nfs-*
nfs-utils-1.3.0-0.68.el7.2.x86_64
libnfsidmap-0.25-19.el7.x86_64
可以看到已安装nfs软件包,若未安装用以下命令执行安装.
[root@hisdb2 ~]#yum install -y nfs-utils

启动NFS服务
[root@hisdb2 ~]# systemctl start nfs-server
[root@hisdb2 ~]# systemctl enable nfs-server  (设置服务开机启动)
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

创建共享目录
[root@hisdb2 ~]# mkdir /share
修改NFS配置文件
[root@hisdb2 ~]# vim /etc/exports
添加如下内容:
/share 192.168.133.150/24(ro,sync)

说明:初始配置文件/etc/exports为空,添加的每一行是对一个共享目录的配置参数
常用的权限参数
参数                        权限
ro(默认)                客户机对NFS服务器中的共享目录有只读权限
rw                        客户机对NFS服务器中的共享目录有读写权限
root_squash(默认)        当客户机使用root用户访问时,将其映射为NFS服务端的匿名用户(nfsnobody)
sync(默认)                将数据同时写入到内存缓冲区和磁盘中,效率低,但可以保证数据的一致性
async                将数据先保存在内存缓冲区中,必要时才写入磁盘,效率较高,但可能造成数据丢失

启动服务使配置生效
[root@hisdb2 ~]# systemctl restart nfs-server

防火墙设置
[root@hisdb2 ~]# firewall-cmd --permanent --add-service=nfs
success
[root@hisdb2 ~]# firewall-cmd --reload
success

NFS客户端的配置
安装nfs-utils软件包
[root@oel ~]# rpm -qa|grep nfs-*
nfs-utils-1.3.0-0.68.el7.2.x86_64
libnfsidmap-0.25-19.el7.x86_64
说明:若未安装包,需安装

创建挂载点
[root@oel ~]# mkdir /mnt/share
挂载NFS共享
[root@oel ~]# mount 192.168.133.161:/share /mnt/share
[root@oel ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 3.8G     0  3.8G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G   13M  3.8G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   90G   16G   75G  18% /
/dev/sda1                2.0G  185M  1.9G  10% /boot
tmpfs                    781M   36K  781M   1% /run/user/1001
/dev/sr0                 4.4G  4.4G     0 100% /run/media/oracle/CentOS 7 x86_64
tmpfs                    781M     0  781M   0% /run/user/0
192.168.133.161:/share    91G   27G   65G  29% /mnt/share
说明:如上所示挂载成功

客户端执行
[root@oel ~]# cd /mnt/share
[root@oel share]# ll
total 0
[root@oel share]# touch 11
touch: cannot touch ‘11’: Read-only file system
说明:客户端无法在共享目录创建文件.

服务端执行
[root@hisdb2 share]# touch /share/leo.txt
[root@hisdb2 share]# ll
total 0
-rw-r--r-- 1 root root 0 May 19 21:32 leo.txt
[root@hisdb2 share]# vi leo.txt
[root@hisdb2 share]# cat leo.txt
hello,world!

客户端执行
[root@oel share]# ll
total 0
-rw-r--r-- 1 root root 0 May 19 21:32 leo.txt
[root@oel share]# cat leo.txt
hello,world!
结论:/share共享目录权限为ro,客户端只能查看.

现在服务端测试创建读写共享目录.
服务端执行
[root@hisdb2 ~]# mkdir /data
[root@hisdb2 ~]# vi /etc/exports
[root@hisdb2 ~]# cat /etc/exports
/share 192.168.133.150/24(ro,sync)
/data  192.168.133.150/24(rw,sync)
[root@hisdb2 ~]# systemctl restart nfs-server

客户端执行
[root@oel share]# mkdir /mnt/data
[root@oel share]# mount 192.168.133.161:/data /mnt/data
[root@oel share]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 3.8G     0  3.8G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G   13M  3.8G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   90G   16G   75G  18% /
/dev/sda1                2.0G  185M  1.9G  10% /boot
tmpfs                    781M   36K  781M   1% /run/user/1001
/dev/sr0                 4.4G  4.4G     0 100% /run/media/oracle/CentOS 7 x86_64
tmpfs                    781M     0  781M   0% /run/user/0
192.168.133.161:/share    91G   27G   65G  29% /mnt/share
192.168.133.161:/data     91G   27G   65G  29% /mnt/data
说明:如上成功挂载/mnt/data

[root@oel share]# cd /mnt/data
[root@oel data]# ll
total 0
[root@oel data]# mkdir test
mkdir: cannot create directory ‘test’: Permission denied
问题:服务器端/data已经配置为rw权限,为什么客户端依然不能创建目录呢?
原因:当客户机使用root用户访问共享目录时,其会被映射为NFS服务端的匿名用户(nfsnobody)
解决方案如下:
服务端执行
[root@hisdb2 ~]# ll /data
drwxr-xr-x 2 root root 6 May 19 21:35 /data
说明:如上/data用户和所属组都是root,可以修改为777,或者将/data用户修改成nfsnobody

[root@hisdb2 ~]# chown nfsnobody /data
[root@hisdb2 ~]# ll /data
drwxr-xr-x 2 nfsnobody root 6 May 19 21:35 /data

客户端再次执行
[root@oel data]# mkdir test
[root@oel data]# ll
total 0
drwxr-xr-x 2 nfsnobody nfsnobody 6 May 19 21:45 test
[root@oel data]# touch leo.txt
[root@oel data]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 19 21:46 leo.txt
drwxr-xr-x 2 nfsnobody nfsnobody 6 May 19 21:45 test
说明:如上所示客户端成功创建目录和文件.

以上挂载当客户端重启后会自动失效,现在实现NFS共享永久挂载
[root@oel ~]# vi /etc/fstab
文末添加:
192.168.133.161:/data   /mnt/data               nfs     rw,soft,intr    0 0
参数说明:
soft:使用软挂载的方式挂载系统,若Client的请求得不到回应,则重新请求并传回错误信息
intr:允许NFS中断文件操作和向调用它的程序返回值,默认不允许文件操作被中断

客户端执行
[root@oel ~]# reboot
[root@oel ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 3.8G     0  3.8G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G   13M  3.8G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   90G   16G   75G  18% /
/dev/sda1                2.0G  185M  1.9G  10% /boot
192.168.133.161:/data     91G   27G   65G  29% /mnt/data
tmpfs                    781M  8.0K  781M   1% /run/user/42
tmpfs                    781M     0  781M   0% /run/user/0

如上:开机后/mnt/data自动挂载.

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-4-18 22:30 , Processed in 0.091221 second(s), 21 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表