• Linux 搭建NFS文件服务器实现文件共享


    我们接着玩Linux,O(∩_∩)O哈哈~

    1.什么是nfs

    NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。

    2.搭建nfs

    首先你得有一个Linux服务器哈,这里我还是用我的廉价腾讯云~

    2.1前期准备:

    1.安装nfs-utils和rpcbind 

    yum install nfs-utils rpcbind

    2.设置开机启动服务 

    chkconfig nfs on 
    chkconfig rpcbind on

    3.启动相关服务 

    service rpcbind start 
    service nfs start

    2.2服务端配置:

    1.创建共享目录 

    mkdir /xjy/data/nfs/server 

    2.编辑/etc/exports文件添加如下内容 

    vim /etc/exports 
    
    /xjy/data/nfs/server *(rw,no_root_squash,no_all_squash,sync)

    ① /xjy/data/nfs/server —要共享的目录 

    ② * 处代表限制访问的ip段 

    可以填192.168.0.* —允许访问的网段,也可以是ip地址、主机名(能够被服务器解析) 
    可以填192.168.0.123/24 
    *(所有人都能访问)

    ③ 括号内的常见参数有: 

    参数值 内容说明 
    rw  ro 该目录分享的权限是可擦写 (read-write) 或只读 (read-only),但最终能不能读写,还是与文件系统的 rwx 及身份有关。 
    sync  async sync 代表数据会同步写入到内存与硬盘中,async 则代表数据会先暂存于内存当中,而非直接写入硬盘! 
    no_root_squash  root_squash 客户端使用 NFS 文件系统的账号若为 root 时,系统该如何判断这个账号的身份?预设的情况下,客户端 root 的身份会由 root_squash 的设定压缩成 nfsnobody, 如此对服务器的系统会较有保障。但如果你想要开放客户端使用 root 身份来操作服务器的文件系统,那么这里就得要开 no_root_squash 才行! 
    all_squash 不论登入 NFS 的使用者身份为何, 他的身份都会被压缩成为匿名用户,通常也就是 nobody(nfsnobody) 啦! 
    no_subtree_check 关闭子树检查 
    anonuid  anongid anon 意指 anonymous (匿名者) 前面关于 *_squash 提到的匿名用户的 UID 设定值,通常为 nobody(nfsnobody),但是你可以自行设定这个 UID 的值!当然,这个 UID 必需要存在于你的 /etc/passwd 当中! anonuid 指的是 UID 而 anongid 则是群组的 GID 啰。 
    其他选项可以通过man exports查阅man文档

    3.刷新配置立即生效 

    exportfs -r

    4.重启nfs 

    service nfs restart

           5.查看 RPC 服务的注册状况 

    [root@VM_15_8_centos server]# rpcinfo -p localhost 
    program vers proto port service 
    4 tcp 111 portmapper 
    3 tcp 111 portmapper 
    2 tcp 111 portmapper 
    4 udp 111 portmapper 
    3 udp 111 portmapper 
    2 udp 111 portmapper 
    1 udp 49979 mountd 
    1 tcp 58393 mountd 
    2 udp 45516 mountd 
    2 tcp 37792 mountd 
    3 udp 32997 mountd 
    3 tcp 39937 mountd 
    2 tcp 2049 nfs 
    3 tcp 2049 nfs 
    4 tcp 2049 nfs 
    2 tcp 2049 nfs_acl 
    3 tcp 2049 nfs_acl 
    2 udp 2049 nfs 
    3 udp 2049 nfs 
    4 udp 2049 nfs 
    2 udp 2049 nfs_acl 
    3 udp 2049 nfs_acl 
    1 udp 51112 nlockmgr 
    3 udp 51112 nlockmgr 
    4 udp 51112 nlockmgr 
    1 tcp 43271 nlockmgr 
    3 tcp 43271 nlockmgr 
    4 tcp 43271 nlockmgr 

     选项与参数: 

    -p :针对某 IP (未写则预设为本机) 显示出所有的 port 与 porgram 的信息; 
    -t :针对某主机的某支程序检查其 TCP 封包所在的软件版本; 
    -u :针对某主机的某支程序检查其 UDP 封包所在的软件版本;

    6.本机查询nfs服务器 

    [root@VM_15_8_centos server]# showmount -e localhost 
    Export list for localhost: 
    
    /xjy/data/nfs/server *

    选项与参数: 
    -a :显示目前主机与客户端的 NFS 联机分享的状态; 
    -e :显示某部主机的 /etc/exports 所分享的目录数据。

    2.3客户端配置:

    1.创建挂载点 

    mkdir /xjy/data/nfs/client 

    2.查看服务器抛出的共享目录信息 

    [root@VM_15_8_centos server]# showmount -e 192.168.0.123 
    Export list for 192.168.0.123: 
    /xjy/data/nfs/server *

    3.挂载目录 
    为了提高NFS的稳定性,使用TCP协议挂载,NFS默认用UDP协议 

    mount -t nfs -o vers=3 192.168.0.123:/xjy/data/nfs/server /xjy/data/nfs/client -o proto=tcp -o nolock 

    4.查看挂载的目录 

    [root@VM_15_8_centos server]# df -h 
    Filesystem Size Used Avail Use% Mounted on 
    /dev/mapper/VolGroup-lv_root 
    18G 1.1G 16G 7% / 
    tmpfs 112M 0 112M 0% /dev/shm 
    /dev/sda1 477M 54M 398M 12% /boot 
    192.168.0.123:/data/lys 
    18G 1.1G 16G 7% /lys

    3.测试:

    服务端 
    [root@VM_15_8_centos xjy]# echo “test” > test.txt 
    客户端 
    [root@VM_15_8_centos xjy]# cat /xjy/test.txt 
    test 
    [root@VM_15_8_centos xjy]# echo204” >> /xjy/test.txt 
    服务端 
    [root@VM_15_8_centos xjy]# cat /data/xjy/test.txt 
    test 
    204

    4.取消挂载:

    [root@VM_15_8_centos server]# umount /xjy/data/nfs/client 
    [root@VM_15_8_centos server]# df -h 
    Filesystem Size Used Avail Use% Mounted on 
    /dev/mapper/VolGroup-lv_root 
    18G 1.1G 16G 7% / 
    tmpfs 112M 0 112M 0% /dev/shm 
    /dev/sda1 477M 54M 398M 12% /boot
    
    如果显示:device is busy,可以使用-lf参数强行删除 
    [root@VM_15_8_centos server]# umount –lf /xjy/data/nfs/client

    5.重启和关闭:

    重启:[root@VM_15_8_centos server]# service 服务名 restart 
    停止:[root@VM_15_8_centos server]# service 服务名 stop 
    开启:[root@VM_15_8_centos server]# service 服务名 start

    6.固定nfs服务端口

    为了方便配置防火墙,需要固定nfs服务端口 
    NFS启动时会随机启动多个端口并向RPC注册,这样如果使用iptables对NFS端口进行限制就会有点麻烦,可以更改配置文件固定NFS服务相关端口。

    [root@VM_15_8_centos server]# 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 
    100005 1 udp 49979 mountd 
    100005 1 tcp 58393 mountd 
    100005 2 udp 45516 mountd 
    100005 2 tcp 37792 mountd 
    100005 3 udp 32997 mountd 
    100005 3 tcp 39937 mountd 
    100003 2 tcp 2049 nfs 
    100003 3 tcp 2049 nfs 
    100003 4 tcp 2049 nfs 
    100227 2 tcp 2049 nfs_acl 
    100227 3 tcp 2049 nfs_acl 
    100003 2 udp 2049 nfs 
    100003 3 udp 2049 nfs 
    100003 4 udp 2049 nfs 
    100227 2 udp 2049 nfs_acl 
    100227 3 udp 2049 nfs_acl 
    100021 1 udp 51112 nlockmgr 
    100021 3 udp 51112 nlockmgr 
    100021 4 udp 51112 nlockmgr 
    100021 1 tcp 43271 nlockmgr 
    100021 3 tcp 43271 nlockmgr 
    100021 4 tcp 43271 nlockmgr

    分配端口,编辑配置文件: 

    [root@VM_15_8_centos server]# vim /etc/sysconfig/nfs 
    添加: 
    RQUOTAD_PORT=30001 
    LOCKD_TCPPORT=30002 
    LOCKD_UDPPORT=30002 
    MOUNTD_PORT=30003 
    STATD_PORT=30004 


    重启

    [root@VM_15_8_centos server]# service nfs restart 
    关闭 NFS 守护进程: [确定] 
    关闭 NFS mountd: [确定] 
    关闭 NFS 服务: [确定] 
    Shutting down RPC idmapd: [确定] 
    启动 NFS 服务: [确定] 
    启动 NFS mountd: [确定] 
    启动 NFS 守护进程: [确定] 
    正在启动 RPC idmapd: [确定]

    查看结果

    [root@VM_15_8_centos server]# 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 
    100005 1 udp 30003 mountd 
    100005 1 tcp 30003 mountd 
    100005 2 udp 30003 mountd 
    100005 2 tcp 30003 mountd 
    100005 3 udp 30003 mountd 
    100005 3 tcp 30003 mountd 
    100003 2 tcp 2049 nfs 
    100003 3 tcp 2049 nfs 
    100003 4 tcp 2049 nfs 
    100227 2 tcp 2049 nfs_acl 
    100227 3 tcp 2049 nfs_acl 
    100003 2 udp 2049 nfs 
    100003 3 udp 2049 nfs 
    100003 4 udp 2049 nfs 
    100227 2 udp 2049 nfs_acl 
    100227 3 udp 2049 nfs_acl 
    100021 1 udp 30002 nlockmgr 
    100021 3 udp 30002 nlockmgr 
    100021 4 udp 30002 nlockmgr 
    100021 1 tcp 30002 nlockmgr 
    100021 3 tcp 30002 nlockmgr 
    100021 4 tcp 30002 nlockmgr

    可以看到,随机端口以固定 
    iptables策略问题完美解决!!!

    7.设置系统开机就挂载磁盘

    在/etc/fstab中加入开机挂载命令 

    [root@VM_15_8_centos server]# vim /etc/fstab 
    10.10.159.68:/s4_attachment /data/nfs/client nfs defaults 0 0

    8.Windows作为客户端挂载nfs

    1.开启nfs客户端:打开控制面板》程序》启用或关闭windos功能》选中nfs客户端,点击确定 
    .2.挂载:mount 192.168.0.123:/xjy/data/nfs X:   (此处注意只能定位到nfs目录,否则会报网络错误) 

    会创建一个网络盘X盘,里面挂载文件 
    3.取消挂载:右键X盘,点击取消连接或者执行命令:umount X:

    OVER!

  • 相关阅读:
    django之验证码
    无法显示SQL Server Management Studio Express解决办法
    程序员常用不常见很难得的地址大全转
    调用ip138的页面获取IP地址
    VS 2010无法创建项目提示写入项目文件时出错 没有注册类别
    webapi token、参数签名是如何生成的(转载)
    尝试asp.net mvc 基于controller action 方式权限控制方案可行性(转载)
    委托学习
    Webapi上传数据(XML)敏感字符解决方案
    redis 学习
  • 原文地址:https://www.cnblogs.com/xujingyang/p/9209549.html
Copyright © 2020-2023  润新知