NFS简介
NFS是Network File System的简写,即网络文件系统.
网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件。通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件。
NFS好处
以下是NFS最显而易见的好处:
1.本地工作站使用更少的磁盘空间,因为通常的数据可以存放在一台机器上而且可以通过网络访问到。
2.用户不必在每个网络上机器里头都有一个home目录。Home目录 可以被放在NFS服务器上并且在网络上处处可用。
3.诸如软驱,CDROM,和 Zip(是指一种高储存密度的磁盘驱动器与磁盘)之类的存储设备可以在网络上面被别的机器使用。这可以减少整个网络上的可移动介质设备的数量。
NFS组成
NFS至少有两个主要部分:一台服务器和一台(或者更多)客户机。客户机远程访问存放在服务器上的数据。为了正常工作,一些进程需要被配置并运行。
实际应用
NFS 有很多实际应用。下面是比较常见的一些:
1.多个机器共享一台CDROM或者其他设备。这对于在多台机器中安装软件来说更加便宜跟方便。
2.在大型网络中,配置一台中心 NFS 服务器用来放置所有用户的home目录可能会带来便利。这些目录能被输出到网络以便用户不管在哪台工作站上登录,总能得到相同的home目录。
3.几台机器可以有通用的/usr/ports/distfiles 目录。这样的话,当您需要在几台机器上安装port时,您可以无需在每台设备上下载而快速访问源码。
案例精讲NFS:
[root@gjp99 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:2A:5C:13
inet addr:192.168.10.99 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2a:5c13/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:373 errors:0 dropped:0 overruns:0 frame:0
TX packets:360 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:33572 (32.7 KiB) TX bytes:28950 (28.2 KiB)
Interrupt:67 Base address:0x2000
[root@gjp99 ~]# rpcinfo –p //查看本地的
program vers proto port
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 867 status
100024 1 tcp 870 status
[root@gjp99 ~]# rpcinfo -p 192.168.10.9 //查看远程的!
program vers proto port
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 900 status
100024 1 tcp 903 status
[root@gjp99 ~]# mkdir /public
[root@gjp99 ~]# cd /public
[root@gjp99 public]# ll
total 0
touch个文件gjp,便于顾客访问时作为参考!
[root@gjp99 public]# echo "guo jiping"> gjp
[root@gjp99 public]# ll
total 4
-rw-r--r-- 1 root root 11 Aug 3 17:07 gjp
[root@gjp99 ~]# man 5 exports
General Options
exportfs understands the following export options:
secure This option requires that requests originate on an internet port less than
IPPORT_RESERVED (1024). This option is on by default. To turn it off, specify
insecure.
rw Allow both read and write requests on this NFS volume. The default is to dis-
allow any request which changes the filesystem. This can also be made
explicit by using the ro option.
async This option allows the NFS server to violate the NFS protocol and reply to
requests before any changes made by that request have been committed to sta-
ble storage (e.g. disc drive).
Using this option might improve performance with version 2 only, but at the
cost that an unclean server restart (i.e. a crash) can cause data to be lost
or corrupted.
sync Reply to requests only after the changes have been committed to stable stor-
age (see async above).
[root@gjp99 ~]# vim /etc/exports
[root@gjp99 ~]# cat /etc/exports
/public 192.168.10.0/24(ro)
[root@gjp99 ~]# rpm -qa |grep nfs
nfs-utils-1.0.9-42.el5
nfs-utils-lib-1.0.8-7.6.el5
[root@gjp99 ~]# rpm -ql nfs-utils |less
/etc/rc.d/init.d/nfs 控制脚本的名字
[root@gjp99 ~]# chkconfig --list |grep nfs 查看nfs服务!
nfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nfslock 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@gjp99 ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS daemon: [ OK ]
Starting NFS mountd: [ OK ]
发现本地多了好多东西!
[root@gjp99 ~]# rpcinfo –p
program vers proto port
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 867 status
100024 1 tcp 870 status
100011 1 udp 903 rquotad
100011 2 udp 903 rquotad
100011 1 tcp 906 rquotad
100011 2 tcp 906 rquotad
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100021 1 udp 40890 nlockmgr
100021 3 udp 40890 nlockmgr
100021 4 udp 40890 nlockmgr
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100021 1 tcp 51129 nlockmgr
100021 3 tcp 51129 nlockmgr
100021 4 tcp 51129 nlockmgr
100005 1 udp 930 mountd
100005 1 tcp 933 mountd
100005 2 udp 930 mountd
100005 2 tcp 933 mountd
100005 3 udp 930 mountd
100005 3 tcp 933 mountd
[root@gjp99 ~]# chkconfig nfs on
必须导出
[root@gjp99 ~]# exportfs –rv // r可译为导出,v为详细信息!
exporting 192.168.10.0/24:/public
测试:
192.168.10. 0/24的客户机192.168.10.9访问
[root@mail ~]# rpcinfo –p //先查看是否有端口映射!
program vers proto port
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 900 status
100024 1 tcp 903 status
[root@mail ~]# showmount -e 192.168.10.99 //查看服务器上的共享列表
Export list for 192.168.10.99:
/public 192.168.10.0/24
创建挂载点:
[root@mail ~]# mkdir /mnt/nfs
[root@mail ~]# mount 192.168.10.99:/public /mnt/nfs //手动挂载
[root@mail ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.5G 2.3G 6.7G 26% /
/dev/sda1 99M 12M 83M 12% /boot
tmpfs 506M 0 506M 0% /dev/shm
192.168.10.99:/public
9.5G 2.3G 6.8G 26% /mnt/nfs
[root@mail ~]# cd /mnt/nfs
[root@mail nfs]# ll //看到服务器上的共享文件
total 4
-rw-r--r-- 1 root root 11 Aug 3 17:07 gjp
[root@mail nfs]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.10.99:/public on /mnt/nfs type nfs (rw,addr=192.168.10.99)
如何做到开机自动挂载?
[root@mail nfs]# pwd 查看当前目录
/mnt/nfs
[root@mail nfs]# cd 离开要卸载的目录
[root@mail ~]# umount /mnt/nfs/ 卸载光盘
[root@mail ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
[root@mail ~]# vim /etc/fstab
192.168.10.99:/public /mnt/nfs nfs ro,soft,intr 0 0
[root@mail ~]# mount -a
[root@mail ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.10.99:/public on /mnt/nfs type nfs (ro,soft,intr,addr=192.168.10.99)
自动挂载:
[root@mail ~]# rpm -qa |grep auto
automake14-1.4p6-13
autoconf-2.59-12
automake-1.9.6-2.1
autofs-5.0.1-0.rc2.131.el5
automake17-1.7.9-7
automake15-1.5-16
automake16-1.6.3-8
[root@mail ~]# chkconfig --list |grep auto
autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@mail ~]# vim /etc/auto.master
10 /mnt/nfs /etc/auto.nfs --timeout=60 //1分钟之内如果没有操作,则自动挂断
[root@mail ~]# ll /etc/auto*
-rw------- 1 root root 2726 Jul 29 2009 /etc/autofs_ldap_auth.conf
-rw-r--r-- 1 root root 756 Sep 5 14:17 /etc/auto.master
-rw-r--r-- 1 root root 581 Jul 29 2009 /etc/auto.misc
-rwxr-xr-x 1 root root 1292 Jul 29 2009 /etc/auto.net
-rwxr-xr-x 1 root root 715 Jul 29 2009 /etc/auto.smb
[root@mail ~]# cp -p /etc/auto.misc /etc/auto.nfs
[root@mail ~]# vim /etc/auto.nfs
server1 -ro,soft,intr 192.168.10.99:/public
[root@mail ~]# service autofs restart 启动服务
Stopping automount: [ OK ]
Starting automount: [ OK ]
[root@mail ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
[root@mail ~]# cd /mnt/nfs
[root@mail nfs]# ll
total 0
[root@mail nfs]# cd server1 进入此目录,才可以挂载!
[root@mail server1]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.10.99:/public on /mnt/nfs/server1 type nfs (ro,soft,intr,addr=192.168.10.99)
[root@mail ~]# mount //1分钟过后,查看挂载点消失
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
服务器上配置:
[root@gjp99 public]# mkdir /gjp
[root@gjp99 public]# cat /etc/exports
/public 192.168.10.0/24(ro)
/gjp 192.168.10.0/24(rw,sync) //增加此行
注意:子网掩码后面不能有空格!
[root@gjp99 public]# ll -d /gjp
drwxr-xr-x 2 root root 4096 Aug 3 18:12 /gjp
[root@gjp99 public]# chmod o+wt /gjp
[root@gjp99 ~]# ll -d /gjp
drwxr-xrwt 2 root root 4096 Oct 15 10:32 /gjp
[root@gjp99 ~]# exportfs -rv
exporting 192.168.10.0/24:/public
exporting 192.168.10.0/24:/gjp
客户端上:
[root@mail ~]# mkdir /mnt/gjp
[root@mail ~]# mount 192.168.10.99:/gjp /mnt/gjp
[root@mail ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.10.99:/gjp on /mnt/gjp type nfs (rw,addr=192.168.10.99)
[root@mail ~]# cd /mnt/gjp
[root@mail gjp]# ll
total 0
[root@mail gjp]# touch gjp
[root@mail gjp]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 15 2012 gjp
服务器上:
man exports
[root@gjp99 ~]# vim /etc/exports
/public 192.168.10.0/24(ro)
/gjp 192.168.10.0/24(rw,no_root_squash,sync)增加参数:取消映射关系
[root@gjp99 ~]# service nfs restart
Shutting down NFS mountd: [ OK ]
Shutting down NFS daemon: [ OK ]
Shutting down NFS quotas: [ OK ]
Shutting down NFS services: [ OK ]
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS daemon: [ OK ]
Starting NFS mountd: [ OK ]
[root@gjp99 ~]# service autofs restart
Stopping automount: [ OK ]
Starting automount: [ OK ]
客户端测试:
[root@mail ~]# umount /mnt/gjp
[root@mail ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.10.99:/public on /mnt/nfs type nfs (ro,soft,intr,addr=192.168.10.99)
[root@mail ~]# mount 192.168.10.99:/gjp /mnt/gjp
[root@mail ~]# cd /mnt/gjp
[root@mail gjp]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 15 2012 gjp
[root@mail gjp]# touch guolu
[root@mail gjp]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 15 2012 gjp
-rw-r--r-- 1 root root 0 Oct 15 2012 guolu
取消映射后:发现root用户,更不安全!还是用映射的好!
本文转自 gjp0731 51CTO博客,原文链接:http://blog.51cto.com/guojiping/1026465