1.什么是NFS?
NFS,全名叫Network File System,中文叫网络文件系统,是Linux、UNIX系统的分布式文件系统的一个组成部分,可实现在不同网络上共享远程文件系统。NFS由Sun公司开发,目前已经成为文件服务的一种标准之一。其最大的功能就是可以通过网络,让不同操作系统的计算机可以共享数据,所以可以把NFS看做是一个文件服务器。NFS缺点是其读写性能比本地硬盘要差一些。
2.使用yum安装nfs-utils、rpcbind
实验环境
centos 6.6
nfs-server 10.0.0.130
lamp-client 10.0.0.137
lnmp-client 10.0.0.138
3.nfs服务端软件安装
[root@nfs-server ~]# yum -y install nfs-utils rpcbind
创建共享文件夹
[root@nfs-server ~]# mkdir -p {/data/r_shared,/data/w_shared}
查看文件是否创建成功
[root@nfs-server data]# ll
总用量 8
drwxr-xr-x 2 nfsnobody root 4096 4月 23 15:13 r_shared
drwxr-xr-x 2 nfsnobody root 4096 4月 23 15:24 w_shared
[root@nfs-server data]#
给文件授权
[root@nfs-server data]# chown -R nfsnobody r_shared w_shared
[root@nfs-server data]# ll
总用量 8
drwxr-xr-x 2 nfsnobody root 4096 4月 23 15:13 r_shared
drwxr-xr-x 2 nfsnobody root 4096 4月 23 15:24 w_shared
[root@nfs-server data]#
4.启动rpcbind、nfs
[root@nfs-server ~]# /etc/init.d/rpcbind start
[root@nfs-server ~]# /etc/init.d/nfs start
[root@nfs-server ~]# /etc/init.d/nfs status
rpc.svcgssd 已停
rpc.mountd (pid 4759) 正在运行...
nfsd (pid 4775 4774 4773 4772 4771 4770 4769 4768) 正在运行...
rpc.rquotad (pid 4754) 正在运行...
[root@nfs-server ~]# /etc/init.d/rpcbind status
rpcbind (pid 1656) 正在运行...
[root@nfs-server ~]#
[root@nfs ~]# lsof -i :111
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 2664 rpc 6u IPv4 15806 0t0 UDP *:sunrpc
rpcbind 2664 rpc 8u IPv4 15809 0t0 TCP *:sunrpc (LISTEN)
rpcbind 2664 rpc 9u IPv6 15811 0t0 UDP *:sunrpc
rpcbind 2664 rpc 11u IPv6 15814 0t0 TCP *:sunrpc (LISTEN)
[root@nfs ~]# netstat -tunpl|grep "111"
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 2664/rpcbind
tcp 0 0 :::111 :::* LISTEN 2664/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 2664/rpcbind
udp 0 0 :::111 :::* 2664/rpcbind
[root@nfs ~]#
[root@nfs ~]# chkconfig --list rpcbind 查看开机是否自启
rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@nfs ~]#
[root@nfs ~]# /etc/init.d/nfs status
rpc.svcgssd 已停
rpc.mountd is stopped
nfsd is stopped
rpc.rquotad is stopped
[root@nfs ~]# /etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
正在启动 RPC idmapd: [确定]
[root@nfs ~]# netstat -tunpl |grep 2049
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp 0 0 :::2049 :::* LISTEN -
udp 0 0 0.0.0.0:2049 0.0.0.0:* -
udp 0 0 :::2049 :::* -
[root@nfs ~]# lsof -i:2049
[root@nfs ~]# rpcinfo -p localhost
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
100005 1 udp 48501 mountd
100005 1 tcp 36022 mountd
100005 2 udp 43391 mountd
100005 2 tcp 39405 mountd
100005 3 udp 49218 mountd
100005 3 tcp 41793 mountd
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 2 tcp 2049 nfs_acl
100227 3 tcp 2049 nfs_acl
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 2 udp 2049 nfs_acl
100227 3 udp 2049 nfs_acl
100021 1 udp 59606 nlockmgr
100021 3 udp 59606 nlockmgr
100021 4 udp 59606 nlockmgr
100021 1 tcp 56597 nlockmgr
100021 3 tcp 56597 nlockmgr
100021 4 tcp 56597 nlockmgr
[root@nfs ~]#
5.修改nfs配置文件
[root@nfs-server ~]# vi /etc/exports
/data/w_shared 10.0.0.0/24(rw,sync,all_squash)
/data/r_shared 10.0.0.0/24(ro)
注:下面介绍一些配置文件中常用的参数含义,以及客户端的IP地址的格式。
rw:可读写权限。
ro:只读权限。
no_root_squash:当登录NFS服务器主机使用共享目录的使用者是root时,其权限将被转换成为匿名使用者,通常它的UID与GID都会变成nobody身份。
root_squash;如果登录NFS主机使用共享目录的使用者是root,那么对于这个共享的目录来说,它具有root的权限,这样会涉及到安全性的问题。
all_squash:忽略登录NFS使用者的身份,其身份都会被转换为匿名使用者,通常即nobody。
anonuid:通常为nobody,也可以自行设定这个UID的值,UID必须存在于/etc/passwd中。
anongid:同anonuid,但是变为Group ID。
sync:同步写入资料到内存与硬盘中。
async:资料会先暂存于内存中,而非直接写入硬盘。
[root@nfs-server ~]#exportfs -r 让其平滑生效
6.nfs客服端配置
[root@lamp01 ~]# yum -y install nfs-utils rpcbind
创建挂载点目录
[root@lamp01 ~]# mkdir -p {/data/b_r,/data/b_w}
查看文件是否创建成功
[root@lamp01 ~]# ll /data
总用量 8
drwxr-xr-x 2 nfsnobody root 4096 4月 23 15:13 b_r
drwxr-xr-x 2 nfsnobody root 4096 4月 23 15:24 b_w
测试挂载
[root@lamp01 ~]# showmount -e 10.0.0.130
Export list for 10.0.0.130:
/data/r_shared 10.0.0.0/24
/data/w_shared 10.0.0.0/24
[root@lamp01 ~]#
挂载
[root@lamp01 ~]#mount.nfs 10.0.0.130:/data/r_shared /data/b_r
[root@lamp01 ~]#mount -t nfs 10.0.0.130:/data/w_shared /data/b_w
查看是否挂载成功
[root@lamp01 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 18244476 1582428 15728624 10% /
tmpfs 515244 0 515244 0% /dev/shm
/dev/sda1 194241 25657 158344 14% /boot
10.0.0.130:/data/r_shared
18244480 1582464 15728640 10% /data/b_r
10.0.0.130:/data/w_shared
18244480 1582464 15728640 10% /data/b_w
[root@lamp01 ~]#
同理在另个客服端使用相同的方法进行挂载即可。
一、NFS服务常见故障排查:
NFS服务出现了故障,主要从以下几个方面检查原因:
(1)检查NFS客户机和服务器的负荷是否太高,Server和Client之间的网络是否正常;
(2)检查/etc/exports文件的正确性;
(3)必要时重启NFS和portmap服务;
(4)运行下列命令重新启动portmap和NFS:
# /etc/init.d/portmap restart (先启动portmap或rpcbind)
# /etc/init.d/nfs restart
# /etc/init.d/rpcbind restart (在RHEL/CentOS 6.x里面)
# chkconfig portmap on
# chkconfig nfs on
# chkconfig rpcbind on (在RHEL/CentOS 6.x里面)
二、NFS常见故障解决方法:
1、The rpcbind failure error
故障现象:
nfs mount: server1:: RPC: Rpcbind failure
RPC: Timed Out
nfs mount: retrying: /mntpoint
原因:
第一,可能因为客户机的hosts文件中存在错误的ip地址、主机名或节点名组合;
第二,服务器因为过载而暂时停止服务。2、The server not responding error
现象:
NFS server server2 not responding, still trying
原因:
第一,网络不通,用ping命令检测一下。
第二,服务器关机。3、The NFS client fails a reboot error
现象:
启动客户机后停住了,不断显示如下提示信息:
Setting default interface for multicast: add net 224.0.0.0: gateway:
client_node_name.
原因:
在etc/vfstab的mount选项中使用了fg而又无法成功mount服务器上的资源,改成bg或将该行注释掉,直到服务器可用为止。4、The service not responding error
现象:
nfs mount: dbserver: NFS: Service not responding
nfs mount: retrying: /mntpoint
原因:
第一,当前级别不是级别3,用who -r查看,用init 3切换。
第二,NFS Server守护进程不存在,用ps -ef | grep nfs检查,用/etc/init.d/nfs start启动。5、The program not registered error
现象:
nfs mount: dbserver: RPC: Program not registered
nfs mount: retrying: /mntpoint
原因:
第一,当前级别不是级别3。
第二,mountd守护进程没有启动,用/etc/init.d/nfs脚本启动NFS守护进程。
第三,看/etc/dfs/dfstab中的条目是否正常。6、The stale file handle error
现象:
stale NFS file handle
原因:
服务器上的共享资源移动位置了,在客户端使用umount和mount重新挂接就可以了。7、The unknown host error
现象:
nfs mount: sserver1:: RPC: Unknown host
原因:
hosts文件中的内容不正确。8、The mount point error
现象:
mount: mount-point /DS9 does not exist.
原因:
该挂接点在客户机上不存在,注意检查命令行或/etc/vfstab文件中相关条目的拼写。9、The no such file error
现象:
No such file or directory.
原因:
该挂接点在服务器上不存在,注意检查命令行或/etc/vfstab文件中相关条目的拼写。10、No route to host
错误现象:
# mount 10.10.11.211:/opt/data/xmldb /c2c-web1/data/xmldb -t nfs -o rw
mount: mount to NFS server ’10.10.11.211′ failed: System Error: No route to host.原因:
防火墙被打开,关闭防火墙。
这个原因很多人都忽视了,如果开启了防火墙(包括iptables和硬件防火墙),NFS默认使用111端口,我们先要检测是否打开了这个端口,还要检查TCP_Wrappers的设定。11、Not owner
现象:
# mount -F nfs -o rw 10.10.2.3:/mnt/c2c/data/resinfo2 /data/data/resinfo2
nfs mount: mount: /data/data/resinfo2: Not owner原因:
这是Solaris 10版本挂载较低版本nfs时报的错误。解决:
需要用-o vers=3参数示例:
# mount -F nfs -o vers=3 10.10.2.3:/mnt/c2c/data/resinfo2 /data/data/resinfo212、RPC: Program not registered & retrying
现象:
nfs mount: 10.10.2.3: : RPC: Program not registered
nfs mount: retrying: /data/data/resinfo2原因:
没有启动NFS共享端服务。解决:需要重新启动share端的NFS服务,
Linux:
mount: RPC: Program not registered
# /etc/init.d/nfs restartSolaris:
mount: RPC: Program not registered
# /etc/rc.d/init.d/nfs restart13、can’t contact portmapper: RPC: Remote system error – Connection refused
现象:
# exportfs -a
can’t contact portmapper: RPC: Remote system error – Connection refused原因:
出现这个错误信息是由于server端的portmap没有启动。解决:
# /etc/init.d/portmap start
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
分类: LINUX
Linux系统有7个运行级别(runlevel)
运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
运行级别2:多用户状态(没有NFS)
运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式
运行级别4:系统未使用,保留
运行级别5:X11控制台,登陆后进入图形GUI模式
运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动
运行级别的原理:
1。在目录/etc/rc.d/init.d下有许多服务器脚本程序,一般称为服务(service)
2。在/etc/rc.d下有7个名为rcN.d的 目录,对应系统的7个运行级别
3。rcN.d目录下都是一些符号链接文件,这些链接文件都指向init.d目录下的service脚本文件,命名规则为K+nn+服务名或S+nn+服务名,其中nn为两位数字。
4。系统会根据指定的运行级别进入对应的rcN.d目录,并按照文件名顺序检索目录下的链接文件
对于以K开头的文件,系统将终止对应的服务
对于以S开头的文件,系统将启动对应的服务
5。查看运行级别用:runlevel
6。进入其它运行级别用:init N
7。另外init0为关机,init 6为重启系统
由于现在的Linux系统安装完后就运行在第5个级别,即系统启动后直接进入图形界面,而不用在字符模式下登录后用startx或者xinit 来起动图形界面。建议在系统安装完成后把系统的默认运行等级设置在第3级,在字符终端登录后,再手工输入 startx 命令起动图形界面。可以用如下的方法修改:
用文本编辑器修改 /etc/inittab文件,把代码:
id:5:initdefault:这一行,修改成
代码:
id:3:initdefault:保存后就reboot重起,系统就默认起动到字符界面。不同运行级别之间的差别的在于系统默认起动的服务的不同,如运行级别3默认不启动X图形界面服务,而运行级别5 却默认起动。本质上是没有区别的,更无所谓不同级别间功能强弱的问题。用户完全可自给定义不同级别的默认服务。在任何运行级别,用户都可用init 命令来切换到其他运行级别。
由于ubuntu系统没有inittab文件。其配置参考:http://blog.chinaunix.net/u2/82530/showart_2033617.html
具体我也没试过。