NFS 挂载 + autofs

NFS:Network File System

RPC:Remote Procedure Call

一、手动挂载  (mount -t nfs 服务端IP:/共享目录  /本地挂载点)

客户端

1.安装nfs:

[root@localhost ~]# yum install nfs* portmap -y

[root@localhost ~]# rpm -qa nfs*

nfs-utils-lib-1.1.5-11.el6.x86_64

nfs4-acl-tools-0.3.3-8.el6.x86_64

nfs-utils-lib-devel-1.1.5-11.el6.x86_64

nfs-utils-1.2.3-70.el6_8.1.x86_64

[root@localhost ~]# rpm -qa rpcbind

rpcbind-0.2.0-12.el6.x86_64

2.启动rpc服务和nfs:

[root@localhost ~]# /etc/init.d/nfs restart

[root@localhost ~]# /etc/init.d/nfs restart

查看rpc服务注册情况:

[root@localhost ~]# rpcinfo -p 192.168.190.138

   program vers proto   port  service

    100000    4   tcp    111  portmapper   (rpc服务默认端口:111)

    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  44957  status

    100024    1   tcp  53988  status

    100011    1   udp    875  rquotad

    100011    2   udp    875  rquotad

    100011    1   tcp    875  rquotad

    100011    2   tcp    875  rquotad

    100005    1   udp  53978  mountd

    100005    1   tcp  57642  mountd

    100005    2   udp  47255  mountd

    100005    2   tcp  54213  mountd

    100005    3   udp  55342  mountd

    100005    3   tcp  43895  mountd

    100003    2   tcp   2049  nfs  (nfs服务器默认端口:2049)

    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  42653  nlockmgr

    100021    3   udp  42653  nlockmgr

    100021    4   udp  42653  nlockmgr

    100021    1   tcp  57709  nlockmgr

    100021    3   tcp  57709  nlockmgr

    100021    4   tcp  57709  nlockmgr

3.编辑nfs主配置文件:

[root@localhost ~]# vim /etc/exports    (第一次使用nfs服务需手动创建)

/home/share *(rw,no_root_squash,sync)

【共享目录】 【主机名称,*是匹配所有】  【权限控制】

主机名称: 可使用IP、主机名或网段 192.168.0.1/24等同于192.168.0.1/255.255.255.0

权限控制: rw (可读写, ro(只读) sync(数据同步写入硬盘) async(暂存于内存,后写入硬盘)

           no_root_squash (root权限) root_squash(默认root权限会变成nfsnobody权限)

           all_squash(默认所有用户均匿名nofsnobody权限)  anonuid(设置UID) anongid(设置GID)

4.创建本地共享目录:

[root@localhost ~]# mkdir /home/share  # 创建共享目录

[root@localhost ~]# ll /home/share

total 0

5.重新加载配置文件使其生效:

[root@localhost ~]# exportfs -arv

exporting *:/home/share

此步骤等同于 重启nfs服务  service nfs restart

6.关闭防火墙或者开启相应的端口,否则客服端无法连接

[root@localhost ~]# service iptables stop

至此, Server端 配置结束

客户端:

1.确定启动rpcbind和nfs服务:

[root@localhost ~]# yum install nfs* -y

[root@localhost ~]# rpm -qa rpcbind

rpcbind-0.2.0-8.el6.i686

[root@localhost ~]# rpm -qa nfs*

nfs4-acl-tools-0.3.3-8.el6.i686

nfs-utils-lib-1.1.5-11.el6.i686

nfs-utils-lib-devel-1.1.5-11.el6.i686

nfs-utils-1.2.3-70.el6_8.1.i686

[root@localhost ~]# service rpcbind restart

[root@localhost ~]# service nfs restart

[root@localhost ~]# service nfs restart

2.查看NFS联机状态:

[root@localhost ~]# showmount -e 192.168.190.138

Export list for 192.168.190.138:

/home/share *

3.创建挂载点:

[root@localhost ~]# mkdir /bak

4. 使用mount命令挂载使用:

[root@localhost ~]# mount 192.168.190.138:/home/share /bak

5.测试:

客户端:

[root@localhost ~]# cd /bak

[root@localhost bak]# mkdir l

[root@localhost bak]# touch 123

[root@localhost bak]# ll

total 4

-rw-r--r--. 1 root root    0 Sep 30 15:20 123

drwxr-xr-x. 2 root root 4096 Sep 30 15:20 l

创建目录默认权限:755

创建文件默认权限:644

服务端:

[root@localhost ~]# cd /home/share

[root@localhost share]# ll

total 4

-rw-r--r--. 1 root root    0 Sep 30 15:20 123

drwxr-xr-x. 2 root root 4096 Sep 30 15:20 l

二、使用autofs自动挂载,默认5分钟取消挂载 (/etc/auto.master、 /etc/auto.misc)

1. 服务端:创建共享目录

[root@localhost ~]# mkdir /user

[root@localhost ~]# vim /etc/exports

/user *(rw,no_root_squash,sync)

[root@localhost user]# exportfs -arv

exporting *:/user

exporting *:/home/share

2. 客户端:创建挂载点

[root@localhost ~]# mkdir /var/autofs

[root@localhost ~]# yum install autofs -y

3.编辑 auto.master (主要是控制作用,指定默认目录及对应文件)

[root@localhost ~]# vim /etc/auto.master

/var/autofs /etc/auto.misc

[root@localhost ~]# cat /etc/auto.master | grep -v '#'

/misc /etc/auto.misc

/var/autofs /etc/auto.misc

/net -hosts

+auto.master

4.编辑 auto.misc    (主要是定义挂载点及指定下一级目录,该目录可以不存在)

[root@localhost ~]# vim /etc/auto.misc

test -rw,soft,intr 192.168.190.138:/user

注意:这里test 是下级目录,千万不要画蛇添足的加/ ,否则自动挂载不成功!!

[root@localhost ~]# cat /etc/auto.misc | grep -v '#'

cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

test -rw,soft,intr 192.168.190.138:/user

5.重启autofs服务:

[root@localhost ~]# /etc/init.d/autofs restart

6.测试:

[root@localhost autofs]# cd /var/autofs/test

[root@localhost test]# ls

mount

[root@localhost test]# touch 123

服务器端:

[root@localhost ~]# cd /user/

[root@localhost user]# ll

total 4

-rw-r--r--. 1 root root    0 Sep 30 15:57 123

drwxr-xr-x. 2 root root 4096 Sep 30 15:36 mount

autofs 服务 默认300s (5min) 会自动取消挂载 可参考文件 /etc/sysconfig/autofs 设置

[root@localhost ~]# cat /etc/sysconfig/autofs | grep 'TIMEOUT' | grep -v '#'

TIMEOUT=300 

三、 开机自动挂载,永久生效 (修改/etc/fstab)

开机自动挂载nfs服务  修改/etc/fstab 即可, 例如:

[root@localhost ~]# cat /etc/fstab|tail -1

192.168.190.138:/home/share  /bak                   ext3    defaults        0 0

总结:

客户端使用NFS服务,可以有3种方法挂载:

1. 手动挂载  (mount -t nfs 服务端IP:/共享目录  /本地挂载点)

2. 使用自动挂载,默认5分钟取消挂载 (/etc/auto.master、 /etc/auto.misc)

3. 开机自动挂载,永久生效 (修改/etc/fstab)

原文地址:https://www.cnblogs.com/mouseleo/p/9517261.html