Change The World

沉默是一种力量,嘶吼就不是力量了?

0%

NFS安装及使用

环境准备

系统:CentOS 7.6 //目前最新发行版,2018年12月31日

网络:可以连接互联网,用于下载必须的软件包

简介与说明

rpcbind

用于将RPC程序编号映射到IP地址端口号组合, RPC进程在启动时通知rpcbind,注册他们正在侦听的端口以及他们希望服务的RPC程序编号。然后,客户端系统使用特定的RPC程序编号联系服务器上的rpcbind。 rpcbind服务将客户端重定向到正确的端口号,以便它可以与请求的服务进行通信。

NFS

网络文件系统(NFS)允许远程主机通过网络安装文件系统,并与这些文件系统进行交互,就好像它们是本地安装的一样。这使系统管理员能够将资源整合到网络上的集中式服务器上。

NFS版本都依赖于客户端和服务器之间的远程过程调用(RPC)。 一般Linux发行版(Fedora、RHEL、CentOS)RPC服务由rpcbind服务控制。要共享或挂载NFS文件系统,以下服务可以协同工作,具体取决于实现的NFS版本

开始安装

软件安装

1
[ocloud@centos-linux ~]$ yum -y install nfs-utils rpcbind

配置

1
2
[ocloud@centos-linux ~]$ sudo vi /etc/exports
/opt/share/ 192.168.0.0/24(rw,no_root_squash,no_all_squash,sync,anonuid=501,anongid=501) #添加内容

简单说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/opt/share/           共享目录

192.168.0.0/24、192.168.0.110、nfs.nfsdemo.org、*.nfsdemo.org 主持网段、IP、域名、通配符域名

sync 文件同时写入硬盘和内存;

async 文件暂存于内存,而不是直接写入内存;

no_root_squash NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。显然开启这项是不安全的。

root_squash NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,拥有匿名用户权限,通常他将使用nobody或nfsnobody身份;

all_squash 不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都是拥有匿名用户权限;

anonuid 匿名用户的UID值,可以在此处自行设定

anongid 匿名用户的GID值

配置文件生效

1
2
[ocloud@centos-linux ~]$ sudo mkdir -p  /opt/share/ #文件夹必须要存在
[ocloud@centos-linux ~]$ sudo exportfs -r

开启服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# rpcbind
[ocloud@centos-linux ~]$ sudo systemctl start rpcbind
[sudo] ocloud 的密码:
[ocloud@centos-linux ~]$ sudo systemctl status rpcbind
● rpcbind.service - RPC bind service
Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
Active: active (running) since 一 2018-12-31 21:33:15 CST; 7s ago
Process: 5966 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
Main PID: 5967 (rpcbind)
CGroup: /system.slice/rpcbind.service
└─5967 /sbin/rpcbind -w

12月 31 21:33:15 centos-linux.shared systemd[1]: Starting RPC bind service...
12月 31 21:33:15 centos-linux.shared systemd[1]: Started RPC bind service.

# nfs
[ocloud@centos-linux ~]$ sudo systemctl start nfs
[ocloud@centos-linux ~]$ sudo systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
Active: active (exited) since 一 2018-12-31 21:41:20 CST; 3s ago
Process: 6128 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)
Process: 6126 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
Process: 6122 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
Process: 6167 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl restart gssproxy ; fi (code=exited, status=0/SUCCESS)
Process: 6151 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Process: 6149 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Main PID: 6151 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/nfs-server.service

12月 31 21:41:20 centos-linux.shared systemd[1]: Starting NFS server and services...
12月 31 21:41:20 centos-linux.shared systemd[1]: Started NFS server and services.

设置开机自启动

1
2
[ocloud@centos-linux ~]$ sudo systemctl enable rpcbind
[ocloud@centos-linux ~]$ sudo systemctl enable nfs

查看已注册的RPC列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[ocloud@centos-linux ~]$ rpcinfo -p
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 48527 status
100024 1 tcp 39724 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 43200 nlockmgr
100021 3 udp 43200 nlockmgr
100021 4 udp 43200 nlockmgr
100021 1 tcp 46330 nlockmgr
100021 3 tcp 46330 nlockmgr
100021 4 tcp 46330 nlockmgr

可以看到nfs相关rpc服务已注册

防火墙设置

如果需要外部主机进行远程访问,则必须将nfs服务开发的端口添加至防火墙

1
2
3
4
[ocloud@centos-linux ~]$ sudo firewall-cmd --permanent --zone=public --add-service=nfs
[ocloud@centos-linux ~]$ sudo firewall-cmd --permanent --zone=public --add-service=mountd
[ocloud@centos-linux ~]$ sudo firewall-cmd --permanent --zone=public --add-service=rpc-bind
[ocloud@centos-linux ~]$ sudo firewall-cmd --reload

客户端进行挂载

1
2
3
4
5
6
[ocloud@centos-linux ~]$ showmount -e 192.168.0.110            #查看可挂载
Export list for 192.168.0.110 :
/opt/share 192.168.0.0/24
[ocloud@centos-linux ~]$ mount -t nfs 192.168.0.110:/opt/share /mnt -o nolock #不提示即为成功
# nfs默认数据传输使用UDP,可以指定传输协议
mount -t nfs 192.168.0.110:/opt/share /mnt -o proto=tcp -o nolock

永久生效设置

1
2
3
4
5
6
7
8
9
[ocloud@centos-linux ~]$ vi /etc/fstab #添加如下的一行内容
...
192.168.0.110:/opt/share /mnt nfs defaults 0 0
...

#第五列
#当其值设置为1时,将允许dump备份程序备份;设置为0时,忽略备份操作
#第六列
#其值是一个顺序。当其值为0时,永远不检查;而 / 根目录分区永远都为1。其它分区从2开始,数字越小越先检查,如果两个分区的数字相同,则同时检查

参考

Network File System

Chapter 9. Network File System (NFS)

6.2. NFS

祝:所有的有理想、有梦想的程序员们,新的一年bug消消乐,码上生花,也希望社会更加宽容…等等

早日活出真实的自己

不如请我吃根冰棒吧