NAS之NFS
=====================================================================================
一、NFS服务器
1. 软件包 nfs-utils
2. 端口 2049/tcp
3. 配置文件 /etc/exports
[root@station02 ~]# mkdir -p /share/dir1 /share/dir2
[root@station02 ~]# chmod 777 /share/dir2
[root@station02 ~]# touch /share/dir1/1
[root@station02 ~]# touch /share/dir2/2
[root@station02 ~]# vim /etc/exports
/share/dir1 192.168.0.0/24(ro,sync)
/share/dir2 *(rw,sync)
4. 启动
[root@station02 ~]# service portmap start #守护进程
[root@station02 ~]# service nfs restart
关闭 NFS mountd: [失败]
关闭 NFS 守护进程: [失败]
关闭 NFS quotas: [失败]
启动 NFS 服务: [确定]
关掉 NFS 配额: [确定]
启动 NFS 守护进程: [确定]
启动 NFS mountd: [确定]
[root@station02 ~]# chkconfig nfs on
[root@station02 ~]# exportfs -r//reload
[root@station02 ~]# exportfs -v//显示当前输出的所有共享
[root@station02 ~]# ps aux |grep nfs
root 4156 0.0 0.0 0 0 ? S< 20:29 0:00 [nfsd4]
root 4158 0.0 0.0 0 0 ? S 20:29 0:00 [nfsd]
root 4159 0.0 0.0 0 0 ? S 20:29 0:00 [nfsd]
root 4160 0.0 0.0 0 0 ? S 20:29 0:00 [nfsd]
root 4161 0.0 0.0 0 0 ? S 20:29 0:00 [nfsd]
root 4162 0.0 0.0 0 0 ? S 20:29 0:00 [nfsd]
root 4163 0.0 0.0 0 0 ? S 20:29 0:00 [nfsd]
root 4164 0.0 0.0 0 0 ? S 20:29 0:00 [nfsd]
root 4165 0.0 0.0 0 0 ? S 20:29 0:00 [nfsd]
root 4314 0.0 0.2 5128 676 pts/1 R+ 21:20 0:00 grep nfs
[root@station02 ~]# netstat -tnlp | grep :2049
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
[root@station02 ~]# netstat -an |grep 2049
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN
tcp 0 0 192.168.0.2:2049 192.168.0.120:927 ESTABLISHED
udp 0 0 0.0.0.0:2049 0.0.0.0:*
====================================================================================
二、NFS客户端
[root@station11 ~]# showmount -e 192.168.0.2
Export list for 192.168.0.2:
/share/dir2 *
/share/dir1 192.168.0.0/24
[root@station11 ~]# mkdir /mnt/dir1 /mnt/dir2
[root@station11 ~]# mount -t nfs 192.168.0.2:/share/dir1 /mnt/dir1
[root@station11 ~]# mount -t nfs 192.168.0.2:/share/dir2 /mnt/dir2
[root@station11 ~]# ls /mnt/dir1
1
[root@station11 ~]# ls /mnt/dir2
2
==================================================================
探究nfs挂载时权限的验证方式
服务器端:
=======
[root@station02 ~]# useradd alice
[root@station02 ~]# mkdir /share/dir3
[root@station02 ~]# chown alice.alice /share/dir3
[root@station02 ~]# id alice
uid=500(alice) gid=500(alice) groups=500(alice)
[root@station02 ~]#
[root@station02 ~]# vim /etc/exports
/share/dir1 192.168.0.0/24(ro,sync)
/share/dir2 *(rw,sync)
/share/dir3 *(rw,sync)//目录对于其他人没有写权限
[root@station02 ~]# exportfs -r
[root@station02 ~]# exportfs -v
/share/dir1 192.168.0.0/24(ro,wdelay,root_squash,no_subtree_check,anonuid=65534,anongid=65534)
/share/dir2 <world>(rw,wdelay,root_squash,no_subtree_check,anonuid=65534,anongid=65534)
/share/dir3 <world>(rw,wdelay,root_squash,no_subtree_check,anonuid=65534,anongid=65534)
从客户端测试:
===========
首先以root用户挂载
[root@station11 ~]# mkdir /mnt/dir3
[root@station11 ~]# mount 192.168.0.2:/share/dir3 /mnt/dir3
[root@station11 ~]# touch /mnt/dir3/file1
touch: 无法触碰 “/mnt/dir3/file1”: 权限不够
[root@station11 ~]# useradd jack
[root@station11 ~]# id jack
uid=500(jack) gid=500(jack) groups=500(jack)
以jack用户访问(普通用户是不能使用mount命令挂载任何文件系统)
[root@station11 ~]# su - jack
[jack@station11 ~]$ touch /mnt/dir3/file1
====
分析:
====
1. 对比服务端和客户端文件属性
[root@station02 ~]# ll -dn /share/dir3//服务器端
drwxr-xr-x 2 500 500 4096 10-02 20:45 /share/dir3
[root@station11 ~]# ll -dn /mnt/dir3//客户端
drwxr-xr-x 2 500 500 4096 2012-10-02 /mnt/dir3
====
结果:
====
NFS权限是通过UID、GID映射的
从客户端如果使用root访问,将默认映射为nfsnobody(服务端在共享时使用了root_squash,服务端共享时添加参数no_root_squash可以让目录拥有写权限)
如果希望两端的UID和GID能够一样
1. useradd tom -u 2000
2. 使用LDAP服务器提供统一的UID和GID
================================================================================
再谈客户端挂载:
方法一:
[root@station11 ~]# mount 192.168.0.2:/share/dir1 /mnt/dir1 //临时
方法二:
vim /etc/fstab
192.168.0.2:/share/dir1 /mnt/dir1 nfs ro 0 0
192.168.0.2:/share/dir2 /mnt/dir2 nfs rw 0 0
[root@station11 ~]# mount -a
方法三:
automount (进程autofs)自动挂载,按需挂载
[root@station11 ~]# mkdir /mnt/nfs//准备一个父挂载点,即监控目录
[root@station11 ~]# vim /etc/auto.master
/mnt/nfs /etc/auto.nfs
[root@station11 ~]# vim /etc/auto.nfs
dir1 -ro 192.168.0.2:/share/dir1
dir2 -rw 192.168.0.2:/share/dir2
[root@station11 ~]# service autofs restart
本文转自zhang25yun51CTO博客,原文链接: http://blog.51cto.com/1585654/1386900,如需转载请自行联系原作者