概述:
NFS介绍★nfs:Network File System ★RPC:Remote Procedure Call 远程过程调用 ★NFS: 属于sun公司, 是一种协议; ★版本: ★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文件系统: /PATH/TO/SOME_DIR CLIENTS_1(export_options,...) CLIENTS_2(export_options,...) single host: IPv4, IPv6, FQDN IP networks:network/netmask,支持两种格式的掩码; wildcards:在主机名字符串中使用通,*.magedu.com, anonymous(匿名):*,表示所有的客户端主机; ro:只读; rw:读写; sync:同步 async:异步 大多数的写操作都是异步的 root_squash:压缩root用户的权限,默认行为;nfsnobody no_root_squash:不压缩root用户的权限; all_squash:压缩所有用户的权限; anonuid and anongid:将压缩的用户映射为此处指定的用户
★NFS Client:
挂载格式:
示例:
共享一个文件系统
1.首先创建一个要共享的目录
1
2
3
| [iyunv@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)权限
1
2
3
4
| [iyunv@centos7 ~]# vim /etc/exports
/nfs/data 10.1.252.153(rw) 10.1.0.0/16(ro)
# 保存退出,这里没有定义其他的,则默认管理员(root_squash)是被压缩的
|
3.启动nfs服务,并查看端口2049
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [iyunv@centos7 ~]# systemctl start nfs
[iyunv@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目录上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [iyunv@CentOS6 ~]# mount -t nfs 10.1.252.161:/nfs/data /mnt
[iyunv@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中,执行写操作
1
2
3
4
| [iyunv@CentOS6 ~]# cd /mnt
[iyunv@CentOS6 mnt]# ls
[iyunv@CentOS6 mnt]# cp /etc/fstab ./
cp: cannot create regular file `./fstab': Permission denied
|
6.发现管理员是没有权限的,这是因为管理员被映射为nfsnobody,没有写权限,那我们在nfs服务端有一个tao用户,赋予tao用户对/nfs/data的rwx权限,然后在客户端,同样创建一个和tao用户id号相同的用户,看能否完成id映射
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| [iyunv@centos7 ~]# setfacl -m u:tao:rwx /nfs/data/
[iyunv@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
[iyunv@centos7 ~]# id tao
uid=1000(tao) gid=1000(tao) groups=1000(tao),2003(distro)
[iyunv@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
[iyunv@CentOS6 ~]# useradd -u 1000 tom
[iyunv@CentOS6 mnt]# id tom
uid=1000(tom) gid=1000(tom) groups=1000(tom)
[iyunv@CentOS6 mnt]# ll
total 4
-rw-rw-r-- 1 tom tom 13 Oct 20 2016 test.txt # 因为id相同,所以这里已经映射成为tom用户
[iyunv@CentOS6 mnt]# echo "zaiyiqi" >> test.txt
-bash: test.txt: Permission denied # 管路员没有权限,原因是被映射为nfsnobody
[iyunv@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 的主机,同样挂载,访问,验证是否为只读权限;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| [iyunv@centos7 ~]# mount -t nfs 10.1.252.161:/nfs/data /mnt
[iyunv@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) # 挂载成功
[iyunv@centos7 home]# useradd -u 1000 xiu # 创建一个同nfs服务端tao用户id相同的用户xiu
[iyunv@centos7 home]# id xiu
uid=1000(xiu) gid=1000(xiu) groups=1000(xiu)
[iyunv@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服务相关的挂载信息 ★选项: Show the NFS server's export list. 查看NFSserver共享了哪些可挂载的信息 List only the directories mounted by some client. 仅列出被某些客户端挂载上的文件系统 命令演示:
1
2
3
4
5
6
7
8
9
10
11
| [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
[iyunv@CentOS6 mnt]# showmount -e 10.1.252.161
Export list for 10.1.252.161:
/nfs/data 10.1.0.0/16
[iyunv@CentOS6 mnt]# showmount -d 10.1.252.161
Directories on 10.1.252.161:
|
3)exportfs命令 ★ -maintain table of exported NFS file systems ★选项: 导出或取消导出所有文件系统,取决于操作选项 重新导出文件系统 关闭导出文件系统
假如我在/nfs/下又创建了一个mydata的目录,想共享给10.1.0.0/16整个网络并且都有rw权限,我们修改完配置文件/etc/exports后,要想生效就要重启服务,但远程其他的文件还在挂载共享,重启服务的话显然是不合适的,这时就要用到exports命令, 演示如下: 1
2
3
4
5
6
7
8
9
10
11
12
| [iyunv@centos7 nfs]# pwd/nfs
[iyunv@centos7 nfs]# mkdir mydata
[iyunv@centos7 nfs]# ls
data mydata
[iyunv@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) # 新添加的共享文件系统
# 如上,保存退出后,在客户端查看共享的文件系统
[iyunv@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 命令 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # 在服务端执行命令
[iyunv@centos7 nfs]# exportfs -ra
# 在客户端再次执行showmount -e 10.1.252.161
[iyunv@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
=====================================================================================
[iyunv@centos7 nfs]# exportfs -ua # 服务端关闭导出所有文件系统
[iyunv@CentOS6 mnt]# showmount -e 10.1.252.161 # 客户端查看
Export list for 10.1.252.161:
[iyunv@CentOS6 mnt]# ls
ls: cannot open directory .: Permission denied # 没有权限
|
4)开机自动挂载:写到/etc/fstab文件中
|