Linux NFS网络文件系统
1.1 NFS简介
NFS是Network File System的缩写,即网络文件系统。NFS是由Sun开发并发展起来的一项用于在不同机器,不同操作系统之间通过网络互相分享各自的文件。NFS server 也可以看作是一个FILE SERVER,用于在UNIX类系统之间共享文件,可以轻松的挂载(mount)到一个目录上,操作起来就像本地文件一样的方便。
挂载原理:NFS是通过网络来进行数据传输的,NFS会使用一些端口来传输数据(端口是随机的)-->通过远程过程调用(RPC)协议/服务来实现的。

说明:
①client端的RPC远程调用服务请求到server端的RPC服务
②server端的RPC服务查到NFS服务的端口号并将端口号返回给client端
③client端根据返回的结果获取到server端NFS服务的端口号
④client端根据端口号请求到server端的NFS服务,从而获取到数据
NFS相关目录 [root@nfs-server data]# ll /var/lib/nfs/ total 32 -rw-r--r-- 1 root root 155 May 15 04:15 etab -rw-r--r-- 1 root root 27 May 15 02:32 rmtab drwxr-xr-x 7 root root 0 May 15 01:58 rpc_pipefs drwx------ 4 rpcuser rpcuser 4096 Apr 14 03:28 statd -rw------- 1 root root 0 Feb 26 2012 state drwxr-xr-x 2 root root 4096 Feb 26 2012 v4recovery -rw-r--r-- 1 root root 0 Feb 26 2012 xtab 挂载记录/var/lib/nfs/rmtab [root@nfs-server nfs]# cat /var/lib/nfs/rmtab 10.0.0.52:/data:0x00000001 挂载参数,没有的会自动补全/var/lib/nfs/etab [root@nfs-server nfs]# cat etab /data 10.0.0.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,mapping=identity,anonuid=999,anongid=99 9) NFS配置文件的设置:/etc/exports exports文件内容格式:<输出目录> [客户端1 选项(访问权限,用户映射,其他)] [客户端2 选项(访问权限,用户映射,其他)
|
输出目录 |
输出目录是指NFS系统中需要共享给客户机使用的目录 |
|||||||||||||||||||||||||||||||||||
|
客户端 |
网络中可以访问这个NFS输出目录的计算机 |
|||||||||||||||||||||||||||||||||||
|
选项 |
设置输出目录的访问权限、用户映射和其它选项
|
NFS默认用户 nfsnobody [root@lic ~]# cat /var/lib/nfs/etab /data 10.0.0.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,mapping=identity,anonuid=65534,anong id=65534) [root@lic ~]# grep nfs /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
1.2 NFS实战应用
[server: 10.0.0.56]
[root@server ~]# cat /etc/redhat-release CentOS release 5.8 (Final) [root@server ~]# uname -r 2.6.18-308.el5 [root@server ~]# ifconfig eth0|sed -n '2p'|awk -F '[ :]+' '{print $4}' 10.0.0.56 #确认服务安装、开启服务、确认用户UID一致 [root@server ~]# rpm -qa|egrep 'portmap|nfs' portmap-4.0-65.2.2.1 nfs-utils-lib-1.0.8-7.9.el5 nfs-utils-1.0.9-60.el5 [root@server ~]# /etc/init.d/portmap start Starting portmap: [ OK ] [root@server ~]# /etc/init.d/nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS daemon: [ OK ] Starting NFS mountd: [ OK ] [root@server ~]# grep 'nfsnobody' /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
#建立共享目录 [root@server ~]# mkdir /data/w_shared /data/r_shared -p [root@server ~]# tree /data /data |-- r_shared `-- w_shared 2 directories, 0 files
#目录赋予权限,更改属主为nfsnobody [root@server ~]# chown -R nfsnobody /data/r_shared/ /data/w_shared/ [root@server ~]# ls -l /data total 8 drwxr-xr-x 2 nfsnobody root 4096 May 21 06:57 r_shared drwxr-xr-x 2 nfsnobody root 4096 May 21 06:57 w_shared
#设置共享,查看 [root@server ~]# cat /etc/exports /data/w_shared 10.0.0.0/24(rw,sync) /data/r_shared 10.0.0.0/24(ro) [root@server ~]# /etc/init.d/nfs reload [root@server ~]# showmount -e 127.0.0.1 Export list for 127.0.0.1: /data/r_shared 10.0.0.0/24 /data/w_shared 10.0.0.0/24 [client01: 10.0.0.57] [root@client01 ~]# cat /etc/redhat-release CentOS release 5.8 (Final) [root@client01 ~]# uname -r 2.6.18-308.el5 [root@client01 ~]# ifconfig eth0|sed -n '2p'|awk -F '[ :]+' '{print $4}' 10.0.0.57 #确认服务安装、开启服务、确认用户UID一致 [root@client01 ~]# rpm -qa|egrep 'portmap|nfs' portmap-4.0-65.2.2.1 nfs-utils-lib-1.0.8-7.9.el5 nfs-utils-1.0.9-60.el5 [root@client01 ~]# /etc/init.d/portmap start Starting portmap: [ OK ] [root@client01 ~]# grep 'nfsnobody' /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
#查看server共享 [root@client01 ~]# showmount -e 10.0.0.56 Export list for 10.0.0.56: /data/r_shared 10.0.0.0/24 /data/w_shared 10.0.0.0/24
#建立挂载目录,挂载、查看挂载 [root@client01 ~]# mkdir /data/b_w /data/b_r -p [root@client01 ~]# tree /data /data |-- b_r `-- b_w 2 directories, 0 files [root@client01 ~]# mount -t nfs 10.0.0.56:/data/w_shared /data/b_w [root@client01 ~]# mount -t nfs 10.0.0.56:/data/r_shared /data/b_r [root@client01 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 19G 1.9G 16G 11% / /dev/sda1 122M 12M 104M 10% /boot tmpfs 62M 0 62M 0% /dev/shm 10.0.0.56:/data/w_shared 19G 1.9G 16G 11% /data/b_w 10.0.0.56:/data/r_shared 19G 1.9G 16G 11% /data/b_r [client02: 10.0.0.58] [root@client02 ~]# cat /etc/redhat-release CentOS release 5.8 (Final) [root@client02 ~]# uname -r 2.6.18-308.el5 [root@client02 ~]# ifconfig eth0|sed -n '2p'|awk -F '[ :]+' '{print $4}' 10.0.0.58 #确认服务安装、开启服务、确认用户UID一致 [root@client02 ~]# rpm -qa|egrep 'portmap|nfs' nfs-utils-lib-1.0.8-7.9.el5 nfs-utils-1.0.9-60.el5 portmap-4.0-65.2.2.1 [root@client02 ~]# /etc/init.d/portmap start Starting portmap: [ OK ] [root@client02 ~]# grep 'nfsnobody' /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin #查看server共享 [root@client02 ~]# showmount -e 10.0.0.56 Export list for 10.0.0.56: /data/r_shared 10.0.0.0/24 /data/w_shared 10.0.0.0/24
#建立挂载目录,挂载、查看挂载 [root@client02 ~]# mkdir /data/w_lican /data/r_lican -p [root@client02 ~]# tree /data /data |-- r_lican `-- w_lican 2 directories, 0 files
#读写目录不建议在server端操作、只读目录只能server端操作 [root@client02 ~]# mount -t nfs 10.0.0.56:/data/w_shared /data/w_lican [root@client02 ~]# mount -t nfs 10.0.0.56:/data/r_shared /data/r_lican [root@client02 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 19G 1.9G 16G 11% / /dev/sda1 122M 12M 104M 10% /boot tmpfs 62M 0 62M 0% /dev/shm 10.0.0.56:/data/w_shared 19G 1.9G 16G 11% /data/w_lican 10.0.0.56:/data/r_shared 19G 1.9G 16G 11% /data/r_lican [测试] [server:10.0.0.56] #只读目录创建文件 [root@server ~]# touch /data/r_shared/read.txt [root@server ~]# ll /data/r_shared/ total 0 -rw-r--r-- 1 root root 0 May 21 07:24 read.txt [root@server ~]# ll /data/w_shared/ total 0
[client01:10.0.0.57] #读写目录操作文件、只读目录不能操作 [root@client01 ~]# touch /data/b_w/write.txt [root@client01 ~]# touch /data/b_r/read.txt touch: cannot touch `/data/b_r/read.txt': Read-only file system [root@client01 ~]# ll /data/b_r/ total 0 -rw-r--r-- 1 root root 0 May 21 07:24 read.txt [client02:10.0.0.58] #读写目录操作文件、删除client01创建的文件、只读目录不能操作 [root@client02 ~]# touch /data/w_lican/write_0.txt [root@client02 ~]# ll /data/w_lican/ total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 May 21 2013 write.txt -rw-r--r-- 1 nfsnobody nfsnobody 0 May 21 2013 write_0.txt [root@client02 ~]# touch /data/r_lican/read_0.txt touch: cannot touch `/data/r_lican/read_0.txt': Read-only file system [root@client02 ~]# rm /data/w_lican/write.txt rm: remove regular empty file `/data/w_lican/write.txt'? y [root@client02 ~]# ll /data/w_lican/ total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 May 21 2013 write_0.txt [root@client02 ~]# touch /data/w_lican/write_1.txt [root@client02 ~]# ll /data/w_lican/ total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 May 21 2013 write_0.txt -rw-r--r-- 1 nfsnobody nfsnobody 0 May 21 2013 write_1.txt [client01:10.0.0.57] #读写目录操作文件、删除client01创建的文件、只读目录不能操作 [root@client01 ~]# ll /data/b_w/ total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 May 21 07:27 write_0.txt -rw-r--r-- 1 nfsnobody nfsnobody 0 May 21 07:30 write_1.txt [root@client01 ~]# rm /data/b_w/write_0.txt rm: remove regular empty file `/data/b_w/write_0.txt'? y [root@client01 ~]# ll /data/b_w/ total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 May 21 07:30 write_1.txt [server:10.0.0.56] #读写目录操作文件、只读目录只有server端可操作 [root@server ~]# ll /data/w_shared/ total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 May 21 07:30 write_1.txt [root@server ~]# rm /data/w_shared/write_1.txt rm: remove regular empty file `/data/w_shared/write_1.txt'? y [root@server ~]# ll /data/w_shared/ total 0 [root@server ~]# touch /data/w_shared/write_2.txt [root@server ~]# ll /data/w_shared/ total 0 -rw-r--r-- 1 root root 0 May 21 07:34 write_2.tx 注意:当前用户是root用户,建立的文件属主为root,nfsnobody用户只读权限,无法操作,不建议在NFS server端在共享读目录内写文件(可切换至nfsnobody用户写)
测试完毕!!!
1.3设置开机自动启动挂载
1.3.1 服务设置开机自启动
方法1:将启动命令写入到/etc/rc.local文件内(server端需要启动nfs和portmap;client端启动portmap即可) cat >> /etc/rc.local << eof /etc/init.d/portmap start /etc/init.d/nfs start eof [root@Center-A ~]# tail -2 /etc/rc.local /etc/init.d/portmap start /etc/init.d/nfs start [root@Client-B ~]# tail -1 /etc/rc.local /etc/init.d/portmap start [root@Client-C ~]# tail -1 /etc/rc.local /etc/init.d/portmap start
方法2:通过chkconfig将服务加入开机启动项 [root@Center-A ~]# chkconfig --list |grep 3:on crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off syslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@Center-A ~]# chkconfig portmap on [root@Center-A ~]# chkconfig nfs on [root@Center-A ~]# chkconfig --list |grep 3:on crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off nfs 0:off 1:off 2:on 3:on 4:on 5:on 6:off portmap 0:off 1:off 2:on 3:on 4:on 5:on 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off syslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
1.3.2 客户端自动挂载设置
方法1:将磁盘挂载信息写入到/etc/fstab文件中 [root@Client-B ~]# tail -2 /etc/fstab 10.0.0.56:/w_shared /data/b_w ext3 defaults 0 0 10.0.0.56:/r_shared /data/b_r ext3 defaults 0 0
方法2:将mount信息写入到/etc/rc.local中 [root@Client-B ~]# echo "mount -t nfs 10.0.0.56:/data/w_shared /data/b_w">>/etc/rc.local [root@Client-B ~]# echo "mount -t nfs 10.0.0.56:/data/r_shared /data/b_r">>/etc/rc.local [root@Client-B ~]# tail -2 /etc/rc.local mount -t nfs 10.0.0.56:/data/w_shared /data/b_w mount -t nfs 10.0.0.56:/data/r_shared /data/b_r

浙公网安备 33010602011771号