首先说一下什么是NFS,其实就是Network File System的缩写,是sun公司开发的。能够实现不同的机器,不同的操作系统乐意彼此共享文件。在我前面的文章中有提到Samba,它也是一种文件共享服务器。不同的是,NFS实现的是linux客户机之间的文件共享,而Samba主要是为了实现windows和linux之间的文件共享,前者相对简单,后者相对复杂,但是复杂必然有复杂的道理。
下面我介绍下如何搭建NFS服务器,以及客户机如何能够实现文件共享:
场景
某单位需要配置 NFS 服务器, 为另外一台运行 Oracle 数据库的 Linux 服务器提供备份存储。
主机名 IP 地址
nfs1.abc.local 192.168.1.241
db1.abc.local 192.168.1.242
2. 实验环境
2.1. 操作系统安装
# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.4 (Santiago)
# uname -a
Linux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29
11:47:41 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
2.2. 服务器基本配置
修改 IP 地址。 (注意:根据您的实现环境进行配置)
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.1.241
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
修改主机名
# vi /etc/sysconfig/network
NETWORKING=yes
#HOSTNAME=localhost.localdomain
HOSTNAME=nfs1.abc.local
# service network restart
为了方便实验,将防火墙关闭。
# service iptables stop
# chkconfig iptables off
同时关闭 selinux。
# vi /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. #SELINUX=enforcing SELINUX=disabled # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted |
重新启动以便生效。
# reboot
3. NFS 服务器安装与配置
3.1. 安装 NFS 服务器组件
3.1.1. 方法 1:通过 RPM 来进行安装
# mkdir /mnt/cdrom # mount /dev/cdrom /mnt/cdrom/ mount: block device /dev/sr0 is write-protected, mounting read-only # cd /mnt/cdrom/Packages/ # ls nfs* nfs4-acl-tools-0.3.3-6.el6.x86_64.rpm nfs-utils-1.2.3-36.el6.x86_64.rpm nfs-utils-lib-1.1.5-6.el6.i686.rpm nfs-utils-lib-1.1.5-6.el6.x86_64.rpm # rpm -ivh nfs-utils-1.2.3-36.el6.x86_64.rpm error: Failed dependencies: keyutils >= 1.4-4 is needed by nfs-utils-1:1.2.3-36.el6.x86_64 libevent is needed by nfs-utils-1:1.2.3-36.el6.x86_64 libevent-1.4.so.2()(64bit) is needed by nfs-utils-1:1.2.3-36.el6.x86_64 libgssglue is needed by nfs-utils-1:1.2.3-36.el6.x86_64 libgssglue.so.1()(64bit) is needed by nfs-utils-1:1.2.3-36.el6.x86_64 libgssglue.so.1(libgssapi_CITI_2)(64bit) is needed by nfs-utils-1:1.2.3-36.el6.x86_64 libnfsidmap.so.0()(64bit) is needed by nfs-utils-1:1.2.3-36.el6.x86_64 libtirpc is needed by nfs-utils-1:1.2.3-36.el6.x86_64 libtirpc.so.1()(64bit) is needed by nfs-utils-1:1.2.3-36.el6.x86_64 nfs-utils-lib >= 1.1.0-3 is needed by nfs-utils-1:1.2.3-36.el6.x86_64 rpcbind is needed by nfs-utils-1:1.2.3-36.el6.x86_64 解决包的依赖性 # rpm -ivh nfs-utils-1.2.3-36.el6.x86_64.rpm \ nfs-utils-lib-1.1.5-6.el6.x86_64.rpm \ rpcbind-0.2.0-11.el6.x86_64.rpm keyutils-1.4-4.el6.x86_64.rpm \ libevent-1.4.13-4.el6.x86_64.rpm \ libgssglue-0.1-11.el6.x86_64.rpm \ libtirpc-0.2.1-5.el6.x86_64.rpm Preparing... ########################################### [100%] 1:libgssglue ########################################### [ 14%] 2:libtirpc ########################################### [ 29%] 3:rpcbind ########################################### [ 43%] 4:libevent ########################################### [ 57%] 5:keyutils ########################################### [ 71%] 6:nfs-utils-lib ########################################### [ 86%] 7:nfs-utils ########################################### [100%]
3.1.2. 方法 2:通过 YUM 来进行安装
通过 YUM 来解决包的相关性,需要配置yum源
# mkdir /mnt/cdrom # mount /dev/cdrom /mnt/cdrom/ mount: block device /dev/sr0 is write-protected, mounting read-only # vi /etc/yum.repos.d/rhel-dvd.repo 创建新的文件,添加如下内容: [rhel-dvd] name=Red Hat Enterprise Linux $releasever - $basearch - DVD baseurl=file:///mnt/cdrom/Server/ enabled=1 gpgcheck=1 gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release # yum list | grep nfs nfs-utils.x86_64 1:1.2.3-36.el6 rhel-dvd nfs-utils-lib.i686 1.1.5-6.el6 rhel-dvd nfs-utils-lib.x86_64 1.1.5-6.el6 rhel-dvd nfs4-acl-tools.x86_64 0.3.3-6.el6 rhel-dvd sblim-cmpi-nfsv3.i686 1.1.1-1.el6 rhel-dvd sblim-cmpi-nfsv3.x86_64 1.1.1-1.el6 rhel-dvd sblim-cmpi-nfsv4.i686 1.1.0-1.el6 rhel-dvd sblim-cmpi-nfsv4.x86_64 1.1.0-1.el6 rhel-dvd # yum -y install nfs-utils
很方便地就安装完毕了
3.2. 启动 NFS 服务
NFS 依赖 rpcbind 服务,设置为其自动启动
由于nfs服务依赖于rpc(远程过程调用)服务,所以在启动nfs服务之前,需要先启动rpc服务
# chkconfig rpcbind on # service rpcbind start # service nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] # tail /var/log/messages 中 rpc.mountd[1797]: Version 1.2.3 starting kernel: NFSD: Using /var/lib/nfs/v4recovery as the NFSv4 state recovery directory kernel: NFSD: starting 90-second grace period # chkconfig nfs on
3.3. 配置 NFS 服务器
查看当前共享的 NFS 信息
# showmount -e localhost
Export list for localhost: 空白无输出
下面,做一个最基础的测试。创建 export 目录,分配权限
# mkdir /nfsdata
# ll /nfsdata/ -d
drwxr-xr-x 2 root root 4096 Jul 31 15:16 /nfsdata/
# chmod a+w /nfsdata/
# ll /nfsdata/ -d
drwxrwxrwx 2 root root 4096 Jul 31 15:16 /nfsdata/
配置 export 文件
# vi /etc/exports
添加如下内容,注意选项中逗号之间没有空格!
/nfsdata *(rw,root_squash,no_all_squash,sync)
这里的意思是说,需要共享/nfsdata这个目录,对所有客户端(*)都是可读写的(rw),并且是异步方式来访问。 因为不设置其他参数, 客户端创建文件或目录时默认的属主和组就是nfsnobody,即使客户端使用的账号是root。
# exportfs -r
Tip: -r Reexport all directories, synchronizing /var/lib/nfs/etab with /etc/exports. This option
removes entries in /var/lib/nfs/etab which have been deleted from /etc/exports, and removes
any entries from the kernel export table which are no longer valid.
# showmount -e localhost
Export list for localhost:
/nfsdata *
自己连接一下自己,测试一下。
# mkdir /mnt/nfs
# mount 127.0.0.1:/nfsdata/ /mnt/nfs
查看 mount 的信息
# mount
/dev/mapper/VolGroup-lv_root 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)
/dev/sr0 on /mnt/cdrom type iso9660 (ro)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
127.0.0.1:/nfsdata/ on /mnt/nfs type nfs (rw,vers=4,addr=127.0.0.1,clientaddr=127.0.0.1)
检查目录是否可以写入。
# touch /mnt/nfs/testfile1.txt
# ll /mnt/nfs/testfile1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 31 15:28 /mnt/nfs/testfile1.txt
注:如果以后直接在服务器上的共享目录创建文件或目录,要记得将属主和组设置为nfsnobody。否则客户端在访问时会出现“Permission denied”。
3.4. 配置 NFS 客户机
NFS 客户机也需要安装 nfs-utils 包。
# showmount -e 192.168.1.241
Export list for 192.168.1.241:
/nfsdata *
[root@db1 ~]# mount 192.168.1.241:/nfsdata /mnt/nfs/
[root@db1 ~]# ls /mnt/nfs/ -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 31 15:28 testfile1.txt
[root@db1 ~]# echo test >> /mnt/nfs/testfile1.txt
[root@db1 ~]# ls /mnt/nfs/testfile1.txt
/mnt/nfs/testfile1.txt
[root@db1 ~]# ls /mnt/nfs/testfile1.txt -l
-rw-r--r-- 1 nfsnobody nfsnobody 5 Jul 31 15:47 /mnt/nfs/testfile1.txt
4.排错
4.1 启动NFS服务时出错
原因可能是rpcbind没有启动
# service rpcbind start
# service nfs start
4.2 权限出错
是文件系统的权限不足
# chmod a+w /nfsdata
本文出自 “刘琼@天道酬勤” 博客,请务必保留此出处http://lqiong.blog.51cto.com/8170814/1549530
原文地址:http://lqiong.blog.51cto.com/8170814/1549530