实验准备
软件:VMware Workstation Pro
虚拟机:Red Hat Enterprise Linux 7 64 位 两台(一台服务器,一台客户端)
综合案例
某公司现需配置一台NFS服务器,NFS服务器的I地址为192.168.100.254。共享的要求如下所述:
(1)共享/mnt/share目录,允许192.168.100.0/24网段的计算机访问该共享目录,可进行读写操作。
(2)共享/mnt managerdata目录,允许公司zhang 用户利用P地址为192.168.100.10主机对该共享目录拥有读写权限
(3)共享/ mnt/upload目录,允许该目录作为192.168.100.0/24 网段主机利用该目录做为上传目录,其中/mnt/upload 的用户和所属组为nfsupload,UID 和GID均为123
(4)共享/home/ nfs目录,该目录允许192.168.100.0/24网段的用户访问,设置该目录为只读。
实例实现
1、在NFS服务器安装NFS服务软件包。
[root@localhost Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 38G 2.9G 35G 8% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
/dev/sr0 3.5G 3.5G 0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@localhost Desktop]# mkdir /mnt/cdrom && mount /dev/sr0 /mnt/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost Desktop]# vim /etc/yum.repos.d/a.repo
[a]
name=welcome to redhatroom
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
[root@localhost Desktop]# yum install -y rpcbind
[root@localhost Desktop]# yum install -y nfs-utils
2、创建相应目录和测试文件
22 mkdir /mnt/share
23 mkdir /mnt/managerdata
24 mkdir /mnt/upload
25 mkdir /mnt/nfs
26 touch /mnt/share/test1
27 touch /mnt/share/test2
28 touch /mnt/managerdata/test3
29 touch /mnt/managerdata/test4
30 touch /mnt/upload/upload.txt
31 touch /mnt/nfs/test5
32 touch /mnt/nfs/test6
3、设置共享目录的权限属性
33 chmod 1777 /mnt/share
34 ll -d /mnt/share/
35 useradd zhang
36 echo redhat | passwd --stdin zhang
37 chmod 700 /mnt/managerdata
39 chown -R zhang:zhang /mnt/managerdata/
40 ll -d /mnt/managerdata/
41 groupadd -g 123 nfsupload
42 man groupadd
43 useradd -g 123 -u 123 -M nfsupload
44 tail -n1 /etc/group
45 tail -n1 /etc/passwd
46 cat /etc/passwd | grep nfs
48 chown -R nfsupload:nfsupload /mnt/upload/
49 ll -d /mnt/upload/
50 ll -d /mnt/nfs/
4、编辑/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(ro)
注意,NFS客户端地址与权限之间没有空格。
参数 | 作用 |
---|---|
ro | 只读 |
rw | 读写 |
root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户 |
no_root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员 |
all_squash | 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户 |
sync | 同时将数据写入到内存与硬盘中,保证不丢失数据 |
async | 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据 |
5、设置防火墙放行规则,启动服务
54 systemctl restart rpcbind.service
55 systemctl status rpcbind.service
63 systemctl start nfs-service
65 systemctl status nfs-server.service
6、在nfs客户端查看共享目录,建立目录执行挂载
[root@localhost 桌面]# showmount -e 192.168.100.254
Export list for 192.168.100.254:
/mnt/nfs 192.168.100.0/24
/mnt/upload 192.168.100.0/24
/mnt/share 192.168.100.0/24
/mnt/managerdata 192.168.100.10
[root@localhost 桌面]# mkdir /mnt/clientnfs
[root@localhost 桌面]# mount -t nfs 192.168.100.254:/mnt/nfs /mnt/clientnfs/
[root@localhost 桌面]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
/dev/sr0 3.5G 3.5G 0 100% /mnt/cdrom
192.168.100.254:/mnt/nfs 18G 3.0G 15G 17% /mnt/clientnfs
[root@localhost 桌面]# cd /mnt/clientnfs/
[root@localhost clientnfs]# ls
test5 test6
[root@localhost clientnfs]# touch nfs3.txt
touch: 无法创建"nfs3.txt": 只读文件系统
[root@localhost clientnfs]# mkdir /mnt/clientupload
[root@localhost clientnfs]# mount -t nfs 192.168.100.254:/mnt/upload /mnt/clientupload/
[root@localhost clientnfs]# cd /mnt/clientupload/
[root@localhost clientupload]# ls
upload.txt
[root@localhost clientupload]# touch a
[root@localhost clientupload]# ls
a upload.txt
[root@localhost clientupload]# ll
总用量 0
-rw-r--r--. 1 123 123 0 10月 30 15:12 a
-rw-r--r--. 1 123 123 0 10月 30 14:42 upload.txt
[root@localhost clientupload]# groupadd -g 123 nfsupload
[root@localhost clientupload]# useradd -g 123 -u 123 -M nfsupload
[root@localhost clientupload]# ll
总用量 0
-rw-r--r--. 1 nfsupload nfsupload 0 10月 30 15:12 a
-rw-r--r--. 1 nfsupload nfsupload 0 10月 30 14:42 upload.txt
可以看到root用户创建的文件属主仍是nfsupload
[root@localhost clientupload]# mkdir /mnt/share
[root@localhost clientupload]# mount -t nfs 192.168.100.254:/mnt/share/ /mnt/share/
[root@localhost clientupload]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
/dev/sr0 3.5G 3.5G 0 100% /mnt/cdrom
192.168.100.254:/mnt/share 18G 3.0G 15G 17% /mnt/share
[root@localhost clientupload]# cd /mnt/share/
[root@localhost share]# ls
test1 test2
[root@localhost share]# ll
总用量 0
-rw-r--r--. 1 root root 0 10月 30 14:41 test1
-rw-r--r--. 1 root root 0 10月 30 14:41 test2
[root@localhost share]# touch 1
[root@localhost share]# ll
总用量 0
-rw-r--r--. 1 root root 0 10月 30 15:33 1
-rw-r--r--. 1 root root 0 10月 30 14:41 test1
-rw-r--r--. 1 root root 0 10月 30 14:41 test2
[root@localhost share]# rm 1
rm:是否删除普通空文件 "1"?y
[root@localhost share]# su - student
上一次登录:日 9月 18 20:00:42 CST 2022从 192.168.150.138pts/1 上
[student@localhost ~]$ cd /mnt/share/
[student@localhost share]$ ls
test1 test2
[student@localhost share]$ ll
总用量 0
-rw-r--r--. 1 root root 0 10月 30 14:41 test1
-rw-r--r--. 1 root root 0 10月 30 14:41 test2
[student@localhost share]$ touch 1
[student@localhost share]$ ll
总用量 0
-rw-rw-r--. 1 student student 0 10月 30 15:33 1
-rw-r--r--. 1 root root 0 10月 30 14:41 test1
-rw-r--r--. 1 root root 0 10月 30 14:41 test2
[student@localhost share]$ rm test
rm: 无法删除"test": 没有那个文件或目录
[student@localhost share]$ rm test1
rm:是否删除有写保护的普通空文件 "test1"?y
rm: 无法删除"test1": 不允许的操作
[student@localhost share]$ rm 1
[student@localhost share]$ ls
test1 test2
[student@localhost share]$ cd ..
[student@localhost mnt]$ ll
总用量 4
dr-xr-xr-x. 10 root root 4096 5月 7 2014 cdrom
drwxr-xr-x. 2 root root 30 10月 30 14:42 clientnfs
drwxr-xr-x. 2 nfsupload nfsupload 31 10月 30 15:12 clientupload
drwxrwxrwt. 2 root root 30 10月 30 15:33 share