NFS 即 network file system,网络文件系统。允许通过网络共享文件系统将服务端文件系统共享给远程客户端,在客户端上访问共享目录与访问客户端本地文件系统一样。
服务端IP:192.168.147.137,客户端IP:192.168.147.138
服务端:
在服务端上安装NFS服务需要安装两个包:nfs-utils 和 rpcbind
login as: root
[email protected]‘s password:
Last login: Sat Feb 25 21:16:48 2017 from 192.168.147.1
[[email protected] ~]# yum install -y nfs-utils rpcbind
创建共享目录并将其权限改为777:
[[email protected] ~]# mkdir -p /home/network/nfs
[[email protected] ~]# chmod 777 /home/network/nfs
打开配置文件/etc/exports:
[[email protected] ~]# vim /etc/exports
默认是空的,我们写入一行内容:
#共享的目录 共享给IP或网段(操作,同步,权限)
/home/network/nfs 192.168.147.138(rw,sync)
保存退出,
启动rpcbind服务
[[email protected] ~]# /etc/init.d/rpcbind start
正在启动 rpcbind: [确定]
然后启动NFS服务
[[email protected] ~]# /etc/init.d/nfs start
启动 NFS 服务: [确定]
启动 NFS mountd: [确定]
启动 NFS 守护进程: [确定]
正在启动 RPC idmapd: [确定]
客户端:
在客户端主机上安装包nfs-utils
login as: root
[email protected]‘s password:
Last login: Wed Feb 22 22:07:44 2017
[[email protected] ~]# yum install -y nfs-utils
启动rpcbind和nfs服务
[[email protected] ~]# /etc/init.d/rpcbind start
正在启动 rpcbind: [确定]
[[email protected] ~]# /etc/init.d/nfs start
启动 NFS 服务: [确定]
启动 NFS mountd: [确定]
启动 NFS 守护进程: [确定]
正在启动 RPC idmapd: [确定]
然后使用命令showmount查看服务端共享信息(共享目录,共享给谁)
[[email protected] ~]# showmount -e 192.168.147.137
Export list for 192.168.147.137:
/home/network/nfs 192.168.147.138
接着就可以将共享的目录挂载到客户端指定的挂载点上了
[[email protected] ~]# mount -t nfs 192.168.147.137:/home/network/nfs /mnt
查看挂载后的磁盘信息
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cp1-lv_root 18G 1.4G 15G 9% /
tmpfs 947M 0 947M 0% /dev/shm
/dev/sda1 485M 31M 429M 7% /boot
192.168.147.137:/home/network/nfs 18G 2.5G 14G 15% /mnt
测试nfs:
1.操作:ro 只读;rw 读写;
服务端:
[[email protected] ~]# vim /etc/exports
/home/network/nfs 192.168.147.138(ro)
[[email protected] ~]# exportfs -arv
exporting 192.168.147.138:/home/network/nfs
客户端:
[[email protected] mnt]$ ls
text.txt
[[email protected] mnt]$ cat text.txt
123456
[[email protected] mnt]$ touch 1.txt
touch: 无法创建"1.txt": 只读文件系统
服务端:
[[email protected] nfs]# vim /etc/exports
/home/network/nfs 192.168.147.138(rw)
[[email protected] nfs]# exportfs -arv
exporting 192.168.147.138:/home/network/nfs
客户端:
[[email protected] mnt]$ ls
text.txt
[[email protected] mnt]$ cat text.txt
123456
[[email protected] mnt]$ touch 1.txt
[[email protected] mnt]$ ls
1.txt text.txt
2.是否同步:
sync,同步,内存数据实时写入磁盘
async,不同步,内存数据定期写入磁盘
3.限制用户:默认情况下,客户端对nfs文件系统操作是限制以nfsnobody用户身份进行的。
root_squash:只限制root用户为anonuid和anongid
no_root_squash:不限制root用户,
all_squash:限制所有用户为anonuid和anongid
no_all_squash:所有用户都不限制
如果限制,需要指明限制为哪个用户和组:anonuid和anongid,否则将限制为默认的nfsnobody。
(1)默认情况下,将客户端root用户创建的文件限制为nfsnobody,一般用户创建的文件限制为nobody:
[[email protected] nfs]# vim /etc/exports
/home/network/nfs 192.168.147.138(rw,sync)
[[email protected] nfs]# exportfs -arv
exporting 192.168.147.138:/home/network/nfs
[[email protected] mnt]# touch root.txt
[[email protected] mnt]# su rachy
[[email protected] mnt]$ touch rachy.txt
[[email protected] mnt]$ ls -l
总用量 0
-rw-rw-r-- 1 nobody nobody 0 2月 25 23:06 rachy.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 2月 25 23:06 root.txt
(2)all_squash
[[email protected] nfs]# vim /etc/exports
/home/network/nfs 192.168.147.138(rw,sync,all_squash)
[[email protected] nfs]# exportfs -arv
exporting 192.168.147.138:/home/network/nfs
[[email protected] mnt]# touch root.txt
[[email protected] mnt]# su rachy
[[email protected] mnt]$ touch rachy.txt
[[email protected] mnt]$ ls -l
总用量 0
-rw-rw-r-- 1 nfsnobody nfsnobody 0 2月 25 23:11 rachy.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 2月 25 23:11 root.txt
(3)all_squash,anonuid=502,anongid=502,将所有用户都限制为服务端上用户rachy
[[email protected] nfs]# vim /etc/exports
/home/network/nfs 192.168.147.138(rw,sync,all_squash,anonuid=502,anongid=502)
[[email protected] nfs]# exportfs -arv
exporting 192.168.147.138:/home/network/nfs
[[email protected] nfs]# tail -n 1 /etc/passwd
rachy:x:502:502::/home/rachy:/bin/bash
[[email protected] mnt]# touch root
[[email protected] mnt]# su rachy
[[email protected] mnt]$ touch rachy
[[email protected] mnt]$ ls -l
总用量 0
-rw-rw-r-- 1 rachy rachy 0 2月 25 23:20 rachy
-rw-r--r-- 1 rachy rachy 0 2月 25 23:20 root
(4)no_root_squash不限制root用户,其他用户自动被限制为nobody:
[[email protected] nfs]# vim /etc/exports
/home/network/nfs 192.168.147.138(rw,sync,no_root_squash)
[[email protected] nfs]# exportfs -arv
exporting 192.168.147.138:/home/network/nfs
[[email protected] mnt]# touch root
[[email protected] mnt]# su rachy
[[email protected] mnt]$ touch rachy
[[email protected] mnt]$ ls -l
总用量 0
-rw-rw-r-- 1 nobody nobody 0 2月 25 23:34 rachy
-rw-r--r-- 1 root root 0 2月 25 23:34 root
(5)root_squash,anonuid=502,anongid=502,将root用户限制为服务端上用户rachy,其他用户自动被限制为nobody:
[[email protected] nfs]# vim /etc/exports
/home/network/nfs 192.168.147.138(rw,sync,root_squash,anonuid=502,anongid=502)
[[email protected] nfs]# exportfs -arv
exporting 192.168.147.138:/home/network/nfs
[[email protected] mnt]# touch root
[[email protected] mnt]# su rachy
[[email protected] mnt]$ touch rachy
[[email protected] mnt]$ ls -l
总用量 0
-rw-rw-r-- 1 nobody nobody 0 2月 25 23:38 rachy
-rw-r--r-- 1 rachy rachy 0 2月 25 23:38 root
(6)no_all_squash,都不限制的话,root被限制为nfsnobody,其他用户为nobody,与什么都不做的情况(1)一样:
[[email protected] nfs]# vim /etc/exports
/home/network/nfs 192.168.147.138(rw,sync,no_all_squash)
[[email protected] nfs]# exportfs -arv
exporting 192.168.147.138:/home/network/nfs
[[email protected] mnt]# touch root
[[email protected] mnt]# su rachy
[[email protected] mnt]$ touch rachy
[[email protected] mnt]$ ls -l
总用量 0
-rw-rw-r-- 1 nobody nobody 0 2月 25 23:42 rachy
-rw-r--r-- 1 nfsnobody nfsnobody 0 2月 25 23:42 root