NFS服务配置.

 NFS:network file system

一、NFS服务介绍

NFS 是 Network FileSystem 的缩写,顾名思义就是网络文件存储系统,它最早是由 Sun 公司发展出来的,也是 FreeBSD 支持的文件系统中的一个,它允许网络中的计算机之间通过 TCP/IP 网络共享资源。通过 NFS,我们本地 NFS 的客户端应用可以透明地读写位于服务端 NFS 服务器上的文件,就像访问本地文件一样方便。简单的理解,NFS 就是可以透过网络,让不同的主机、不同的操作系统可以共享存储的服务。

NFS 在文件传送或信息传送过程中依赖于 RPC(Remote Procedure Call) 协议,即远程过程调用, NFS 的各项功能都必须要向 RPC 来注册,如此一来 RPC 才能了解 NFS 这个服务的各项功能 Port、PID、NFS 在服务器所监听的 IP 等,而客户端才能够透过 RPC 的询问找到正确对应的端口,所以,NFS 必须要有 RPC 存在时才能成功的提供服务,简单的理解二者关系:NFS是 一个文件存储系统,而 RPC 是负责信息的传输。


协议:rpc:远程过程调用

启用的端口

[root@localhost ~]# netstat -tpln | grep 111
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      54127/rpcbind       
tcp6       0      0 :::111                  :::*                    LISTEN      54127/rpcbind

配置文件:

/etc/exports (空文件,全部手写)

配置文件分成左右2部分,左边是共享目录名(共享资源),右边为选项(选项很多) 

[root@localhost ~]# cat /etc/exports
/nfs 192.168.22.0/24(rw,sync,insecure,no_subtree_check,no_root_squash)

 

二、安装服务

#服务端

yum install -y nfs-utils rpcbind

#客服端

yum install -y nfs-utils

 

三、配置NFS

我们在服务端创建共享目录,作为客服端挂载的远程入口,并设置权限

[root@localhost ~]# mkdir -p /nfs
[root@localhost ~]# chmod 777 /nfs

修改配置文件

[root@localhost ~]# vim /etc/exports
/nfs 192.168.22.0/24(rw,sync,insecure,no_subtree_check,no_root_squash)

四、启动服务

先启动RPC服务

[root@localhost ~]# systemctl start rpcbind

查看 NFS 服务项 rpc 服务器注册的端口列表

[root@localhost ~]# rpcinfo -p localhost 
   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

 

后启动NFS服务

[root@localhost ~]# systemctl start nfs

启动 NFS 服务后 rpc 服务已经启用了对 NFS 的端口映射列表

[root@localhost ~]# rpcinfo -p localhost
   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  33745  status
    100024    1   tcp  36980  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  38960  nlockmgr
    100021    3   udp  38960  nlockmgr
    100021    4   udp  38960  nlockmgr
    100021    1   tcp  38362  nlockmgr
    100021    3   tcp  38362  nlockmgr
    100021    4   tcp  38362  nlockmgr

看下是否正确加载了设置的 /etc/exports 配置。

[root@localhost ~]# showmount -e localhost
Export list for localhost:
/nfs 192.168.22.0/24
[root@localhost ~]# exportfs -v
/nfs 192.168.22.0/24(rw,wdelay,insecure,no_root_squash,no_subtree_check,sec=sys,rw,insecure,no_root_squash,no_all_squash)

 

客户端NFS测试

[root@localhost ~]# showmount -e 192.168.22.22
Export list for 192.168.22.22:
/nfs 192.168.22.0/24

 五、防火墙设置(三选一)firewall-cmd --reload

1、关闭防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld

2、配置端口防火墙

111/tcp 111/udp 926/tcp 926/udp 2049/tcp 2049/udp 20048/tcp 20048/udp

[root@localhost ~]# firewall-cmd --add-port=111/tcp --permanent
[root@localhost ~]# firewall-cmd --add-port=111/udp --permanent
[root@localhost ~]# firewall-cmd --add-port=926/tcp --permanent
[root@localhost ~]# firewall-cmd --add-port=926/udp --permanent
[root@localhost ~]# firewall-cmd --add-port=2049/tcp --permanent
[root@localhost ~]# firewall-cmd --add-port=2049/udp --permanent
[root@localhost ~]# firewall-cmd --add-port=20048/tcp --permanent
[root@localhost ~]# firewall-cmd --add-port=20048/udp --permanent

3、配置服务防火墙

firewall-cmd --add-service=nfs --permanent
firewall-cmd --add-service=mountd --permanent
firewall-cmd --add-service=rpc-bind --permanent

 

六、挂载

linux:

[root@localhost ~]# mount -t nfs 192.168.22.22:/nfs /mnt/nfs/
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
192.168.22.22:/nfs        25G  4.1G   21G  17% /mnt/nfs

 

Windows:

C:\Users\Administrator>mount 192.168.22.22:/nfs H:
H: 现已成功连接到 192.168.22.22:/nfs

命令已成功完成。

七、附录

参数 说明
ro 只读访问
rw 读写访问
sync 所有数据在请求时写入共享(同步)
async nfs 在写入数据前可以响应请求
secure nfs 通过 1024 以下的安全 TCP/IP 端口发送
insecure nfs 通过 1024 以上的端口发送
wdelay 如果多个用户要写入 nfs 目录,则归组写入(默认)
no_wdelay 如果多个用户要写入 nfs 目录,则立即写入,当使用 async 时,无需此设置
hide 在 nfs 共享目录中不共享其子目录
no_hide 共享 nfs 目录的子目录
subtree_check 如果共享 /usr/bin 之类的子目录时,强制 nfs 检查父目录的权限(默认)
no_subtree_check 不检查父目录权限
all_squash 共享文件的 UID 和 GID 映射匿名用户 anonymous,适合公用目录
no_all_squash 保留共享文件的 UID 和 GID(默认)
root_squash root 用户的所有请求映射成如 anonymous 用户一样的权限(默认)
no_root_squash root 用户具有根目录的完全管理访问权限
anonuid=xxx 指定 nfs 服务器 /etc/passwd 文件中匿名用户的 UID
anongid=xxx 指定 nfs 服务器 /etc/passwd 文件中匿名用户的 GID
posted @ 2019-06-22 22:19  kklinux  阅读(901)  评论(0编辑  收藏  举报