NFS是Network File System的简写,即网络文件系统. NFS允许一个系统在网络上与他人共享目录和文件。通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件。NFS服务是C/S架构的服务。
首先安装NFS服务
1.在安装nfs之前有一个重要的服务必须已经安装在centos5.5中叫做portmap,但是在6.x以后的版本中被称作为rpcbind,系统默认已经安装了这个服务。
[root@localhost ~]# rpm -q portmap
portmap-4.0-65.2.2.1
portmap-4.0-65.2.2.1
如果没有安装,可以直接使用yum install postmap* -y安装即可
2.安装NFS服务
系统默认也是安装了这个服务的
[root@localhost ~]# rpm -q nfs-utils
nfs-utils-1.0.9-24.el5
nfs-utils-1.0.9-24.el5
如果没有安装,yum install nfs-utils -y
3.启动服务
这里我们要注意一下启动顺序,首先要启动portmap服务,然后才能启动nfs服务。因为nfs启动过程以后会到portmap这里注册,得到一些nfs所需要的服务的一些端口(比如rpc.nfsd、rpc.mountd、rpc.lockd、rcp.statd等等)。
service portmap restart 启动portmap服务
*service rpcbind restart (在centos6.x中使用该命令)
portmap-4.0-65.2.2.1
[root@localhost ~]# service portmap restart
停止 portmap: [确定]
启动 portmap: [确定]
停止 portmap: [确定]
启动 portmap: [确定]
[root@localhost ~]# service nfslock restart
停止 NFS 锁定: [确定]
停止 NFS statd: [确定]
启动 NFS statd: [确定]
停止 NFS 锁定: [确定]
停止 NFS statd: [确定]
启动 NFS statd: [确定]
[root@localhost ~]# service nfs restart
关闭 NFS mountd: [确定]
关闭 NFS 守护进程: [确定]
关闭 NFS 服务: [确定]
启动 NFS 服务: [确定]
启动 NFS 守护进程: [确定]
启动 NFS mountd: [确定]
关闭 NFS mountd: [确定]
关闭 NFS 守护进程: [确定]
关闭 NFS 服务: [确定]
启动 NFS 服务: [确定]
启动 NFS 守护进程: [确定]
启动 NFS mountd: [确定]
ok,所有服务都已经启动了。
4.分配固定端口
由于portmap(rpcbind)是随机来分配端口号的,这对于开启防火墙的服务器来说很难管理,所以我们需要给nfs的各个服务设置一些固定的端口号,这里我们需要修改/etc/sysconfig/nfs这个文件,这里修改几个主要的端口即可
LOCKD_TCPPORT=4000
LOCKD_UDPPORT=4000
MOUNTD_PORT=4001
STATD_PORT=4002
LOCKD_UDPPORT=4000
MOUNTD_PORT=4001
STATD_PORT=4002
好了,修改完这些端口就可以了
5.添加iptables条目
iptables -I INPUT 1 -p tcp --dport 4000 -j ACCEPT
iptables -I INPUT 1 -p tcp --dport 4001 -j ACCEPT
iptables -I INPUT 1 -p tcp --dport 4002 -j ACCEPT
iptables -I INPUT 1 -p udp --dport 4000 -j ACCEPT
iptables -I INPUT 1 -p udp --dport 4001 -j ACCEPT
iptables -I INPUT 1 -p udp --dport 4002 -j ACCEPT
iptables -I INPUT 1 -p tcp --dport 111 -j ACCEPT #rpc服务的端口
iptables -I INPUT 1 -p udp --dport 111 -j ACCEPT #rpc服务的端口
iptables -I INPUT 1 -p tcp --dport 2049 -j ACCEPT #NFS服务自身的端口
iptables -I INPUT 1 -p tcp --dport 4001 -j ACCEPT
iptables -I INPUT 1 -p tcp --dport 4002 -j ACCEPT
iptables -I INPUT 1 -p udp --dport 4000 -j ACCEPT
iptables -I INPUT 1 -p udp --dport 4001 -j ACCEPT
iptables -I INPUT 1 -p udp --dport 4002 -j ACCEPT
iptables -I INPUT 1 -p tcp --dport 111 -j ACCEPT #rpc服务的端口
iptables -I INPUT 1 -p udp --dport 111 -j ACCEPT #rpc服务的端口
iptables -I INPUT 1 -p tcp --dport 2049 -j ACCEPT #NFS服务自身的端口
service iptables save
6.重启portmap服务,查看所分配的端口
[root@localhost ~]# service portmap restart
停止 portmap: [确定]
启动 portmap: [确定]
停止 portmap: [确定]
启动 portmap: [确定]
[root@localhost ~]# rpcinfo -p localhost #查看rpc分配给本机的端口号
程序 版本 协议 端口
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 4002 status
100024 1 tcp 4002 status
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100021 1 udp 4000 nlockmgr
100021 3 udp 4000 nlockmgr
100021 4 udp 4000 nlockmgr
100021 1 tcp 4000 nlockmgr
100021 3 tcp 4000 nlockmgr
100021 4 tcp 4000 nlockmgr
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100005 1 udp 4001 mountd
100005 1 tcp 4001 mountd
100005 2 udp 4001 mountd
100005 2 tcp 4001 mountd
100005 3 udp 4001 mountd
100005 3 tcp 4001 mountd
程序 版本 协议 端口
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 4002 status
100024 1 tcp 4002 status
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100021 1 udp 4000 nlockmgr
100021 3 udp 4000 nlockmgr
100021 4 udp 4000 nlockmgr
100021 1 tcp 4000 nlockmgr
100021 3 tcp 4000 nlockmgr
100021 4 tcp 4000 nlockmgr
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100005 1 udp 4001 mountd
100005 1 tcp 4001 mountd
100005 2 udp 4001 mountd
100005 2 tcp 4001 mountd
100005 3 udp 4001 mountd
100005 3 tcp 4001 mountd
好了,这里我们所更改的端口也都按着我们的意愿分配好了。
7.NFS配置
nfs的主要配置文件是/etc/exports
/share 192.168.1.0/24(rw,sync)
共享目录 可以访问的主机范围(读写,同步)
以上是一条很简单的共享条目,具体的复杂一些的设置,大家可以google,百度一下。主要是在()内进行一些设置,当然也有允许访问的主机的范围,这里提一点,如果只允许某个主机访问的话,这里掩码要写成32,即192.168.1.211/32 ,这样写才被认作是单独的主机,如果掩码仍然为/24,nfs认为它是一个网段。
8.客户端连接
首先产看一下,要访问的主机共享的是哪个目录
[root@client ~]# showmount -e 192.168.1.230
Export list for 192.168.1.230:
/share 192.168.1.0/24
Export list for 192.168.1.230:
/share 192.168.1.0/24
接下来,将共享目录挂载到本地
[root@client ~]# mount 192.168.1.230:/share /mnt
ok,然后cd到/mnt下,你就可以访问1.230共享的nfs了。
[root@client mnt]# touch 123
[root@client mnt]# ls
123
[root@client mnt]# ls
123
转载于:https://blog.51cto.com/addam/1189435