nfs服务
应用场景
实现多台客户端同时挂载一台nfs server时,无论从哪一个客户端写入数据,其他客户端同样也可以读写
,即让所有nfs客户端写入到nfs server 的文件或者目录在nfs server上的用户和组都是同一个名称.
好处
节省本地存储空间,将常用的数据存放在一台NFS服务器上且可以通过网络访问,那么本地终端将可以减少自身存储空间的使用。
用户不需要在网络中的每个机器上都建有Home目录,Home目录可以放在NFS服务器上且可以在网络上被访问使用。
一些存储设备如软驱、CDROM和Zip(一种高储存密度的磁盘驱动器与磁盘)等都可以在网络上被别的机器使用。这可以减少整个网络上可移动介质设备的数量。
手动搭建nfs服务器
实验环境
主机名 | IP地址 |
---|---|
nfs-server | 192.168.100.33 |
nfs-client | 192.168.100.60 |
关闭防火墙
[root@nfs-server ~]# systemctl stop firewalld.service
[root@nfs-server ~]# systemctl disable firewalld.service
关闭selinux
[root@nfs-server ~]# setenforce 0
实验一
开放/nfs/shared目录,供所有用户查阅资料
服务端配置
- 安装nfs-utils,开启,并加入到开机自启
[root@nfs-server ~]# yum -y install nfs-utils
[root@nfs-server ~]# systemctl start nfs.service
[root@nfs-server ~]# systemctl enable nfs.service
- 新建/nfs/shared目录
[root@nfs-server ~]# mkdir /nfs/shared -p
- 编辑/etc/exports
[root@nfs-server shared]# cat /etc/exports
/nfs/shared *(ro,sync)
- 重新读取exports文件
[root@nfs-server ~]# exportfs -ar
- 重启nfs服务
[root@nfs-server ~]# systemctl restart nfs.service
客户端
- 查看nfs服务器上的共享目录
[root@nfs-client ~]# showmount -e 192.168.100.33
Export list for 192.168.100.33:
/nfs/shared 192.168.100.0/24
- 将共享目录挂载到mnt目录下
[root@nfs-client ~]# mount -t nfs 192.168.100.33:/nfs/shared /mnt/
查看
[root@nfs-client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 8897536 1605740 7291796 19% /
devtmpfs 490012 0 490012 0% /dev
tmpfs 499860 0 499860 0% /dev/shm
tmpfs 499860 6856 493004 2% /run
tmpfs 499860 0 499860 0% /sys/fs/cgroup
/dev/sda1 1038336 121688 916648 12% /boot
tmpfs 99972 0 99972 0% /run/user/0
192.168.100.33:/nfs/shared 19383296 12597504 6785792 65% /mnt
- 测试
[root@nfs-client mnt]# mkdir aaa
mkdir: cannot create directory ‘aaa’: Read-only file system //没有写入权限
[root@nfs-client mnt]# cat aaa //只有读取权限
xiefei.com
实验二
开放/nfs/upload目录为192.168.100.0网段的数据上传目录,
客户端
- 新建/nfs/upload
[root@nfs-server ~]# mkdir /nfs/upload
- 新建用户和用户组uid和gid都是300
[root@nfs-server ~]# groupadd nfs-upload -g 300
[root@nfs-server ~]# useradd nfs-upload -u 300 -g 300
- 将upload目录的属主和属组加入到nfs-upload
[root@nfs-server ~]# chown -R nfs-upload.nfs-upload /nfs/upload/
- 修改/etc/exports配置文件
[root@nfs-server ~]# cat /etc/exports
/nfs/shared *(ro,sync)
/nfs/upload 192.168.100.0/24(rw,sync,anonuid=300,anongid=300)
- 重新读取配置文件
[root@nfs-server ~]# exportfs -ar
- 重启nfs服务
[root@nfs-server ~]# systemctl restart nfs.service
客户端
- 查看nfs服务器上的共享目录
[root@nfs-client ~]# showmount -e 192.168.100.33
Export list for 192.168.100.33:
/nfs/shared *
/nfs/upload 192.168.100.0/24
- 将共享目录挂载到nfs目录上
[root@nfs-client ~]# mkdir /nfs
[root@nfs-client ~]# mount -t nfs 192.168.100.33:/nfs/upload /nfs
- 测试
检查nfs属组和属主
[root@nfs-client ~]# ll /nfs/
total 0
drwxr-xr-x 2 300 300 16 Sep 5 12:33 haha
在客户端的nfs目录下新建haha目录
[root@nfs-client ~]# mkdir /nfs/haha //有写入权限
[root@nfs-server upload]# echo "123.com" >> haha/hh //在服务端的haha目录写入文件
[root@nfs-client ~]# cat /nfs/haha/hh //有读取权限
123.com
自动挂载
卸载nfs目录挂载
[root@nfs-client ~]# umount /nfs/
在/etc/fstab写入配置文件
192.168.100.33:/nfs/upload /nfs nfs defaults 0 0
然后重新加载配置文件
[root@nfs-client ~]# mount -a
查看
[root@nfs-client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 8897536 1605740 7291796 19% /
devtmpfs 490012 0 490012 0% /dev
tmpfs 499860 0 499860 0% /dev/shm
tmpfs 499860 6828 493032 2% /run
tmpfs 499860 0 499860 0% /sys/fs/cgroup
/dev/sda1 1038336 121688 916648 12% /boot
tmpfs 99972 0 99972 0% /run/user/0
192.168.100.33:/nfs/upload 19383296 12596480 6786816 65% /nfs