阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux NFS文件服务器安装与配置简述

144次阅读
没有评论

共计 5017 个字符,预计需要花费 13 分钟才能阅读完成。

1. 场景

某单位需要配置 NFS 服务器,为同步图片文件。

主机名:nfs.local,test.local

IP 地址:192.168.3.15、192.168.3.18

 

2. 实验环境

2.1. 操作系统

# cat /etc/RedHat-release 

Red Hat Enterprise Linux Server release 6.5 (Santiago)

# uname -a

Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 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.3.15

NETMASK=255.255.255.0

GATEWAY=192.168.3.1

修改主机名

# vi /etc/sysconfig/network

NETWORKING=yes

#HOSTNAME=localhost.localdomain

HOSTNAME=nfs.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 或 init 6

 

3. NFS 服务器安装与配置

3.1. 安装 NFS 服务器组件

3.1.1. 通过 YUM 来进行安装

通过 YUM 来解决包的相关性。

 

# yum -y install nfs-utils

很方便地就安装完毕了

 

3.2. 启动 NFS 服务

NFS 依赖 rpcbind 服务,设置为其自动启动

# chkconfig rpcbind on

# chkconfig nfs on

# service rpcbind start

# service nfs start

Starting NFS services: [OK]

Starting NFS quotas: [OK]

Starting NFS mountd: [OK]

Starting NFS daemon: [OK]

在日志文件 /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

 

3.3. 配置 NFS 服务器(192.168.3.15)

查看当前共享的 NFS 信息

# showmount -e localhost

Export list for localhost: 空白无输出

 

配置 export 文件

# vi /etc/exports

添加如下内容,注意选项中逗号之间没有空格!

/home/tomcat/image/webapps/static/resources  *(rw,no_root_squash,no_all_squash,sync)  

 

保存退出

exportfs -r            // 重新挂载 /etc/exports 中的设置

service rpcbind restart
service nfs restart

 

3.3.1. 服务端验证 NFS 共享:

[root@localhost ~]# showmount -e

Export list for localhost.localdomain:

/home/tomcat/image/webapps/static/resources *

 

注:正常能显示出上面设置的共享目录,如果报如下错误:

clnt_create: RPC: Program not registered

说明 rpcbind 服务与 NFS 服务 启动顺序不对,把他俩都停了,重新启动,先启动 rpcbind 服务再启动 nfs 服务;

 

3.4. 配置 NFS 客户机(192.168.3.18)

NFS 客户机也需要安装 nfs-utils 包。

# yum -y install nfs-utils

[root@localhost ~]# showmount -e 192.168.3.15

Export list for 192.168.3.15:

/home/tomcat/image/webapps/static/resources *

 

3.5. 客户端基本配置

修改 IP 地址。(注意:根据您的实现环境进行配置)

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

TYPE=Ethernet

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.3.18

NETMASK=255.255.255.0

GATEWAY=192.168.3.1

修改主机名

# vi /etc/sysconfig/network

NETWORKING=yes

#HOSTNAME=localhost.localdomain

HOSTNAME=test.local

# service network restart

 

为了方便实验,将防火墙、selinux 关闭。

 

3.6. 启动服务

启动 rpcbind 服务,设置为其自动启动

# chkconfig rpcbind on

# service rpcbind start

客户端 rpcbind 服务一定要启动,nfs 可以不用启动

 

3.7. 设置启动自动挂载 (192.168.3.18)

[root@localhost ~]# vi /etc/fstab

新增

192.168.3.15:/home/tomcat/image/webapps/static/resources /home/tomcat/image/webapps/static/resources          nfs    defaults        0 0

#
# /etc/fstab
# Created by anaconda on Fri Dec 16 15:54:15 2016
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                      ext4    defaults        1 1
UUID=78193f8b-3efc-4c29-bbaf-2d0b1613d69f /boot                  ext4    defaults        1 2
/dev/mapper/VolGroup-lv_home /home                  ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                  /dev/shm                tmpfs  defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                  /sys                    sysfs  defaults        0 0
proc                    /proc                  proc    defaults        0 0
192.168.3.15:/home/tomcat/image/webapps/static/resources /home/tomcat/image/webapps/static/resources          nfs    defaults        0 0

四. 知识扩展

 

exports 命令:

 

参数说明如下。
(1)-a:全部挂载(或卸载)/etc/exports 文件内的设定。
(2)-r:重新挂载 /etc/exports 中的设置,此外同步更新 /etc/exports 及 /var/lib/nfs/xtab 中的内容。
(3)-u:卸载某一目录。
(4)-v:在 export 时将共享的目录显示在屏幕上。

 

 

/etc/exports 文件内容格式:

 

< 共享目录 > [客户端 1 选项(访问权限, 用户映射, 其他)]

 

用户映射选项

  • all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);

  • no_all_squash:与 all_squash 取反(默认设置);

  • root_squash:将 root 用户及所属组都映射为匿名用户或用户组(默认设置);

  • no_root_squash:与 rootsquash 取反;

  • anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);

  • anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);

其它选项

  • secure:限制客户端只能从小于 1024 的 tcp/ip 端口连接 nfs 服务器(默认设置);
  • insecure:允许客户端从大于 1024 的 tcp/ip 端口连接服务器;
  • sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
  • async:将数据先保存在内存缓冲区中,必要时才写入磁盘;
  • wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置);
  • no_wdelay:若有写操作则立即执行,应与 sync 配合使用;
  • subtree:若输出目录是一个子目录,则 nfs 服务器将检查其父目录的权限 (默认设置);
  • no_subtree:即使输出目录是一个子目录,nfs 服务器也不检查其父目录的权限,这样可以提高效率;

CentOS 7 下 NFS 实例笔记 http://www.linuxidc.com/Linux/2016-12/138708.htm

Linux 下 NFS 搭建步骤 http://www.linuxidc.com/Linux/2016-08/134022.htm

NFS 文件系统详解  http://www.linuxidc.com/Linux/2016-06/131940.htm

基于 LAMP 平台利用 NFS 远程共享资源实现站点构建  http://www.linuxidc.com/Linux/2016-07/133510.htm

Linux NFS 服务器的安装与配置详解 http://www.linuxidc.com/Linux/2017-01/139886.htm

CentOS 7.2 中 NFS1.3 安装 http://www.linuxidc.com/Linux/2016-10/135970.htm

Ubuntu 14.04 安装配置 NFS 服务器 http://www.linuxidc.com/Linux/2016-04/129848.htm

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-03/142410.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计5017字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中