zoukankan      html  css  js  c++  java
  • 一起来学linux:NFS服务器搭建

    nfsnetwork file system的缩写,作用在于让不同的网络,不同的机器,不同的操作系统可以共享彼此的文件。也就是讲远程服务器共享的目录挂载到本地的文件系统中。远程目录看起来就像是自己的一个磁盘分区

    NFS运行在SUNRPC(Remote Procedure Call, 远程过程调用)基础上, RPC定义了一种与系统无关的方法来实现进程间通信. 由此, NFS server也可以看作是RPC server.
    正因为NFS是一个RPC服务程序, 所以在使用它之前, 先要映射好端口——通过portmap设定. 比如: 某个NFS client发起NFS服务请求时, 它需要先得到一个端口(port). 所以它先通过portmap得到port number.


    下面 来看下服务器测的安装方式:

    1 首先nfs安装:apt-get install nfs-kernel-server

    2 安装完nfs-kernel-server后,系统会自动为我们安装好portmap(端口映射器)和nfs-common(客户端软件,用于测试)

    3 配置共享目录,在/etc/exports文件中添加如下命令:意思分别是

    /tmp:服务器需要共享的目录。

    192.168.0.11 允许能访问服务器的IP地址。这里可以是具体的地址也可以是IP网络号。比如192.168.1.1/24。 也可以采用通配符*,表示允许所有能访问到server的主机来连接共享目录

    rw: 共享权限。

    rw:客户端拥有读写权限(ro:代码只有读权限)
    sync
    :同步写入资料到内存和硬盘中
    no_subtree_check:
    不检测子目录权限
    root_squash:
    如果客户端使用root权限访问共享目录,则会被认为是匿名用户(权限缩小)
    no_root_squash:
    如果客户端使用root权限访问共享目录,权限依旧为root


    另外在这里我们还可以设定对共享目录文件访问的权限,这里需要用到setfacl命令。比如

    setfacl -m -u:zhf:rwx /tmp 就代表zhf的用户对这个目录下的文件具有rwx的权限


    4 配置完成后,重启nfs-server服务/etc/init.d/nfs-kernel-server restart。并执行showmount -e就可以看到下面的挂载点。

    root@zhf-virtual-machine:~# showmount -e

    /tmp 192.168.0.11(rw,no_root_squash)


    5 服务器测安装apt-get install nfs-common

    6挂载服务器共享目录到本地

    root@zhf-linux:/home# mount -t nfs 192.168.0.9:/tmp /home/nfs/public

    root@zhf-linux:/home# df

    Filesystem 1K-blocks Used Available Use% Mounted on

    udev 1010832 0 1010832 0% /dev

    tmpfs 205944 6528 199416 4% /run

    /dev/sda1 238171776 11020944 215029284 5% /

    tmpfs 1029712 30012 999700 3% /dev/shm

    tmpfs 5120 4 5116 1% /run/lock

    tmpfs 1029712 0 1029712 0% /sys/fs/cgroup

    cgmfs 100 0 100 0% /run/cgmanager/fs

    tmpfs 205944 56 205888 1% /run/user/1000

    192.168.0.9:/tmp 19477248 1026432 17438464 6% /home/nfs/public


    7 进入到/home/nfs/public就可以看到在192.168.0.9当中共享的文件

    root@zhf-linux:/home/nfs/public# ls -al

    total 72

    drwxrwxrwt 17 root root 4096 Oct 30 21:36 .

    drwxr-xr-x 3 root root 4096 Oct 30 21:39 ..

    -rw------- 1 zhf zhf 0 Oct 30 21:08 config-err-C8mCtt

    drwxrwxrwt 2 root root 4096 Oct 30 21:06 .font-unix

    drwxrwxrwt 2 root root 4096 Oct 30 21:08 .ICE-unix

    drwx------ 3 root root 4096 Oct 30 20:57 systemd-private-3b1f89764b29474681718903030213e2-colord.service-GtPb2r

    drwx------ 3 root root 4096 Oct 30 21:07 systemd-private-3b1f89764b29474681718903030213e2-rtkit-daemon.service-eMi74n

    drwx------ 3 root root 4096 Oct 30 21:06 systemd-private-3b1f89764b29474681718903030213e2-systemd-timesyncd.service-HMSDuI

    drwxrwxrwt 2 root root 4096 Oct 30 21:06 .Test-unix

    drwx------ 9 root root 4096 Oct 30 21:09 ubuntu-release-upgrader-p9q1b5zo

    -rw-rw-r-- 1 zhf zhf 0 Oct 30 21:08 unity_support_test.0

    drwxr-xr-x 2 root root 4096 Oct 30 21:09 upgrade-chroot-ano169rw

    drwx------ 2 root root 4096 Oct 30 21:09 upgrade-rw-699p254l

    drwxrwxrwt 2 root root 4096 Oct 30 21:07 VMwareDnD

    drwxr-xr-x 2 root root 4096 Oct 30 20:57 vmware-root

    drwx------ 2 root root 4096 Oct 30 21:07 vmware-root-2689209388

    drwx------ 2 zhf zhf 4096 Oct 30 21:08 vmware-zhf

    -r--r--r-- 1 root root 11 Oct 30 20:57 .X0-lock

    drwxrwxrwt 2 root root 4096 Oct 30 20:57 .X11-unix

    drwxrwxrwt 2 root root 4096 Oct 30 21:06 .XIM-unix


    前面讲了如何进行nfs的配置和使用。但是有一个问题就是每次登录系统后都需要手动利用mount命令来挂载一次。我们可不可以做到当客户端在有使用NFS文件系统的需求时才让系统自动挂载呢?那就要用到autofs。举例来说,当客户端要时候用/home/nfsfile/public的数据时,此时autofs才会去192.168.0.9上挂载/tmp目录。当隔了5分钟使用该目录下的数据后,则客户端系统将会主动卸载/home/nfsfile/public. autofs的配置很简单。下面来看下步骤:

    1 建立主配置文件/etc/auto.master.设置如下:这个的意思是当要使用/home/nfsfile的数据时,会去读取/etc/auto.nfs的数据进行自动挂载

    /home/nfsfile /etc/auto.nfs

    2 建立数据对应文件内/etc/auto.nfs的挂载信息与服务器对应的资源。下面的这个意思是当要使用/home/nfsfile/public的数据时,去自动挂载192.168.0.9/tmp资源

    public -rw,bg,rsize=32768,wsize=32768 192.168.0.9:/tmp


    3 /home/nfsfile/public去实际查看的时候发现是自动挂载的

    root@zhf-linux:/home/nfsfile/public# mount | grep nfsfile

    /etc/auto.nfs on /home/nfsfile type autofs (rw,relatime,fd=6,pgrp=5258,timeout=300,minproto=5,maxproto=5,indirect)

    192.168.0.9:/tmp on /home/nfsfile/public type nfs4 (rw,relatime,vers=4.0,rsize=32768,wsize=32768,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.0.11,local_lock=none,addr=192.168.0.9)


    4 文件的挂载也出现了

    root@zhf-linux:/home/nfsfile/public# df /home/nfsfile/public

    Filesystem 1K-blocks Used Available Use% Mounted on

    192.168.0.9:/tmp 19477184 1018848 17445888 6% /home/nfsfile/public

  • 相关阅读:
    shutil的一些基本用法
    python用random产生验证码,以及random的一些其他用法
    hashlib 文件校验,MD5动态加盐返回加密后字符
    python中logging日志基本用法,和进程安全问题
    用递归函数得到目录下的所有文件
    python装饰器
    列表推导式三种模式和生成器
    vue.js响应式原理解析与实现
    Promise详解
    三分钟教你同步 Visual Studio Code 设置
  • 原文地址:https://www.cnblogs.com/zhanghongfeng/p/7764203.html
Copyright © 2011-2022 走看看