服务端:
[root@oldboy ~]# yum install nfs-utils rpcbind -y(yum安装nfs和rpcbind软件包)
[root@oldboy ~]# rpm -aq nfs-utils rpcbind(rpm查看是否安装成功)
nfs-utils-1.2.3-64.el6.x86_64
rpcbind-0.2.0-11.el6.x86_64
[root@nfs01 ~]# /etc/init.d/rpcbind status(查看服务是否运行,注意rpcbind服务要先于nfs服务启动)
rpcbind 已停
[root@nfs01 ~]# /etc/init.d/rpcbind start(开启服务运行)
正在启动 rpcbind: [确定]
[root@nfs01 ~]# /etc/init.d/rpcbind status
rpcbind (pid 4822) 正在运行...
[root@nfs01 ~]# netstat -lntup|grep rpcbind(查看进程是否启动,可以看出端口号为111)
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 4822/rpcbind
tcp 0 0 :::111 :::* LISTEN 4822/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 4822/rpcbind
udp 0 0 0.0.0.0:757 0.0.0.0:* 4822/rpcbind
udp 0 0 :::111 :::* 4822/rpcbind
udp 0 0 :::757 :::* 4822/rpcbind
[root@nfs01 ~]# lsof -i:111(rpcbind服务端口号为111)
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 4822 rpc 6u IPv4 30169 0t0 UDP *:sunrpc
rpcbind 4822 rpc 8u IPv4 30172 0t0 TCP *:sunrpc (LISTEN)
rpcbind 4822 rpc 9u IPv6 30174 0t0 UDP *:sunrpc
rpcbind 4822 rpc 11u IPv6 30177 0t0 TCP *:sunrpc (LISTEN)
[root@nfs01 ~]# netstat -lntup|grep 111(查看端口号为111的服务为什么进程)
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 4822/rpcbind
tcp 0 0 :::111 :::* LISTEN 4822/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 4822/rpcbind
udp 0 0 :::111 :::* 4822/rpcbind
[root@nfs01 ~]# chkconfig --list rpcbind(查看开机启动项里是否开启)
rpcbind 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@nfs01 ~]# rpcinfo -p localhost(查看服务运行是否正常,为不正常,因此时nfs服务未启动)
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
[root@nfs01 ~]# /etc/init.d/nfs status(查看nfs服务是否开启)
rpc.svcgssd 已停
rpc.mountd 已停
nfsd 已停
rpc.rquotad 已停
[root@nfs01 ~]# /etc/init.d/nfs start(开启nfs服务)
启动 NFS 服务: [确定]
关掉 NFS 配额: [确定]
启动 NFS mountd: [确定]
启动 NFS 守护进程: [确定]
正在启动 RPC idmapd: [确定]
[root@nfs01 ~]# netstat -lntup|grep 2049(nfs主端口号为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@nfs01 ~]# rpcinfo -p localhost(再次查看服务运行是否正常,nfs服务启动后正常)
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 49508 mountd
100005 1 tcp 22445 mountd
100005 2 udp 56548 mountd
100005 2 tcp 18567 mountd
100005 3 udp 21822 mountd
100005 3 tcp 58356 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 5769 nlockmgr
100021 3 udp 5769 nlockmgr
100021 4 udp 5769 nlockmgr
100021 1 tcp 64110 nlockmgr
100021 3 tcp 64110 nlockmgr
100021 4 tcp 64110 nlockmgr
[root@nfs01 ~]# /etc/init.d/rpcbind restart(重启rpcbind服务后看结果)
停止 rpcbind: [确定]
正在启动 rpcbind: [确定]
[root@nfs01 ~]# rpcinfo -p localhost(nfs服务先运行,此时为不正常)
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
[root@nfs01 ~]# chkconfig --list nfs(查看nfs开机自启动)
nfs 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
[root@nfs01 ~]# chkconfig nfs on(nfs开机自启动)
[root@nfs01 ~]# chkconfig --list nfs
nfs 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@nfs01 ~]# less /etc/init.d/rpcbind (查看开机启动顺序)
#! /bin/sh
#
# rpcbind Start/Stop RPCbind
#
# chkconfig: 2345 13 87(开机启动和关机关闭顺序)
# description: The rpcbind utility is a server that converts RPC program \
# numbers into universal addresses. It must be running on the \
# host to be able to make RPC calls on a server on that machine.
#
# processname: rpcbind
# probe: true
# config: /etc/sysconfig/rpcbind
# This is an interactive program, we need the current locale
[ -f /etc/profile.d/lang.sh ] && . /etc/profile.d/lang.sh
# We can‘t Japanese on normal console at boot time, so force LANG=C.
if [ "$LANG" = "ja" -o "$LANG" = "ja_JP.eucJP" ]; then
if [ "$TERM" = "linux" ] ; then
LANG=C
fi
[root@nfs01 ~]# less /etc/init.d/nfs
#!/bin/sh
#
# nfs This shell script takes care of starting and stopping
# the NFS services.
#
# chkconfig: - 30 60(开机启动和关机关闭顺序)
# description: NFS is a popular protocol for file sharing across networks.
# This service provides NFS server functionality, which is \
# configured via the /etc/exports file.
# probe: true
# config: /etc/sysconfig/nfs
### BEGIN INIT INFO
# Provides: nfs
# Required-Start: $local_fs $network $syslog $rpcbind
# Required-Stop: $local_fs $network $syslog $rpcbind
# Default-Stop: 0 1 6
# Short-Description: Start up the NFS server sevice
# Description: NFS is a popular protocol for file sharing across networks \
# This service provides NFS server functionality, \
# which is configured via the /etc/exports file.
[root@nfs01 ~]# vi /etc/rc.local(编辑开机自启动)
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/etc/init.d/rpcbind start(rpcbind服务放在nfs服务前面启动)
/etc/init.d/nfs start
~
~
~
~
~
~
~
~
~
~
~
~
"/etc/rc.local" 9L, 268C written
[root@nfs01 ~]# ll /etc/exports(nfs服务配置文件)
-rw-r--r-- 1 root root 60 2015-11-10 15:12 /etc/exports
[root@nfs01 ~]# cat /etc/exports(默认配置为空)
[root@nfs01 ~]# vim /etc/exports
######2015-11-10 by gong######
/data 172.16.1.0/24(rw,sync)(rw为给予读、写权限,sync为同步写入到磁盘不放在缓存里)
"/etc/exports" 2L, 60C 已写入
[root@nfs01 ~]# cat /etc/exports
######2015-11-10 by gong######
/data 172.16.1.0/24(rw,sync,all_squash)(all_squash参数为所有用户压缩为统一用户)
[root@nfs01 ~]# mkdir /data(建立目录)
[root@nfs01 ~]# /etc/init.d/nfs reload(平滑重启服务等价于exportfs -r,-v参数为输出)
[root@nfs01 ~]# showmount -e 127.0.0.1(检查本机是否可以挂载,127.0.0.1可替换为localhost)
Export list for 127.0.0.1:
/data 172.16.1.0/24
[root@nfs01 ~]# mount -t nfs 172.16.1.31:/data /mnt(实际检查是否可挂载)
[root@nfs01 ~]# df -h(查看磁盘挂载及分区设备使用情况,-h为以人类可读方式查看)
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 7.2G 1.7G 5.1G 25% /
tmpfs 112M 0 112M 0% /dev/shm
/dev/sda1 93M 36M 53M 41% /boot
172.16.1.31:/data 7.2G 1.7G 5.1G 25% /mnt(已实际挂载,强制卸载umount -lf /mnt)
[root@nfs01 ~]# cat /var/lib/nfs/etab(查看默认挂载属性)
/data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash)(挂载为UID和GID为65534的用户和组)
[root@nfs01 ~]# grep "65534" /etc/passwd(过滤UID和GID为65534的用户和组)
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin(过滤结果为nfsnobody)
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data(更改属主和属组为nfsnobody,-R为递归)
[root@nfs01 ~]# ll -d /data/
drwxr-xr-x 2 nfsnobody nfsnobody 4096 2015-11-10 15:23 /data/
客户端:
[root@oldboy ~]# yum install nfs-utils rpcbind -y(yum安装nfs和rpcbind软件包)
[root@oldboy ~]# rpm -aq nfs-utils rpcbind(rpm查看是否安装成功)
nfs-utils-1.2.3-64.el6.x86_64
rpcbind-0.2.0-11.el6.x86_64
[root@nfs01 ~]# /etc/init.d/rpcbind status(查看服务是否运行,注意rpcbind服务要先于nfs服务启动)
rpcbind 已停
[root@nfs01 ~]# /etc/init.d/rpcbind start(开启服务运行)
正在启动 rpcbind: [确定]
[root@nfs01 ~]# /etc/init.d/rpcbind status
rpcbind (pid 4822) 正在运行...
[root@nfs01 ~]# chkconfig rpcbind on(rpcbind开机自启动)
[root@nfs01 ~]# chkconfig --list rpcbind(查看开机启动项里是否开启)
rpcbind 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@web01 ~]# showmount -e 172.16.1.31(客户端虽然不使用nfs服务,但showmount命令存在于nfs软件包里)
Export list for 172.16.1.31:
/data 172.16.1.0/24
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 7.2G 1.7G 5.2G 25% /
tmpfs 112M 0 112M 0% /dev/shm
/dev/sda1 93M 36M 53M 41% /boot
172.16.1.31:/data 7.2G 1.7G 5.1G 25% /mnt
[root@web01 ~]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
172.16.1.31:/data on /mnt type nfs (rw,vers=4,addr=172.16.1.31,clientaddr=172.16.1.8)(挂载信息)
[root@web01 ~]# cat /proc/mounts
rootfs / rootfs rw 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
devtmpfs /dev devtmpfs rw,relatime,size=99000k,nr_inodes=24750,mode=755 0 0
devpts /dev/pts devpts rw,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /dev/shm tmpfs rw,relatime 0 0
/dev/sda3 / ext4 rw,relatime,barrier=1,data=ordered 0 0
/proc/bus/usb /proc/bus/usb usbfs rw,relatime 0 0
/dev/sda1 /boot ext4 rw,relatime,barrier=1,data=ordered 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
172.16.1.31:/data/ /mnt nfs4 rw,relatime,vers=4,rsize=32768,wsize=32768,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.8,minorversion=0,local_lock=none,addr=172.16.1.31 0 0(挂载信息)
[root@web01 ~]# vi /etc/rc.local(编辑开机自启动)
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/etc/init.d/rpcbind start(开机启动服务)
mount -t nfs 172.16.1.31:/data /mnt(开机挂载,不能放在fstab,磁盘挂载优先网络启动)
~
~
~
~
~
~
~
~
~
~
~
~
"/etc/rc.local" 9L, 282C
本文出自 “DNA种子” 博客,请务必保留此出处http://whyseed.blog.51cto.com/10827449/1718559
原文地址:http://whyseed.blog.51cto.com/10827449/1718559