14.1 NFS介绍 14.2 NFS服务端安装配置 14.3 NFS配置选项

可莉
• 阅读 467

第14章 NFS服务搭建与配置

14.1 NFS介绍

NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。NFS的数据传输基于RPC(remote procedure call)协议。

应用场景

A,B,C三台机器上需要被访问到的文件是一样的,A共享数据出来,B和C分别取挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。

14.2 NFS服务端安装配置

准备两台虚拟机,一台作为服务端,一台作为客户端。

服务端192.168.230.135

[root@cham002 ~]# yum install -y nfs-utils rpcbind


[root@cham002 ~]# vim /etc/exports
/home/nfstestdir  192.168.230.145/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
#指定要进行分享的目录;指定要共享该目录的机器,ip可以ip段,也可以写ip。

:wq              

创建分享目录并制定权限:
[root@cham002 ~]# mkdir /home/nfstestdir
[root@cham002 ~]# chmod 777 /home/nfstestdir  

安装完rpcbind之后服务会自动启动
[root@cham002 ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      11547/rpcbind       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6019/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1913/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2498/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      6019/nginx: master  
tcp6       0      0 :::3306                 :::*                    LISTEN      9831/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      11547/rpcbind       
tcp6       0      0 :::22                   :::*                    LISTEN      1913/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2498/master      

[root@cham002 ~]# ps aux |grep rpc
rpc      11547  0.0  0.1  64964  1044 ?        Ss   22:57   0:00 /sbin/rpcbind -w
root     11700  0.0  0.0 112680   976 pts/2    S+   23:16   0:00 grep --color=auto rpc



                                                              

启动NFS服务

[root@cham002 ~]# systemctl start nfs
[root@cham002 ~]# ps aux |grep nfs
root     11740  0.0  0.0      0     0 ?        S<   23:17   0:00 [nfsd4_callbacks]
root     11746  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11747  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11748  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11749  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11750  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11751  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11752  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11753  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11770  0.0  0.0 112680   972 pts/2    S+   23:17   0:00 grep --color=auto nfs
[root@cham002 ~]# ps aux |grep rpc
rpc      11547  0.0  0.1  64964  1304 ?        Ss   22:57   0:00 /sbin/rpcbind -w
root     11723  0.0  0.0      0     0 ?        S<   23:17   0:00 [rpciod]
root     11728  0.0  0.0  43824   544 ?        Ss   23:17   0:00 /usr/sbin/rpc.idmapd
root     11729  0.0  0.0  42564   944 ?        Ss   23:17   0:00 /usr/sbin/rpc.mountd
root     11772  0.0  0.0 112680   972 pts/2    S+   23:18   0:00 grep --color=auto rpc

将NFS服务加入开机启动项: 
[root@cham002 ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

客户端192.168.230.145

[root@test ~]# yum install -y nfs-utils
[root@test ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      38651/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1337/sshd           
tcp6       0      0 :::3306                 :::*                    LISTEN      38908/mysqld        
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1337/sshd           
[root@test ~]#  ps aux |grep rpc
rpc      61559  0.0  0.1  64964  1048 ?        Ss   22:58   0:00 /sbin/rpcbind -w
root     61727  0.0  0.0 112684   980 pts/0    R+   23:16   0:00 grep --color=auto rpc

检查客户端是否有权限访问服务端文件:
[root@test ~]# showmount -e 192.168.230.135
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
报错: 无法连接到服务端机器(网络不通)!

解决办法:

检查服务端NFS服务是否开启(监听111端口)
如果确认服务端NFS服务已经开启,那么检查防火墙状态,关闭服务端和客户端firewalld和SELinux防火墙
192.168.230.145
[root@test ~]# systemctl stop firewalld
[root@test ~]# getenforce
Enforcing
[root@test ~]# setenforce 0
[root@test ~]# getenforce
Permissive
192.168.230.135
[root@cham002 ~]# systemctl stop firewalld
[root@cham002 ~]# systemctl stop firewalld
[root@cham002 ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@cham002 ~]# systemctl stop iptables

解决完上述错误后再次执行命令:
[root@test ~]# showmount -e 192.168.230.135
Export list for 192.168.230.135:
/home/nfstestdir 192.168.230.0/24
即,客户端可以正常访问服务端机器,也可以看到共享的目录了。

开始挂载

[root@test ~]# mount -t nfs 192.168.230.135:/home/nfstestdir /mnt/
[root@test ~]# df -h
文件系统                          容量  已用  可用 已用% 挂载点
/dev/sda3                          17G  8.5G  7.7G   53% /
devtmpfs                          483M     0  483M    0% /dev
tmpfs                             493M     0  493M    0% /dev/shm
tmpfs                             493M  6.8M  486M    2% /run
tmpfs                             493M     0  493M    0% /sys/fs/cgroup
/dev/sda1                         197M  109M   88M   56% /boot
tmpfs                              99M     0   99M    0% /run/user/0
192.168.230.135:/home/nfstestdir   17G  7.0G  9.2G   44% /mnt
[root@test ~]# 

192.168.230.145
[root@test ~]# cd /mnt/
[root@test mnt]# ls
testdir
[root@test mnt]# touch champinlinux.111
[root@test mnt]# ls -l
总用量 0
-rw-r--r-- 1 cham cham 0 1月  17 00:03 champinlinux.111
drwxr-xr-x 2 root root 6 1月  17 00:01 testdir

[root@test mnt]# id cham
uid=1000(cham) gid=1000(cham) 组=1000(cham),1007(user5)



192.168.230.135

[root@cham002 ~]# ls -l /home/nfstestdir/
总用量 0
-rw-r--r-- 1 user1 user1 0 1月  17 00:03 champinlinux.111
drwxr-xr-x 2 root  root  6 1月  17 00:01 testdir
[root@cham002 ~]# id user1
uid=1000(user1) gid=1000(user1) 组=1000(user1)

NFS配置选项

• rw 读写

• ro 只读

• sync 同步模式,内存数据实时写入磁盘

• async 非同步模式

• no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大

• root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户

• all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户

• anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

14.4 exportfs命令

exportfs命令用来管理当前NFS共享的文件系统列表。

• exportfs -arv //不用重启nfs服务,配置文件就会生效

 192.168.230.145把它先卸载掉然后再重启nfs, 如果挂载了几十台机器每一台都要去卸载么?

[root@test ~]# umount /mnt
umount.nfs4: /mnt: device is busy
[root@test ~]# df -h
文件系统                          容量  已用  可用 已用% 挂载点
/dev/sda3                          17G  8.5G  7.7G   53% /
devtmpfs                          483M     0  483M    0% /dev
tmpfs                             493M     0  493M    0% /dev/shm
tmpfs                             493M  6.8M  486M    2% /run
tmpfs                             493M     0  493M    0% /sys/fs/cgroup
/dev/sda1                         197M  109M   88M   56% /boot
tmpfs                              99M     0   99M    0% /run/user/0
192.168.230.135:/home/nfstestdir   17G  7.0G  9.2G   44% /mnt
[root@test ~]# umount -l /mnt
[root@test ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        17G  8.5G  7.7G   53% /
devtmpfs        483M     0  483M    0% /dev
tmpfs           493M     0  493M    0% /dev/shm
tmpfs           493M  6.8M  486M    2% /run
tmpfs           493M     0  493M    0% /sys/fs/cgroup
/dev/sda1       197M  109M   88M   56% /boot
tmpfs            99M     0   99M    0% /run/user/0

•常用选项

• -a 全部挂载或者全部卸载

• -r 重新挂载

• -u 卸载某一个目录

• -v 显示共享目录

• 以下操作在服务端上

•vim /etc/exports //增加

/tmp/ 192.168.230.0/24(rw,sync,no_root_squash)

192.168.230.135

[root@cham002 ~]# exportfs -arv
exporting 192.168.230.0/24:/home/nfstestdir


验证一下
[root@cham002 ~]# vim /etc/exports
/home/nfstestdir  192.168.230.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
/tmp 192.168.230.145 (rw,sync,no_root_squash)

[root@cham002 ~]# exportfs -arv
exportfs: No options for /tmp 192.168.230.145: suggest 192.168.230.145(sync) to avoid warning
exportfs: No host name given with /tmp (rw,sync,no_root_squash), suggest *(rw,sync,no_root_squash) to avoid warning
exporting 192.168.230.145:/tmp
exporting 192.168.230.0/24:/home/nfstestdir
exporting *:/tmp

192.168.230.145
[root@test ~]# showmount -e 192.168.230.135
Export list for 192.168.230.135:
/home/nfstestdir 192.168.230.0/24
/tmp             (everyone)
[root@test ~]# mount -t nfs 192.168.230.135:/tmp/ /mnt/
[root@test ~]# df -h
文件系统              容量  已用  可用 已用% 挂载点
/dev/sda3              17G  8.5G  7.7G   53% /
devtmpfs              483M     0  483M    0% /dev
tmpfs                 493M     0  493M    0% /dev/shm
tmpfs                 493M  6.8M  486M    2% /run
tmpfs                 493M     0  493M    0% /sys/fs/cgroup
/dev/sda1             197M  109M   88M   56% /boot
tmpfs                  99M     0   99M    0% /run/user/0
192.168.230.135:/tmp   17G  7.0G  9.2G   44% /mnt
[root@test ~]# ls /mnt
champ.sock                                                                test.com.log-20180108
mysql2.sql                                                                test.com.log-20180109
mysql_all.sql                                                             test.com.log-20180110
mysqlbak180116.sql                                                        test.com.log-20180112
mysql.sock                                                                test.com.log-20180115
php-fcgi.sock                                                             test.com.log-20180116
systemd-private-4045549bb7d44cbd9149ab7506b1c43c-vmtoolsd.service-iF9vYt  user.sql
test.com.log
[root@test ~]# vim 123.txt
123
[root@test ~]# ls -l /mnt/
总用量 1964
-rw-r--r-- 1 root  root       16 1月  18 08:09 1212.txt
srw-rw-rw- 1 root  root        0 1月  16 23:48 champ.sock
-rw-r--r-- 1 root  root    30848 1月  16 00:33 mysql2.sql
-rw-r--r-- 1 root  root  1306193 1月  16 00:29 mysql_all.sql
-rw-r--r-- 1 root  root   653300 1月  16 00:14 mysqlbak180116.sql
srwxrwxrwx 1 user5 user5       0 1月  16 23:48 mysql.sock
srw-rw-rw- 1 root  root        0 1月  16 23:48 php-fcgi.sock
drwx------ 3 root  root       17 1月  16 23:48 systemd-private-4045549bb7d44cbd9149ab7506b1c43c-vmtoolsd.service-iF9vYt
-rw-r--r-- 1 root  root        0 1月  17 00:00 test.com.log
-rw-r--r-- 1 root  root        0 1月   5 00:00 test.com.log-20180108
-rw-r--r-- 1 root  root     1583 1月   9 02:21 test.com.log-20180109
-rw-r--r-- 1 root  root        0 1月  10 00:00 test.com.log-20180110
-rw-r--r-- 1 root  root        0 1月  11 00:00 test.com.log-20180112
-rw-r--r-- 1 root  root        0 1月  13 00:00 test.com.log-20180115
-rw-r--r-- 1 root  root        0 1月  16 00:00 test.com.log-20180116
-rw-r--r-- 1 root  root     7002 1月  16 00:22 user.sql

192.168.230.135
[root@cham002 ~]# ls -l /mnt/
总用量 4
-rw-r--r-- 1 root root 16 11月  6 14:12 23.txt
[root@cham002 ~]# ls -l /tmp/
总用量 1964
-rw-r--r-- 1 root  root       16 1月  18 08:09 1212.txt
srw-rw-rw- 1 root  root        0 1月  16 23:48 champ.sock
-rw-r--r-- 1 root  root    30848 1月  16 00:33 mysql2.sql
-rw-r--r-- 1 root  root  1306193 1月  16 00:29 mysql_all.sql
-rw-r--r-- 1 root  root   653300 1月  16 00:14 mysqlbak180116.sql
srwxrwxrwx 1 mysql mysql       0 1月  16 23:48 mysql.sock
srw-rw-rw- 1 root  root        0 1月  16 23:48 php-fcgi.sock
drwx------ 3 root  root       17 1月  16 23:48 systemd-private-4045549bb7d44cbd9149ab7506b1c43c-vmtoolsd.service-iF9vYt
-rw-r--r-- 1 root  root        0 1月  17 00:00 test.com.log
-rw-r--r-- 1 root  root        0 1月   5 00:00 test.com.log-20180108
-rw-r--r-- 1 root  root     1583 1月   9 02:21 test.com.log-20180109
-rw-r--r-- 1 root  root        0 1月  10 00:00 test.com.log-20180110
-rw-r--r-- 1 root  root        0 1月  11 00:00 test.com.log-20180112
-rw-r--r-- 1 root  root        0 1月  13 00:00 test.com.log-20180115
-rw-r--r-- 1 root  root        0 1月  16 00:00 test.com.log-20180116
-rw-r--r-- 1 root  root     7002 1月  16 00:22 user.sql

说明:通常情况下,不限制root的情况多,就是no_root_squash

14.5 NFS客户端问题

针对NFS4版本在centos6中应用存在如下问题:
客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主、数组为nobody。

解决方法:

  • 方法1:在客户端进行挂载时加上选项-o nfsvers=3

    [root@test ~]# mount -t nfs -o nfsvers=3 192.168.230.135:/tmp/ /mnt/ 如果目录已经挂载,而又不想卸载,执行如下命令: [root@test ~]# mount -t nfs -oremount,nfsvers=3 192.168.230.135:/tmp/ /mnt/

  • 方法2:客户端和服务端都需要

•客户端和服务端都需要

• vim /etc/idmapd.conf //

• 把“#Domain = local.domain.edu” 改为 “Domain = xxx.com” (这里的xxx.com,随意定义吧),然后再重启rpcidmapd服务(cenos7 是没有这个服务的,重启rpcbind就行)

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
Linux NFS服务详解
1.什么是NFS(NetworkFileSystem) NFS就是NetworkFileSystem的缩写,最早之前是由Sun所发展出来的。他最大的功能就是可以透过网络,让不同的机器、不同的操作系统、可以彼此分享个别的档案(sharefile),所以,也可以简单的将他看做是一个fileserver呢!这个NFSS
Wesley13 Wesley13
2年前
NFS 服务器搭建
NFS(网络文件系统)用于Unix/Linux之间的文件共享,NFS在文件传送过程中依赖与RPC(远程过程调用)协议。NFS本身是没有提供信息传送的协议和功能。只要用到NFS的地方都需要启动RPC服务,不论是NFS的服务端还是客户端。在启动NFS服务之前,首先要启动RPC服务(CentOS5是portmap服务,CentOS6.6以后的版本是rp
Stella981 Stella981
2年前
Cento7搭建nfs共享目录
一、简介:NFS是NetworkFileSystem的缩写,是网络文件系统,具体的功能就是通过网络让不同的机器,不同的操作系统能够彼此分享个别的数据,让应用程序客户端通过网络访问位于服务器磁盘中的数据。二、服务端安装1、环境说明服务器端:IP:192.168.112.128共享目录:/data/k8
Wesley13 Wesley13
2年前
NFS的基本配置
本文索引:NFS介绍NFS服务器安装配置NFS配置选项NFS介绍NFS是网络文件系统(NetworkFileSystem)的缩写。需要借助网络,实现数据的同步。NFS最早由Sun公司进行开发,分2,3,4三个版本,2和3版本有Sun公司起草开发,4.0开始由Netapp公司参与主导开发,最
Stella981 Stella981
2年前
Linux NFS 详解
目录:1、了解NFS服务2、NFS主要文件3、NFS安装4、NFS配置5、NFS客户端配置   6、固定NFS端口7、Windows挂载NFS1、了解NFS服务   NFS是Network 
Wesley13 Wesley13
2年前
NFS部署教程
NFS(NetworkFileSystem)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。简单的来说:它就是是可以透过网络,让不同的主机、不同的操作系统可以共享存储。NFS在文件传送或信
Wesley13 Wesley13
2年前
NFS
环境:VMwareWorkstation12Pro,Windows10,CentOS7.5,Xshell5\TOC\NFS介绍什么是NFS(NetworkFileSystem)简单来说NFS就是实现文件共享功能的,与windows文件共享功能类似,但本篇博客
芝士年糕 芝士年糕
1年前
NFS服务详解
今天使用新租的3A服务器搭建NFS,噢对了,我租了两台,真的好使1.概述NFS是一种基于TCP/IP传输的网络文件系统协议。通过使用NFS协议,客户机可以像访问本地目录一样访问远程服务器中的共享资源NAS存储:NFS服务的实现依赖于RPC(
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这