linux 两台服务器共享目录NFS实现

一、环境信息

服务器1:10.10.82.73        文件夹:/home/fileroot

服务器2:10.10.82.72       文件夹:/home/fileroot

将服务器1的/home/fileroot文件夹共享到服务器2的/home/fileroot文件夹。

要求两个服务器都安装有NFS服务。

yum -y install nfs*

二、服务器1设置

1、修改exports文件

vi /etc/exports

一般为空文件,添加以下内容

/home/fileroot *(insecure,rw,sync,no_root_squash)

*代表运行所有机器访问,或者指定10.10.82.72 访问

/home/fileroot 服务器1上要共享的文件夹

():内添加具体的参数,可添加的参数如下:

    ro:该主机对该共享目录有只读权限
    rw:该主机对该共享目录有读写权限
    root_squash:客户机用root用户访问该共享文件夹时,将root用户映射成匿名用户 ;
    no_root_squash:NFS服务器共享目录用户的属性,客户机用root访问该共享文件夹时,不映射root用户;
    all_squash 客户机上的任何用户访问该共享目录时都映射成匿名用户
    anonuid 将客户机上的用户映射成指定的本地用户ID的用户
    anongid 将客户机上的用户映射成属于指定的本地用户组ID
    sync 资料同步写入到内存与硬盘中
    async 资料会先暂存于内存中,而非直接写入硬盘
    insecure 允许从这台机器过来的非授权访问

2、设置NFS自启动

chkconfig nfs on
    service nfs start

 #CentOS7:
    systemctl start nfs
    systemctl enable nfs

三、服务器2设置

1、设置NFS自启动

chkconfig nfs on
    service nfs start

 #CentOS7:
    systemctl start nfs
    systemctl enable nfs

如遇到报错

rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)

 解决办法:

1 #service rpcbind restart

2 #service nfs start

2、执行挂载语句

mount -t nfs 10.10.82.73:/home/fileroot/  /home/fileroot/

3、查看挂载结果

df -h

4、编辑启动文件,将挂载命令设置为启动自动执行

vi /etc/rc.local

添加之前执行的挂载语句:

mount -t nfs 10.10.82.73:/home/fileroot/  /home/fileroot/

【注意】

CentOS7开始,默认情况下开机是不执行/etc/rc.local文件的,需要授予执行权限,才能开机启动执行此文件

chmod 777 /etc/rc.local

四、其他命令

1、服务器2取消挂载(取消挂载时,不能处于挂载目录内):

umount /home/fileroot

2、服务器1查看以共享的目录

showmount -e

3、服务器2查看服务器1共享目的目录

showmount -e 10.10.82.73

原文地址:https://www.cnblogs.com/liuzhenguo/p/14078558.html