Linux NFS服务器 (文件共享)

NFS主要应用场景是在生产的环境中,上百台服务器为了实现数据同步或者文件共享,会利用到NFS,局域网的一种共享

1.1. 概述

RPC主程序:rpcbind    rpcbind.i686      port:111

NFS主程序:nfs-utils   nfs-utils.i686     port:2049

 

文件共享

权限设置

文件锁定

文件一致性rpc.statd

 

默认端口

 

rpc  远程过程调用

 

 

 

1.2. 安装软件:


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

 

1.3. 服务与端口


Service autofs restart
Service nfs restart
Service rpcbind restart

 

centos5.x的服务

/etc/init.d/portmap restart;
/etc/init.d/nfs restart;

 

centos6.x的服务

/etc/init.d/rpcbind restart\
/etc/init.d/nfs restart

 

1.4. 配置文件

vi /etc/sysconfig/nfs
 
# 主要针对三个程序设置端口
 
#LOCKDARG=
# TCP port rpc.lockd should listen on.
LOCKD_TCPPORT=4000
# UDP port rpc.lockd should listen on.
LOCKD_UDPPORT=4000
 
# Optional arguments passed to rpc.mountd.See rpc.mountd(8)
#RPCMOUNTDOPTS=""
# Port rpc.mountd should listen on.
MOUNTD_PORT=4001
 
#
# Optional arguments passed to rpc.statd.See rpc.statd(8)
#STATDARG=""
# Port rpc.statd should listen on.
STATD_PORT=4002
# Outgoing port statd should used. Thedefault is port
# is random
#STATD_OUTGOING_PORT=2020
 
rpcinfo -plocalhost    # 查询rpc已分配的端口

 

 

1.5. 防火墙的配置:

 

iptables -I INPUT 1 -P tcp --dport 111 -jACCEPT
iptables -I INPUT 1 -P udp --dport 111 -jACCEPT
iptables -I INPUT 1 -P tcp --dport 2049 -jACCEPT
iptables -I INPUT 1 -P udp --dport 2049 -jACCEPT
iptables -I INPUT 1 -P tcp --dport4000:4002 -j ACCEPT
iptables -I INPUT 1 -P udp --dport4000:4002 -j ACCEPT
 
 
service iptables save

 

 

 

1.6. NFS 配置

 

配置步骤:

1. 设置/etc/exports的共享目录  /xx     192.168.1.0/24(rw,sync)

2. 重启服务

3. 客户端访问  showmount -e192.168.1.100  查询共享是否成功

4. Mount挂载

 

 

vi /etc/exports

………………………………………………………………….

共享目录   范围(属性1,属性2,属性3)

 

/xx    192.168.1.0/24(rw,sync,"root_squash") 

…………………………………………………………………

属性列表:

# ro 只读访问
# rw 读写访问
# sync  所有数据在请求时写入共享
# hide      在NFS共享目录中不共享其子目录
# no_hide   共享NFS目录的子目录
# root_squash是对客户端用root访问共享文件夹来转换root权限,
# no root_squash是取消转换权限
# all_squash 对所有用户权限转换成nfsnobody
# no_all_squash   保留共享文件的UID和GID(默认)
# anonuid=1000,anongid=1000  指定转换为UID 1000的用户
# vi /var/lib/nfs/etab  查询默认的一些选项
# exportfs -arv 所有共享重新挂载
# exportfs -au  取消所有共享
# 24代表全网段,如果要代表某台主机,要算子掩数32

 

 

1.7. 客户端访问

客户端要安装nfs-utils

showmount -e 192.168.1.250
 
# 查询1.250服务器共享了哪些共享
 
mount –t nfs 192.168.1.250:/xx  /bb
 
# 将服务器上共享文件夹挂载到本机根目录的bb文件夹

 

 

 

 

 

 

 

1.8. 自动挂载 使用挂载点时自动挂载

 

当使用外置设备,如光驱挂载点时,才自动挂载,闲置时不挂载

 

1.首先写改挂载目录文件,

vi /etc/auto.master
 
# 此文件内容主要是修改需要挂载的外置设备的文件
#
/misc  /etc/auto.misc
/mnt /etc/auto.misc         # /mnt  代表mnt目录下所有子文件夹
/-   /etc/auto.misc         # /- 代表根目录下所有子文件夹
#
 
 
 
Vi /etc/auto.misc
 
# 此目录修改外置外载点
#
# This is an automounter map and it has thefollowing format
# key [ -mount-options-separated-by-comma ]location
# Details may be found in the autofs(5)manpage
 
cd              -fstype=iso9660,ro,nosuid,nodev:/dev/cdrom
cdrom           -fstype=iso9660                 :/dev/cdrom
nfs             -fstype=nfs,rw            192.168.30.250:/xx
 
# cd cdrom nfs代表是子目录,本身不存在的目录
# -fstype=设备类型
# 后面是挂载设备可以是设备也可以是共享文件夹
 
# the following entries are samples topique your imagination
#linux          -ro,soft,intr           ftp.example.org:/pub/linux
#boot           -fstype=ext2            :/dev/hda1
 
 
 
service autofs stop
 
service autofs start 
 
# 不能使用rstart

 

 

1.9. windwos挂载linux主机NFS

启动windosNFS客户端服务:

1. 打开控制面板->程序->打开或关闭windows功能->NFS客户端

勾选NFS客户端,即开启windows NFS客户端服务.

2.win+R->cmd

mount 192.168.1.10:/home/用户/share X:

成功挂载,打开我的点脑,你即可在你网络位置看到X:盘了

解释: 

mount,是指令

192.168.1.10你的linux主机IP

/home/share你的共享目录

X:你挂载的网络文件盘--注意,可能会与你的其他盘冲突,你可以随意更改

3.取消挂载:

直接在 我的电脑 里面鼠标点击取消映射网络驱动器 X:

或者:win+R->cmd

输入: umountX:

(umount-a 取消所有网络驱动器)

好了,尽情享用吧,祝你成功!

 

1.10. 共享目录权限实例1

5个账号同时共享一个目录/home/usergroup这个目录,这个目能只能让这5个用户使用,对此目录有所有权限,其它人无权限,

 

需要有SGID权限,否则5个账号间会出现彼此无法修改对方的数据,

mkdir /home/usergroup
chgrp usergroup /home/usergroup
chomd 2770 /home/usergroup
ll -d /home/usergroup

 

对共享目录做磁盘配额限制

 

每个用户的配额为2GB(hard)以及1.8GB(soft),

1.vim /etc/fstab   # 建立开机挂载
2.在rw后面加上userquota,grpquota   也可以mount -o usrquto,grpquota /dev/sda1 /mnt/disk
3.quotacheck -avug 制作数据文件
4.group quotas turned on
5.user quotas turned on
6.edquota -u user1  # 给用户配置配额
7.edquota -p user1 user2  # 复制配置给其它用户
8.repquota -au   #查询所有用户配额配

 

 

1.11. 步骤精华  ★★★

  1. 安装软件
  2. 配置共享 vi /etc/export  写入共享目录 格式:
  3.        重启服务
  4. 关闭防火墙或selinux       检查分配端口
  5. 客户端安装
  6. Showmount –e 192.18.1.100 测试结果
  7. 在mount挂载

 

 

 

1.12. 故障排除

 

往共享目录写文件

 

[root@localhost ~]#touch aa

touch: 无法创建"aa":权限不够

 

检查共享目录本身是否对other有写权限

 

注:默认情况共享文件夹客户端不允许root权限,即实使用root也会


被更改为nfsnobody

 

挂载意外中断,无法进行操作的案例:

 

 

错误提示:

[root@localhost~]# mount 192.168.182.128:/mnt /tmp/
mount.nfs:access denied by server while mounting 192.168.182.128:/mnt

解决方法:这是因为授权访问无权限  在exports中,要指定网段或IP有权限mount,也可以用*代表所有

/mnt  192.168.1.0/24(rw)    # 这个Ip段,或者用*代表所有/mnt *(rw)

1.12.1. 限制只有1.10主机能访问,其实不能访问

/XX 192.168.1.10/32(rw,sync)192.168.1.0/24(r0,sync)
 
# 如果不加192.168.1.0/24(r0,sync),代表其它电脑不能挂载

 

 

1.12.2. 后台挂载,网络断开自动挂载

mount -0 bg 192.168.1.250:xx /mnt

 

 

错误提示:

 

[root@server2 ~]# showmount -e192.168.242.128
clnt_create: RPC: Program not registered
 
# 软件未注册,因为服务没重启
 
[root@server2 ~]# showmount -e192.168.242.128
clnt_create: RPC: Port mapper failure -Unable to receive: errno 113 (No route to host)
 
# 端口连接失败,防火墙设置
 
 
 
[root@186 etc]# showmount -e 192.168.16.170
clnt_create: RPC: Port mapper failure -Unable to receive: errno 111 (Connection refused)

 

转载于:https://blog.51cto.com/zhongliang/1878978