NFS服务端(示例ip:10.213.120.71)
1 安装nfs
[root@nfs_server ~]# yum -y install nfs-utils rpcbind
(小提示:在安装完nfs-utils后,rpcbind默认是启动了的。)
2 enable services。设置开机启动nfs相关服务。
[root@nfs_server ~]# systemctl enable rpcbind
[root@nfs_server ~]# 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@nfs_server ~]# systemctl enable nfs-lock
[root@nfs_server ~]# systemctl enable nfs-idmap
3 启动nfs service
[root@nfs_server ~]# systemctl start rpcbind
[root@nfs_server ~]# systemctl start nfs-server
[root@nfs_server ~]# systemctl start nfs-lock
[root@nfs_server ~]# systemctl start nfs-idmap
4 创建需要共享的目录
[root@nfs_server ~]# mkdir -p /data/zbox -p 级联创建
[root@nfs_server ~]# chmod -R 777 /data/zbox 更改share文件夹及其子文件夹权限为777
5 配置需要共享的目录到 /etc/exports下,xxx.xxx.xxx.xxx为需要共享的对象ip地址。
[root@nfs_server ~]# echo "/data/zbox 10.213.120.71.*(rw,sync,no_root_squash)" >> /etc/exports
[root@nfs_server ~]# exportfs -rv 使exports的修改生效
或者
[root@nfs_server ~]# echo "/data/zbox 10.213.120.71/24(rw,sync,no_root_squash)" >> /etc/exports
[root@nfs_server ~]# exportfs -rv 使exports的修改生效
6 检查共享目录是否设置正确
[root@nfs_server /]# showmount -e
Export list for nfs_server:
/data/zbox 10.213.120.*
7 调整防火墙配置
【 111 2049是固定端口】
vim /etc/services
mountd 20048/tcp
mountd 20048/udp
nfs 2049/tcp
nfs 2049/udp
nlockmgr 29194/tcp -- 是动态的,可忽略
nlockmgr 29194/udp
firewall-cmd --add-port=111/tcp --permanent
firewall-cmd --add-port=2049/tcp --permanent
firewall-cmd --add-port=20048/tcp --permanent
firewall-cmd --reload
[root@nfs_server ~]# firewall-cmd --add-service=nfs --permanent --zone=public
success
[root@nfs_server ~]# firewall-cmd --add-service=mountd --permanent --zone=public
success
[root@nfs_server ~]# firewall-cmd --add-service=rpc-bind --permanent --zone=public
success
[root@nfs_server ~]# firewall-cmd --reload 重新载入配置,使其生效
success
可使用 命令 iptables -L -n 查看开放的端口
target prot opt source destination
cali-INPUT all -- 0.0.0.0/0 0.0.0.0/0 /* cali:Cz_u1IQiXIMmKD4c */
ACCEPT udp -- 0.0.0.0/0 169.254.25.10 udp dpt:53
ACCEPT tcp -- 0.0.0.0/0 169.254.25.10 tcp dpt:53
KUBE-FIREWALL all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:67
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:67
重启服务
systemctl start rpcbind.service
systemctl start nfs-server.service
NFS客户端
注意:客户端不需要启动nfs服务
1 安装nfs
[root@nfs_client ~]# yum -y install nfs-utils
2 检查共享目录是否设置正确,xxx.xxx.xxx.xxx 为共享服务器地址
[root@nfs_client ~]# showmount -e 10.213.120.71
Export list for 10.213.120.71:
/data/zbox 10.213.120.*
3 挂载远程服务器NFS分区到本地挂载点
# make mount points
[root@nfs_client ~]# mkdir -p /data/zbox
# mount nfs
[root@nfs_client ~]# mount -t nfs 10.213.120.71:/data/zbox /data/zbox
挂载格式解读:
mount -t nfs -o nolock,vers=2 10.0.0.147:/work/nfs /mnt
解释一下:
mount :挂载命令
nfs :使用的协议
nolock :不阻塞
vers : 使用的NFS版本号
IP : NFS服务器的IP(NFS服务器运行在哪个系统上,就是哪个系统的IP)
/work/nfs: 要挂载的目录(Ubuntu的目录)
/mnt : 要挂载到的目录(开发板上的目录,注意挂载成功后,/mnt下原有数据将会被隐藏,无法找到)
取消挂载(先df -h 查看分区挂载情况)
[root@localhost local]# umount 10.213.120.71:/data/zbox
取消挂载方式1
mount -t nfs 192.168.222.38:/share/api/images /home/work/images
取消挂载
#挂载
umount 192.168.222.38:/share/api/images
取消挂载方式2:
#挂载
mount -t nfs 192.168.222.38:/share/api/images /home/work/images
取消挂载
umount /home/work/images
验证nfs网络文件共享是否成功:
切换到nfs_server端:
[root@nfs_server ~]# cd /data/zbox/
[root@nfs_server share]# ls
[root@nfs_server share]# touch a.txt
切换到nfs_client端:
查看挂载是否成功,即查看是否在被挂载目录下,同步生成了 a.txt文件
[root@nfs_client ~]# mkdir -p /data/zbox
[root@nfs_client ~]# mount -t nfs 10.213.120.71:/data/zbox /data/zbox
[root@nfs_client ~]# cd /data/zbox/
[root@nfs_client share]# ls
a.txt 可看到在nfs客户端同步生成了a.txt
在客户端使用命令df查看整个的挂载情况
[root@nfs_client etc]# df
10.213.120.71:/data/zbox 52403200 15967232 36435968 31% /data/zbox
注意:
- 若服务端挂了,则客户端将无法使用
- 服务端若设置了开机启动,则每次重启后,服务仍然活着
客户端重启后,需要重新挂载
配置客户端开机自动挂载nfs共享目录
vi /etc/fstab,增加一行
10.213.120.71:/data/zbox /data/zbox nfs defaults 0 0
/etc/fstab 里面的挂载配置立即生效的命令
sudo mount -a
或者在在/etc/rc.d/rc.local中添加一行
mount 10.213.120.71:/data/zbox /data/zbox
chmod +x /etc/rc.d/rc.local