欢迎光临
我们一直在努力

NFS介绍与基本安装

1NFS介绍

NFS是Network File System的缩写(分布式文件系统协议),==它最大的功能就是可以透过网络,让不同的机器、不同的操作系统、可以彼此分享个别的档案 (share files)==
NFS最早由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版本
NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。

《客户端NFS和服务端NFS通讯过程》
1)首先服务器端启动RPC服务,并开启111端口
2)启动NFS服务,并向RPC注册端口信息
3)客户端启动RPC(portmap服务),向服务端的RPC(portmap)服务请求服务端的NFS端口
4)服务端的RPC(portmap)服务反馈NFS端口信息给客户端。
5)客户端通过获取的NFS端口来建立和服务端的NFS连接并进行数据的传输。

(注:rpcbind是centos6及以后称之为rpcbind,portmap是centos5及以前的版本)

场景1:有3台机器A,B,C,需求是这3台机器的数据是一样的,当A的机器负载高时客户想要访问A服务器上的数据时,这时有2台机器B,C的负载不是很高,将A与B,C的数据共享,可是怎么样能时实的更新B,与C的机器上呢。
此时用NFS服务即可实现,时实的更新的数据。(NFS不监听端口,portmap监听111端口)

2nfs服务端安装配置

服务端的配置:

[root@chy01 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.212.11  netmask 255.255.255.0  broadcast 192.168.212.255
        inet6 fe80::243b:ddac:7a2b:a5b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:13:b3:3b  txqueuelen 1000  (Ethernet)
        RX packets 5269  bytes 2572740 (2.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1167  bytes 157323 (153.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
(服务端的ip地址)
[root@chy01 ~]# yum install -y nfs-utils rpcbind
[root@chy01 ~]# vim /etc/exports
(nfs的配置文件)
/home/nfstestdir 192.168.212.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
(/home/nfstestdir 需要共享的目录,这个目录后续需要创建,192.168.212.0/24(给192.168.212.0这个网络段共享目录。(rw,sync,all_squash,anonuid=1000,anongid=1000)如此是
[root@chy01 ~]# mkdir /home/nfstestdir
[root@chy01 ~]# chmod 777 /home/nfstestdir
(创建目录并且给777的权限)
[root@chy01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1275/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2240/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1744/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1275/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2240/master       
(可以查看到rpc服务已经启动)
[root@chy01 ~]# ps aux |grep nfs
root       3502  0.0  0.0 112664   976 pts/0    S+   07:06   0:00 grep --color=auto nfs
(虽然rpc已经启动,但是nfs服务并没有启动)
[root@chy01 ~]# systemctl start nfs
(手动启动nfs)
[root@chy01 ~]# ps aux |grep nfs
root       3550  0.0  0.0      0     0 ?        S<   07:07   0:00 [nfsd4_callbacks]
root       3556  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3557  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3558  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3559  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3560  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3561  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3562  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3563  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3568  0.0  0.0 112664   976 pts/0    S+   07:07   0:00 grep --color=auto nfs
(查看nfs服务)
[root@chy01 ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
(nfs开机启动)

客户端简单配置:

[root@chy ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.212.10  netmask 255.255.255.0  broadcast 192.168.212.255
        inet6 fe80::338e:589c:fa07:65e5  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:70:77:62  txqueuelen 1000  (Ethernet)
        RX packets 8880  bytes 8055224 (7.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1205  bytes 139708 (136.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
(客户端的IP地址)
[root@chy ~]# yum install -y nfs-utils rpcbind
(同样需要安装nfs-utils包)

3nfs配置选项

nfs配置选项

[root@chy01 ~]# cat /etc/exports
/home/nfstestdir 192.168.212.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
rw 读写
 ro 只读
 sync 同步模式,内存数据实时写入磁盘
 async 非同步模式
 no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
 root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
 all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
 anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

挂载客户端

在此就不需要在安装nfs,因为之前就有安装nfs安装包(需要关闭防火墙)
[root@chy ~]# showmount -e 192.168.212.11
Export list for 192.168.212.11:
/home/nfstestdir 192.168.212.0/24
(可以看到共享的目录/home/nfstestdir)
[root@chy ~]# mount -t nfs 192.168.212.11:/home/nfstestdir /mnt/ (挂载时需要加-t
[root@chy ~]# df -h 
文件系统                         容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root              8.8G  5.4G  2.9G   66% /
devtmpfs                         737M     0  737M    0% /dev
tmpfs748M     0  748M    0% /dev/shm
tmpfs748M  8.6M  739M    2% /run
tmpfs748M     0  748M    0% /sys/fs/cgroup
/dev/sda1                        190M  107M   70M   61% /boot
/dev/mapper/cl-var               8.4G  281M  7.7G    4% /var
/dev/mapper/cl-home              497M   26M  472M    6% /home
tmpfs150M     0  150M    0% /run/user/0
192.168.212.11:/home/nfstestdir  497M   26M  472M    6% /mnt
[root@chy mnt]# touch chy.111
[root@chy mnt]# ls -l 
总用量 0
-rw-r--r-- 1 mysql mysql 0 8月  24 08:06 chy.111
(在客户端创建一个chy.111)
[root@chy mnt]# id mysql
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

在服务端测试

[root@chy01 ~]# ls -l /home/nfstestdir/
总用量 0
-rw-r--r-- 1 mysql mysql 0 8月  24 08:06 chy.111
[root@chy01 ~]# id mysql
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

也许有个疑问?为什么授权用户和组是mysql呢?

[root@chy01 ~]# cat /etc/exports
/home/nfstestdir 192.168.212.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
我们看到,我们设置的权限为anonuid=1000,anongid=1000,那么1000就是mysql
赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。