在做docker volume挂载的实验,想要挂载一个网络卷,所以学习了一些CentOS7中配置nfs服务的知识。
NFS的应用还是挺广泛的,有时候可以将linux上的一个目录作为文件挂载在windows中,方便编写的程序在linux上快速编译运行;有时候可以起到一个乞丐版nas的作用,能够通过网络访问指定目录中的文件,对文件进行读写等,方便文件共享。
实验环境:
NFS服务器
地址:192.168.0.105
系统:CentOS Linux release 7.2.1511 (Core)NFS客户端
地址:192.168.0.104
系统:CentOS Linux release 7.2.1511 (Core)
1、服务端首先安装nfs-utils
和rpcbind
[root@localhost etc]# yum -y install nfs-utils rpcbind
修改/etc/exports
文件,添加配置
[root@localhost etc]# vi /etc/exports
/opt/test/ 192.168.0.105/24(rw,no_root_squash,no_all_squash,sync,anonuid=501,anongid=501)
保存后,到/opt
目录,创建test
目录作为共享目录
[root@localhost etc]# cd /opt/
[root@localhost opt]# mkdir test
2、服务端生效配置文件
[root@localhost opt]# exportfs -r
配置文件说明:
- /opt/test: 共享目录
- 192.168.0.105/24: 可以为一个网段,一个IP,也可以是域名,域名支持通配符 如: *.qq.com
- rw:read-write,可读写;
- ro:read-only,只读;
- sync:文件同时写入硬盘和内存;
- async:文件暂存于内存,而不是直接写入内存;
- no_root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。显然开启这项是不安全的。
- root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,拥有匿名用户权限,通常他将使用nobody或nfsnobody身份;
- all_squash:不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都是拥有匿名用户权限;
- anonuid:匿名用户的UID值,可以在此处自行设定。
- anongid:匿名用户的GID值。
3、服务端启动NFS服务
[root@localhost opt]# systemctl start nfs rpcbind
为了避免麻烦,关闭防火墙
[root@localhost opt]# systemctl stop firewalld
关闭防火墙会降低安全性,实验环境为内网环境,相对还好。如果在外网环境,需要将服务器的24端口加入防火墙的通过列表
如果启动了iptables
服务,同样也关闭
[root@localhost opt]# systemctl stop iptables
4、客户端安装nfs-utils
[root@localhost ~]# yum install -y nfs-utils
查看服务端的情况:
[root@localhost ~]# showmount -e 192.168.0.105
Export list for 192.168.0.105:
/opt/test 192.168.0.105/24
[root@localhost ~]# rpcinfo -p 192.168.0.105
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 49340 status
100024 1 tcp 42911 status
100005 1 udp 20048 mountd
100005 1 tcp 20048 mountd
100005 2 udp 20048 mountd
100005 2 tcp 20048 mountd
100005 3 udp 20048 mountd
100005 3 tcp 20048 mountd
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 3 tcp 2049 nfs_acl
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 3 udp 2049 nfs_acl
100021 1 udp 59176 nlockmgr
100021 3 udp 59176 nlockmgr
100021 4 udp 59176 nlockmgr
100021 1 tcp 33527 nlockmgr
100021 3 tcp 33527 nlockmgr
100021 4 tcp 33527 nlockmgr
客户端挂载目录到/mnt
[root@localhost ~]# mount -t nfs 192.168.0.105:/opt/test /mnt
测试对共享目录的操作情况:
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
[root@localhost mnt]# touch hahaha
[root@localhost mnt]# ls
hahaha
服务端查看文件:
[root@localhost ~]# cd /opt/test/
[root@localhost test]# ll
总用量 0
-rw-r--r--. 1 root root 0 6月 4 20:01 hahaha
NFS服务最基本的功能就完成了,详细的说明可以看鸟哥的私房菜,非常非常详细!
鸟哥私房菜–>NFS 服务器(简体)鸟哥私房菜–>NFS 服务器(繁体)