您的位置:首页 > 其它

文件共享服务---NFS

2016-10-20 14:34 232 查看
文件共享服务---NFS=============================================================================
概述:

NFS介绍

nfsNetwork File System★RPC:Remote Procedure Call 远程过程调用★NFS: 属于sun公司, 是一种协议;★版本:NFSv1,
NFSv2, NFSv3,
NIS:Network Information Service(网络信息服务)
nfsd:借助nfsd监听在 tcp的2049端口,2049/tcp★辅助类的服务:rpc (portmap)rpc.mountd:认证;
rpc.lockd:加锁
rpc.statd:状态

NFS安装与配置管理

nfs server:安装: 确保内核空间有内核模块nfsd,一般都会提供; 只需安装用户空间的工具程序:nfs-utils(# yum install nfs-utils)
主程序文件: Unit File:/usr/lib/systemd/system/nfs.service
主配置文件: /etc/exports, /etc/exports.d/*
管理共享的nfs文件系统:在/etc/exports文件,或者/etc/exports.d/*目录下任何文件中新创建一个导出文件即可;
配置文件每一行定义一个共享文件系统
定义格式:

/PATH/TO/SOME_DIR CLIENTS_1(export_options,...) CLIENTS_2(export_options,...) CLIENTS:
single host: IPv4, IPv6, FQDN IP networks:network/netmask,支持两种格式的掩码; wildcards:在主机名字符串中使用通,*.magedu.com, anonymous(匿名):*,表示所有的客户端主机;General Options
ro:只读; rw:读写; sync:同步 async:异步 大多数的写操作都是异步的User ID Mapping:主要是做id映射的
root_squash:压缩root用户的权限,默认行为;nfsnobody no_root_squash:不压缩root用户的权限; all_squash:压缩所有用户的权限; anonuid and anongid:将压缩的用户映射为此处指定的用户
NFS Client:
挂载格式:mount -t nfs NFS_SERVER:/PATH/TO/EXPORTED_DIR /MOUNT_POINT [-rvVwfnsh] [-o OPTIONS]

示例:
共享一个文件系统
1.首先创建一个要共享的目录
[root@centos7 ~]# mkdir /nfs/data -pv
mkdir: created directory ‘/nfs’
mkdir: created directory ‘/nfs/data’
2.编辑配置文件/etc/exports,定义导出/nfs/data文件至10.1.0.0/16这个网络,但仅10.1.252.153有rw权限,其余的都为只读(ro)权限
[root@centos7 ~]# vim /etc/exports
/nfs/data  10.1.252.153(rw)  10.1.0.0/16(ro)

# 保存退出,这里没有定义其他的,则默认管理员(root_squash)是被压缩的
3.启动nfs服务,并查看端口2049
[root@centos7 ~]# systemctl start nfs
[root@centos7 ~]# ss -tnl
State       Recv-Q Send-Q                                      Local Address:Port                                                     Peer Address:Port
LISTEN      0      50                                                      *:445                                                                 *:*
LISTEN      0      64                                                      *:2049                                                                *:*
LISTEN      0      128                                                     *:43108                                                               *:*
LISTEN      0      50                                                      *:139                                                                 *:*
LISTEN      0      128                                                     *:111                                                                 *:*
LISTEN      0      128                                                     *:20048                                                               *:*
LISTEN      0      64                                                      *:50897                                                               *:*
LISTEN      0      128                                                     *:22                                                                  *:*
LISTEN      0      128                                             127.0.0.1:631                                                                 *:*
LISTEN      0      100                                             127.0.0.1:25                                                                  *:*
LISTEN      0      128                                             127.0.0.1:6010                                                                *:*
LISTEN      0      50                                                     :::445                                                                :::*
LISTEN      0      64                                                     :::2049                                                               :::*
4.以CentOS 6 作为客户端挂载nfs服务的/nfs/data到本地的/mnt目录上
[root@CentOS6 ~]# mount -t nfs 10.1.252.161:/nfs/data  /mnt
[root@CentOS6 ~]# mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
/dev/sda5 on /home type ext4 (rw,usrquota,grpquota)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
10.1.252.161:/nfs/data on /mnt type nfs (rw,vers=4,addr=10.1.252.161,clientaddr=10.1.252.153)
5.进到挂载点/mnt中,执行写操作
[root@CentOS6 ~]# cd /mnt
[root@CentOS6 mnt]# ls
[root@CentOS6 mnt]# cp /etc/fstab ./
cp: cannot create regular file `./fstab': Permission denied
6.发现管理员是没有权限的,这是因为管理员被映射为nfsnobody,没有写权限,那我们在nfs服务端有一个tao用户,赋予tao用户对/nfs/data的rwx权限,然后在客户端,同样创建一个和tao用户id号相同的用户,看能否完成id映射
[root@centos7 ~]# setfacl -m u:tao:rwx /nfs/data/
[root@centos7 ~]# getfacl /nfs/data/
getfacl: Removing leading '/' from absolute path names
# file: nfs/data/
# owner: root
# group: root
user::rwx
user:tao:rwx
group::r-x
mask::rwx
other::r-x

[root@centos7 ~]# id tao
uid=1000(tao) gid=1000(tao) groups=1000(tao),2003(distro)

[root@centos7~]# su - tao
[tao@centos7 ~]$ cd /nfs/data/
[tao@centos7 data]$ echo "taotaoxiuixu" > test.txt
[tao@centos7 data]$ ls
test.txt    # 可见在nfs服务端tao用户可执行写操作

====================================================================================

# 在客户端创建一个同tao用户id(1000)相同的用户tom
[root@CentOS6 ~]# useradd -u 1000 tom
[root@CentOS6 mnt]# id tom
uid=1000(tom) gid=1000(tom) groups=1000(tom)
[root@CentOS6 mnt]# ll
total 4
-rw-rw-r-- 1 tom tom 13 Oct 20  2016 test.txt  # 因为id相同,所以这里已经映射成为tom用户

[root@CentOS6 mnt]# echo "zaiyiqi" >> test.txt
-bash: test.txt: Permission denied  # 管路员没有权限,原因是被映射为nfsnobody

[root@CentOS6 mnt]# su - tom # 切换到tom用户
[tom@CentOS6 ~]$ cd /mnt
[tom@CentOS6 mnt]$ ls
test.txt
[tom@CentOS6 mnt]$  echo "zaiyiqi" >> test.txt  # 可以执行写操作
[tom@CentOS6 mnt]$ cat test.txt
taotaoxiuixu
zaiyiqi
[tom@CentOS6 mnt]$ cp /etc/issue ./
[tom@CentOS6 mnt]$ ll
total 8
-rw-r--r-- 1 tom tom 90 Oct 20  2016 issue
-rw-rw-r-- 1 tom tom 21 Oct 20  2016 test.txt

======================================================================================
# 再切换到服务端
[tao@centos7 data]$ ll
total 8
-rw-r--r-- 1 tao tao 90 Oct 20 12:35 issue  # 在客户端属主为tom的在服务端为tao,可见他们仅通过id进行映射
-rw-rw-r-- 1 tao tao 21 Oct 20 12:25 test.txt
7.我们在nfs服务端还定义了其他网络中的客户机只有读权限,这里我使用ip为10.1.249.203 的主机,同样挂载,访问,验证是否为只读权限;
[root@centos7 ~]# mount -t nfs 10.1.252.161:/nfs/data  /mnt
[root@centos7 ~]# mount
10.1.252.161:/nfs/data on /mnt type nfs4 (rw,relatime,vers=4.0,rsize=131072,wsize=131072,
namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.1.249.203,
local_lock=none,addr=10.1.252.161)  # 挂载成功

[root@centos7 home]# useradd -u 1000 xiu # 创建一个同nfs服务端tao用户id相同的用户xiu
[root@centos7 home]# id xiu
uid=1000(xiu) gid=1000(xiu) groups=1000(xiu)
[root@centos7 home]# su - xiu  # 切换到xiu用户
[xiu@centos7 ~]$ cd /mnt
[xiu@centos7 mnt]$ ls
issue  test.txt
[xiu@centos7 mnt]$ rm - issue  # 执行删除操作
rm: cannot remove ‘-’: No such file or directory
rm: remove write-protected regular file ‘issue’? y
rm: cannot remove ‘issue’: Read-only file system   # 文件为只读,没有写权限
[xiu@centos7 mnt]$ ls
issue  test.txt

# 这里虽然对文件系统有写权限,但服务没有写权限,所以,不能执行写操作


所有的文件共享服务都遵循的法则:
一是服务权限,

二是文件系统权限,

只有二者同时满足才可以执行其操作,

2)showmount命令
★-show mount information for an NFS server 查看指定的nfs服务相关的挂载信息★选项: -e or --exports
Show the NFS server's export list. 查看NFSserver共享了哪些可挂载的信息 -d or --directories
List only the directories mounted by some client. 仅列出被某些客户端挂载上的文件系统 命令演示:
[tom@CentOS6 ~]$ showmount -e 10.1.252.161
Export list for 10.1.252.161:
/nfs/data 10.1.0.0/16
[tom@CentOS6 ~]$ exit
logout
[root@CentOS6 mnt]# showmount -e 10.1.252.161
Export list for 10.1.252.161:
/nfs/data 10.1.0.0/16

[root@CentOS6 mnt]# showmount -d 10.1.252.161
Directories on 10.1.252.161:
3)[b]exportfs命令[/b]★ -maintain table of exported NFS file systems★选项:-aExportor unexport all directories.
导出或取消导出所有文件系统,取决于操作选项 -r: Reexport all directories, synchronizing/var/lib/nfs/etab with /etc/exports and files under /etc/exports.d.
重新导出文件系统-u: Unexportone or more directories.
关闭导出文件系统
假如我在/nfs/下又创建了一个mydata的目录,想共享给10.1.0.0/16整个网络并且都有rw权限,我们修改完配置文件/etc/exports后,要想生效就要重启服务,但远程其他的文件还在挂载共享,重启服务的话显然是不合适的,这时就要用到exports命令, 演示如下:
[root@centos7 nfs]# pwd/nfs
[root@centos7 nfs]# mkdir mydata
[root@centos7 nfs]# ls
data  mydata
[root@centos7 data]# vim /etc/exports
/nfs/data  10.1.252.153(rw)  10.1.0.0/16(ro)
/nfs/mydata  10.1.0.0/16(rw)  # 新添加的共享文件系统

# 如上,保存退出后,在客户端查看共享的文件系统
[root@CentOS6 mnt]# showmount -e 10.1.252.161
Export list for 10.1.252.161:/nfs/data 10.1.0.0/16
# 发现共享的仍然只有/nfs/data目录
要想不用重启NFS服务,又在客户端显示出来使用exports -ra 命令
# 在服务端执行命令
[root@centos7 nfs]# exportfs -ra

# 在客户端再次执行showmount -e 10.1.252.161
[root@CentOS6 mnt]# showmount -e 10.1.252.161
Export list for 10.1.252.161:
/nfs/mydata 10.1.0.0/16  # 新创建的共享
/nfs/data   10.1.0.0/16
=====================================================================================

[root@centos7 nfs]#  exportfs -ua # 服务端关闭导出所有文件系统

[root@CentOS6 mnt]# showmount -e 10.1.252.161  # 客户端查看
Export list for 10.1.252.161:
[root@CentOS6 mnt]# ls
ls: cannot open directory .: Permission denied  # 没有权限
4)开机自动挂载:写到/etc/fstab文件中
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  NFS