linux 服务器之间共享目录



一.linux挂载windows

1.windows目录打开共享;

2.mount -t cifs -o username=admin***tor,password=abc //192.168.***.***/目录 /home/sharestore/windows/

二.linux挂载linux

1.检查是否安装以下服务:

rpm -qa | grep nfs-utils

rpm -qa | grep rpcbind

2.若没有则安装:

yum -y install nfs-utils
    yum -y install rpcbind

3.配置nfs访问目录/etc/exports,默认为空,添加一行(开放目录权限给指定主机):

/test 192.168.***.102(rw,no_root_squash,no_all_squash,async)

刷新配置:

exportfs -a

4.启动以下服务:

systemctl start rpcbind.service
    systemctl start nfs.service

5.服务器创建挂载点:

mkdir /mnt/test102

6.挂载nfs:

mount -t nfs 192.168.***.101:/test /home/test102

三.卸载

umount /home/test102



NFS服务的简介及常见故障解决方法

1、NFS基本介绍
(1)NFS简介
NFS 是Network File System的缩写,即网络文件系统。一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布。功能是让客户端通过网络访问不同主机上磁盘里的数据,主要用在类Unix系统上实现文件共享的一种方法。NFS在文件传送或信息传送过程中依赖于RPC协议。

blob.png

(2)NFS服务需要安装的软件
nfs-utils-* :包括基本的NFS命令与监控程序

rpcbind-* :支持安全NFS RPC服务的连接

注:通常情况下,是作为系统的默认包安装的

CentOS6.*之前rpcbind叫portmap。

(3)NFS系统守护进程
nfsd:它是基本的NFS守护进程,主要功能是管理客户端是否能够登录服务器

mountd:它是RPC安装守护进程,主要功能是管理NFS的文件系统。当客户端顺利通过nfsd登录NFS服务器后,在使用NFS服务所提供的文件前,还必须通过文件使用权限的验证。它会读取NFS的配置文件/etc/exports来对比客户端权限。

rpcbind:主要功能是进行端口映射工作。当客户端尝试连接并使用RPC服务器提供的服务(如NFS服务)时,rpcbind会将所管理的与服务对应的端口提供给客户端,从而使客户可以通过该端口向服务器请求服务。

(4)NFS的常用目录

/etc/exports NFS服务的主要配置文件

/usr/sbin/exportfs NFS服务的管理命令

/usr/sbin/showmount 客户端的查看命令

/var/lib/nfs/etab 记录NFS分享出来的目录的完整权限设定值

/var/lib/nfs/xtab 记录曾经登录过的客户端信息

NFS服务的配置文件为 /etc/exports,这个文件是NFS的主要配置文件,不过系统并没有默认值,所以这个文件不一定会存在,可能要使用vim手动建立,然后在文件里面写入配置内容。

(5)/etc/exports文件内容格式
<输出目录> [客户端1 选项(访问权限,用户映射,其他)] [客户端2 选项(访问权限,用户映射,其他)]

a. 输出目录:输出目录是指NFS系统中需要共享给客户机使用的目录

b. 客户端:客户端是指网络中可以访问这个NFS输出目录的计算机

客户端常用的指定方式:

指定ip地址的主机:192.168.0.200

指定子网中的所有主机:192.168.0.0/24 192.168.0.0/255.255.255.0

指定域名的主机:david.bsmart.cn

指定域中的所有主机:*.bsmart.cn

所有主机:*

c. 选项:选项用来设置输出目录的访问权限、用户映射等。

NFS主要有3类选项:

访问权限选项:

设置输出目录只读:ro

设置输出目录读写:rw

用户映射选项:

all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);

no_all_squash:与all_squash取反(默认设置);

root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置);

no_root_squash:与rootsquash取反;

anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);

anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);

其它选项

secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置);

insecure:允许客户端从大于1024的tcp/ip端口连接服务器;

sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;

async:将数据先保存在内存缓冲区中,必要时才写入磁盘;

wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置);

no_wdelay:若有写操作则立即执行,应与sync配合使用;

subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);

no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;

2、配置NFS服务端
(1)关闭系统防火墙和selinux
[root@localhost ~]# systemctl stop firewalld 关闭防火墙

[root@localhost ~]# systemctl status firewalld 查看防火墙状态

firewalld.service - firewalld - dynamic firewall daemon

Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

Active: inactive (dead)

 Docs: man:firewalld(1)

[root@localhost ~]# sed -i “7s/enforcing/disabled/” /etc/selinux/config

[root@localhost ~]# setenforce 0

(2)安装需要的软件
[root@localhost ~]# yum -y install rpc-bind nfs-utils

(3)修改/etc/exports文件,定义NFS共享
将NFS服务器的/zhangsan共享给192.168.115.0/24网段,rw权限

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

/zhangsan 192.168.115.0/24(rw)

(4)创建/zhangsan这个共享目录并设置权限
[root@localhost ~]# mkdir /zhangsan

[root@localhost ~]# chmod 777 /zhangsan

(5)启动NFS服务并设置为开机自启动
[root@localhost ~]# systemctl enable nfs

Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

[root@localhost ~]# systemctl start nfs

[root@localhost ~]# systemctl enable rpcbind

[root@localhost ~]# systemctl start rpcbind

[root@localhost ~]# systemctl status nfs 查看NFS服务的状态

nfs-server.service - NFS server and services

Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)

Drop-In: /run/systemd/generator/nfs-server.service.d

       └─order-with-mounts.conf

Active: active (exited) since 日 2018-02-25 13:47:29 CST; 52s ago

Main PID: 2406 (code=exited, status=0/SUCCESS)

CGroup: /system.slice/nfs-server.service

(6)查看本机共享的文件或目录
[root@localhost ~]# exportfs

/zhangsan 192.168.115.0/24

3、客户端配置
(1)客户端关闭防火墙和selinux,方法同上。

(2)客户端安装NFS软件包。
[root@localhost ~]# yum -y install nfs-utils

(3)客户端查看nfs共享状态
[root@localhost ~]# showmount -e 192.168.115.120

Export list for 192.168.115.120:

/zhangsan 192.168.115.0/24

(4)客户端挂载nfs服务器共享目录
[root@localhost ~]# mount 192.168.115.120:/zhangsan /media

[root@localhost ~]# df -h

文件系统 容量 已用 可用 已用% 挂载点

/dev/mapper/cl-root 17G 1.2G 16G 7% /

devtmpfs 482M 0 482M 0% /dev

tmpfs 493M 0 493M 0% /dev/shm

tmpfs 493M 6.8M 486M 2% /run

tmpfs 493M 0 493M 0% /sys/fs/cgroup

/dev/sda1 1014M 139M 876M 14% /boot

tmpfs 99M 0 99M 0% /run/user/0

192.168.115.120:/zhangsan 17G 1.2G 16G 7% /media

(5)验证客户端和nfs服务器端文件是否一致
在服务端共享目录/zhangsan里创建一个文件1.txt,在客户端的挂载目录/media里创建一个2.txt文件,然后查看文件。

[root@localhost ~]# ls -l /media/

-rw-r–r-- 1 root root 0 2月 25 14:10 1.txt

-rw-r–r-- 1 nfsnobody nfsnobody 0 2月 25 14:14 2.txt

4、nfs共享权限和访问控制
(1)客户端root用户
使用客户端的root身份在nfs服务器上创建文件,文件的所有者和所属组是nfsnobody。

(2)客户端普通用户
使用客户端的普通用户身份在nfs服务器上创建文件,所有者和所属组是nobody或普通用户。

(3)共享目录的完整权限值设定/var/lib/nfs/etab
1、客户端连接NFS的时候,对root的检查

如果设置no_root_squash,那么此时root用户的身份被压缩为NFS server上面的root;

如果设置了all_squash、anonuid、anongid,此时root 身份被压缩为指定用户;

如果没有明确指定,此时root用户被压缩为nfsnobody;

如果同时指定no_root_squash与all_squash 用户将被压缩为 nfsnobody,如果设置了anonuid、anongid将被压缩到所指定的用户与组;

2、客户端连接NFS的时候,对普通用户的检查

如果明确设定了普通用户被压缩的身份,那么此时客户端用户的身份转换为指定用户;

如果NFS server上面有同名用户,那么此时客户端登录账户的身份转换为NFS server上面的同名用户;

如果没有明确指定,也没有同名用户,那么此时 用户身份被压缩成nobody;

5、NFS共享目录的卸载和自动挂载
(1)NFS共享目录的卸载
a、卸载客户端的挂载目录,在客户端执行以下命令

[root@localhost ~]# umount /media/

b、停止服务器端的共享,在服务器端执行以下命令

[root@localhost ~]# exportfs -au

c、重新共享所有目录并输出详细信息,服务器端执行以下命令

[root@localhost ~]# exportfs -rv

d、exportfs命令

如果我们在启动了NFS之后又修改了/etc/exports,是不是还要重新启动nfs呢?这个时候我们就可以用exportfs 命令来使改动立刻生效,该命令格式如下:

格式:exportfs [-aruv]

-a 全部挂载或卸载 /etc/exports中的内容

-r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab

-u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)

-v 在export的时候,将详细的信息输出到屏幕上。

具体例子:

exportfs -au 卸载所有共享目录

exportfs -rv 重新共享所有目录并输出详细信息

e、rpcinfo命令

rpcinfo利用rpcinfo -p 可以查看出RPC开启的端口所提供的程序有哪些,其中nfs 开启的是2049,portmapper(rpcbind) 开启的是111,其余则是rpc开启的

[root@localhost ~]# rpcinfo -p

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

100024    1   tcp  51424  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  48119  nlockmgr

100021    3   udp  48119  nlockmgr

100021    4   udp  48119  nlockmgr

100021    1   tcp  33728  nlockmgr

100021    3   tcp  33728  nlockmgr

100021    4   tcp  33728  nlockmgr

(2)NFS共享目录的自动挂载
把挂载信息写入到/etc/fstab文件中

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

………………………………………………….省略

192.168.115.120:/zhangsan /media nfs defaults 0 0

[root@localhost ~]# mount -a

[root@localhost ~]# df -h

文件系统 容量 已用 可用 已用% 挂载点

/dev/mapper/cl-root 17G 1.2G 16G 7% /

devtmpfs 482M 0 482M 0% /dev

tmpfs 493M 0 493M 0% /dev/shm

tmpfs 493M 6.8M 486M 2% /run

tmpfs 493M 0 493M 0% /sys/fs/cgroup

/dev/sda1 1014M 139M 876M 14% /boot

tmpfs 99M 0 99M 0% /run/user/0

192.168.115.120:/zhangsan 17G 1.2G 16G 7% /media

6、NFS常见故障解决方法
(1)The rpcbind failure error
故障现象:

nfs mount: server1:: RPC: Rpcbind failure

RPC: Timed Out

nfs mount: retrying: /mntpoint

故障原因:

第一,可能因为客户机的hosts文件中存在错误的ip地址、主机名或节点名组合;

第二,服务器因为过载而暂时停止服务。

(2)The server not responding error
故障现象:

NFS server server2 not responding, still trying

故障原因:

第一,网络不通,用ping命令检测一下。

第二,服务器关机。

(3)The NFS client fails a reboot error
故障现象:

启动客户机后停住了,不断显示如下提示信息:

Setting default interface for multicast: add net 224.0.0.0: gateway:

client_node_name.

故障原因:

在etc/vfstab的mount选项中使用了fg而又无法成功mount服务器上的资源,改成bg或将该行注释掉,直到服务器可用为止。

(4)The service not responding error
故障现象:

nfs mount: dbserver: NFS: Service not responding

nfs mount: retrying: /mntpoint

故障原因:

第一,当前级别不是级别3,用who -r查看,用init 3切换。

第二,NFS Server守护进程不存在,用ps -ef | grep nfs检查,用/etc/init.d/nfs start启动。

(5)The program not registered error
故障现象:

nfs mount: dbserver: RPC: Program not registered

nfs mount: retrying: /mntpoint

故障原因:

第一,当前级别不是级别3。

第二,mountd守护进程没有启动,用/etc/init.d/nfs脚本启动NFS守护进程。

第三,看/etc/dfs/dfstab中的条目是否正常。

(6)The stale file handle error
故障现象:

stale NFS file handle

故障原因:

服务器上的共享资源移动位置了,在客户端使用umount和mount重新挂接就可以了。

(7)The unknown host error
故障现象:

nfs mount: sserver1:: RPC: Unknown host

故障原因:

hosts文件中的内容不正确。

(8)The mount point error
故障现象:

mount: mount-point /DS9 does not exist.

故障原因:

该挂接点在客户机上不存在,注意检查命令行或/etc/vfstab文件中相关条目的拼写。

(9)The no such file error
故障现象:

No such file or directory.

故障原因:

该挂接点在服务器上不存在,注意检查命令行或/etc/vfstab文件中相关条目的拼写。

(10)No route to host
故障现象:

mount 192.168.115.120:/opt/data /data -t nfs -o rw

mount: mount to NFS server ‘192.168.115.120’ failed: System Error: No route to host.

故障原因:

防火墙被打开,关闭防火墙。

这个原因很多人都忽视了,如果开启了防火墙(包括iptables和硬件防火墙),NFS默认使用111端口,我们先要检测是否打开了这个端口,还要检查TCP_Wrappers的设定。

(11)Not owner
故障现象:

mount -F nfs -o rw 192.168.115.120:/mnt/data /data

nfs mount: mount: /data: Not owner

故障原因:

这是Solaris 10版本挂载较低版本nfs时报的错误。

解决:

需要用-o vers=3参数

示例:

mount -F nfs -o vers=3 192.168.115.120:/mnt/data /data

(12)RPC: Program not registered & retrying
故障现象:

nfs mount: 192.168.115.120: : RPC: Program not registered

nfs mount: retrying: /data

故障原因:

没有启动NFS共享端服务。

解决:需要重新启动share端的NFS服务,

Linux:

mount: RPC: Program not registered

/etc/init.d/nfs restart

Solaris:

mount: RPC: Program not registered

/etc/rc.d/init.d/nfs restart

(13)can’t contact portmapper: RPC: Remote system error – Connection refused
故障现象:

exportfs -a

can’t contact portmapper: RPC: Remote system error – Connection refused

故障原因:

出现这个错误信息是由于server端的portmap没有启动。

解决:

/etc/init.d/portmap start

参考:https://www.cnblogs.com/tarencez/p/10605193.html
https://blog.csdn.net/weixin_45120915/article/details/109907527

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值