Linux工具篇-NFS 安装部署与使用

简介: Linux工具篇-NFS 安装部署与使用

Linux服务端

安装

  1. centos7自带了rpcbind,所以只需执行
yum -y install nfs-utils

当然,如果要执行

yum -y install nfs-utils rpcbind

同时安装nfs+rpc也没有问题。

  1. 验证是否安装成功
rpm -qa nfs-utils
[root@node2 ~]#rpm -qa nfs-utils
nfs-utils-1.3.0-0.68.el7.1.x86.64
  1. 查看当前nfs服务所支持的nfs的版本
cat /proc/fs/nfsd/versions
[root@node2 ~]#cat /proc/fs/nfsd/versions
-2 +3 +4 +4.1 +4.2  

配置

  1. 创建一个共享目录/data/share(目录可自定义)并设置权限
mkdir /data/share
chmod -R 777 /data/share
复制代码
  1. 配置编辑NFS默认的配置文件/etc/exports
// 固定网段所有IP可挂载
echo "/data/nfs 172.18.30.*(rw,sync,no_root_squash)" >> /etc/exports
// 固定网段固定IP区间可挂载
echo "/data/nfs 172.18.30.0/24(rw,sync,no_root_squash)" >> /etc/exports
// 所有客户端可挂载
echo "/data/nfs *(rw,sync,no_root_squash)" >> /etc/exports
复制代码

权限配置的说明:

  • rw:可读写
  • ro: 只读,但最终能不能读写,还是与文件系统的 rwx 及身份有关
  • no_root_squash:当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员
  • root_squash: 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名(nobody)用户
  • all_squash: 不论登入 NFS 的使用者身份为何, 均被映射为匿名用户,通常就是 nobody(nfsnobody)
  • insecure: 允许从客户端过来的非授权访问
  • sync: 数据会同步写入到内存与硬盘中
  • async: 数据会先暂存于内存当中,而非直接写入硬盘
  • anonuid: 指定uid的值,此uid必须存在于/etc/passwd中
  • anongid:指定gid的值
  1. 使nfs配置生效exportfs -r

exportfs的参数说明:

  • -a:全部mount 或unmount /etc/exports中的内容
  • -r: Reexport all directories: 重新导出所有目录
  • -u:umount目录
  • -v: verbose,输出详情
  1. 查看当前配置为nfs共享的目录及其状态exportfs -v
[root@node2 data]#exportfs-v
/nfsroot  <world>(sync,wdelay,hide,no_subtreecheck,sec=sys,rw,insecure,no root squash,no all sguash)  
/data/share <world>(sync,wdelay,hide,nosubtree_check,sec=sys,rw,insecure,no_root squash,no tt squash
  1. 检查共享目录是否设置成功showmount -e
[rootanode2 data]#showmount -e Export list for node2:/data/share */nfsroot
[ root @node2 data ]# rpcinfo - p 
 program vers proto 
p0rt
10000 o 
 tcp 
1l1
10oo00
 tcp 
11l
100000
 tcp 
111
10o000
 udp 
100000
 udp 
1e0000
 udp 
100024
 udp 
100024
 tcp 
100005
 udp 
1000e5
 tcp 
100005
 udp 
100005
 tcp 
100005
 udp 
10ooe5
 tcp 
1eoee3
 tcp 
100003
 tcp 
100227
 tcp 
100003
 udp 
100003
 udp 
100227
 udp 
100021
 udp 
100021
 udp 
100021
 udp 
100021
 tcp 
100021
 tcp 
100021
 tCp 
 service 
 portmapper portmapper portmapper portmapper portmapper portmapper 
 status 
 status mountd mountd mountd mountd mountd mountd 
 nfs 
 nfs 
 nfs _ acl 
111
111
46060
34108
20048
20048
20048
20048
20048
20048
2049
2049
2049
2049 nfs 
2049
 nfs 
2049
 nfs _ acl 
21502
 nlockmgr 
21502
 nlockmgr 
21502
 nlockmgr 
21501
 nlockmgr 
21501 I 
 nlockmgr 
21501 nlockmgr 

启动&检查

  1. 启动 RPC 服务
// 启动rpc 
systemctl start rpcbind 
// 设置开机启动 
systemctl enable rpcbind
复制代码
  1. 启动 NFS 服务
// 启动nfs 
systemctl start nfs
// 设置开机启动 
systemctl enable nfs
复制代码
  1. rpcinfo -p ,如果显示rpc 服务器注册的端口列表(端口:111),则启动成功。

Windows & Linux 客户端

安装NFS

Linux

客户端上不需要启动nfs服务,只是为了使用showmount/mount工具

  1. 安装nfs yum -y install nfs-utils
  2. 检测rpc服务 rpcinfo -p
( base )[ root @ VM _0_10_ centos ~]# rpcinfo - I 
 program vers proto 
 port service 
100000
 tcp 
111
 portmapper 
100000
 tcp 
111
 portmapper 
100000
 tcp 
111
 portmapper 
100000
 udp 
111
 portmapper 
100000
 udp 
111
 portmapper 
100000
 udp 
111
 portmapper 
100024
 udp 
49213
 status 
100024
 tcp 
)38681
 status 
  1. 查看服务器端可挂载目录 showmount -e 129.xxx.xx.139
(base)[root@VM 0 10 centos ~]# showmount -e 129.. .139 Export list for 129..139:/data/share *
/nfsroot  

Windows

参考win7 下设置挂载Linux服务器nfs共享的数据

挂载目录

Linux

  1. 将服务器端目录挂在到本地
mount -t nfs 129.xxx.xx.139:/data/share /data/share
  1. 验证:在服务器端 /data/share路径下新建文件后去客户端挂载目录查看是否同步
  • 使用df -hT查看 Linux 系统上的文件系统磁盘使用情况,可以看到挂载盘
( base )[ root @ vM _0_l0_ centos ~]# df - hT 
 Filesystem 
 Type 
 SlZe 
/ dev /vda1
ext4
50G
 devtmpfs 
 devtmpfs 
1.9G
 tmpfs 
 tmptS 
1.9G
 tmpfs 
 tmpfs 
1l.9G
 tmpfs 
 tmpfs 
1.9G
 tmofs 
 tmofs 
37gM
129.204.4.139:/ nfsroot nfs4
50G
 Used Avait Use % s Mounted on 
16G
336/
0s/ dev 
1%/ dev / shm 
1%/ run 
0%/ sys / fs / cgroup 0%/ run / user /0
45G2.4G96%/ data / share 
32G
1.9G
24K1.9G
500K1.9G
1.9G
379M
自动挂载

客户端机器启动后能自动加载nfs文件系统:

把mount命令:例如:

mount -t nfs 192.168.3.172:/data/share /data/share

写入到 /etc/rc.local

说明:有很多资料建议写入到/etc/fstab

os启动时一般会先加载文件系统后启动网络,在没有网络时nfs是无法加载的,总是出错,所以还是建议放到/etc/rc.local中

Windows7

mount \\file-system-nas.com\data\share y:
D:\>mount \/file-system-nas.com\datalshare y:
y:现已成功连接到/file-system-nas.com\data\share
命令已成功完成。

参考win7 下设置挂载Linux服务器nfs共享的数据

自动挂载

参考=> windows自动挂载NFS文件系统

常见问题

RPC: Port mapper failure

错误提示:RPC:端口映射失败,既然是端口的问题,那就放行端口。查看rpc正在运行的关于nfs的端口rpcinfo -p

// 防火墙上放行端口
firewall-cmd --zone=public --add-port=111/tcp --permanent
firewall-cmd --zone=public --add-port=20048/tcp --permanent
firewall-cmd --zone=public --add-port=111/udp --permanent
firewall-cmd --zone=public --add-port=20048/udp --permanent
// 重启防火墙
firewall-cmd --reload
// 查看防火墙public端口
firewall-cmd --zone=public --list-ports
复制代码

windows 挂载报错 NET HELPMSG 53

NFS 服务器有一个在不安全模式下工作的选项(允许更高的传入端口号)。Windows NFS 客户端通常使用更高的端口号。您可以通过向共享添加一个选项来启用此选项 insecure

示例:/share *(insecure,rw)

其他命令

客户端查看nfs服务的版本:

nfsstat -c

[ root @ vM _0_10_ centos ~]# nfsstat - c rpc stats :
 retrans 
 authrefrsh 
1597
 Client nfs v4:
 fead write 
 commit 
 open 
%a0
0% g 
 setattr 
 fsinfo 
s0
 g %0
 open _ conf renew 
 access 0%12
 tink 
090
77 nu 
 open _ noat 
 open _ dgrd 
 close 
 A 9%A
0 a 
 setclntid 
 confim 
 lock 
00
 getattr 
 lookup 
 lookup _ root remove 
 rename 
976
0%2
0 sa 
0 g 
 symlink 
 pathconf 
 statfs 
 readlink 
 readdir 
0%4
08s1
9A
00
 g % o 
 server _ caps delegreturn g 
 getacl 
 setac 
0%0
 A 9
00
 E 
 tockt tocku 
0
61%6
%0
 create 
 fs locations rel lkowner .
  %
00
 A 9A
 secinfo 
 exchange _ id create _ ses 
 destroy _ ses sequence 
 get _ lease _ t 
02
032
03%553
349s
 A 9:A
 reclaim _ comp layoutget 
 getdevinfo 1
 layoutcommit layoutreturn getdevlist 
02
0
 A 9%: c 
%0
0% A 
0 e 
( null )
9-0

服务端查看 nfs的版本

nfsstat -s

[ root @node2 datal # nfsstat - s Server rpc stats :
 calls 
 badcalls 
 badclnt 
1712
18
 TT 
 Server nfs v3:
 badauth 
 xdrcall 
 getattr 
34%1l
 write 
% g 
 remove 
 mdir 
0 g 
 fsstat 
 fsinfo 
 setattr 
 lookup 0%s15
 create 
 mkdir 
0 so 
 rename 
1nk
09A
 o o 
 access 
 readlink 
9-
 symlink 
mkn0d
9p readdir 0%29:A
11nu
12%0
 read 
69
 readdirplus 
1%2
9T
 pathconf 
 commit 
 T 90
0% a 
381
5erver nfs v4:
 compound 0%1588
66
 Berver nfs v4 operations :op0- unused opl - unused 
 G 9A
 create 
77 nu 
op2- future access 
0%12
 delegpurge delegreturn getattr 
 getfh 
0%1015
27%6
% G 
 lockt 
 locku 
 lookup 
 lookup _ root nverify 
 SA 
9-/ a 
 g % a 
0%a0
 openattr 
 open _ conf 
 open _ dgrd 
 putfh 
 putpubfh 
0%1021
278
090
0%0
0
 readdir 
 readlink 
 remove 
 rename 
 sc 
0 o 
0% o 
90
 restorefh 
 savefh 
 secinfo 
 setattr 
00
日0
00
00
 rellockowner bc _ ctl 
0
0 a 
 close commit 
 link 
 e % e 
 lock 
 open 
 putrootfh 
 read 
 o 
 A 8A
 renew 
 setcltid 
09
 bind _ conn 
 T 9a0
 setcltidconf verify 
 O g 
 exchange _ id create _ ses destroy _ ses free _ stateid getdirdeleg getdevinfo 
 S - n 
 A sg 
 getdevlist layoutcommit layoutget 
 layoutreturn secinfononam sequence 
0%l579
 test _ stateid want _ deleg destroy _ clid reclaim _ comp 
02
口9C
Wr1te
00
62
06
02
A90
080
0%2
43
日9:A
 A g - A 
 set ssv 0%2

查看本地centos的版本:

cat /etc/redhat-release
(base)[root@VM 0 10 centos ~]# cat /etc/redhat-release
Centos Linux release 7.6.1810(Core) 

去掉nfs共享

只需只需卸载命令即可,umount用法如下

(base) root@vM日10 centos~#umount -V
Usage:
umount [-hV]
umount -a [options]
umount [options] <source> <directory?
Options:
-a,--all  unmount all filesystems 
-A,--all-targets  unmount all mountpoins for the given device 
in the current namespace
-c,--no-canonicalize  don't canonicalize paths  
-d,--detach-loop  if mounted loop device, also free this loop device  
--fake  dryrun; skip the umount(2) syscall  
-f,--force  force unmount (in case of an unreachable NFS system 
-i,--internal-only  don't call the umount.<type> helpers  
-n,--no-mtab  don't write to /etc/mtab  
-l,--lazy detach the filesystem now, and cleanup all later  
-0,--test-opts <list> limit the set of filesystems (use with -a)  
-R,--recursive  recursively unmount a target with all its children  
-r,--read-only  In case unmounting fails, try to remount read-only  
-t, --types <list>  limit the set of filesystem types 
-v,--verbose  say what is being done  
-h,--help display this help and exit  
-V,--version  output version information and exit

mount 挂载追加参数

通过执行man nfs查看帮助文档,nfs版本不同,对应的挂载参数名不同

示例:

mount -t nfs -o vers=4.1,proto=tcp,timeo=60 129.204.4.139:/data/share /data/share

参考阅读

启动和停止 NFS 服务器

常见的 NFS 挂载选项

配置 NFS 服务器


目录
相关文章
|
3月前
|
Linux 网络安全 数据安全/隐私保护
Linux 超级强大的十六进制 dump 工具:XXD 命令,我教你应该如何使用!
在 Linux 系统中,xxd 命令是一个强大的十六进制 dump 工具,可以将文件或数据以十六进制和 ASCII 字符形式显示,帮助用户深入了解和分析数据。本文详细介绍了 xxd 命令的基本用法、高级功能及实际应用案例,包括查看文件内容、指定输出格式、写入文件、数据比较、数据提取、数据转换和数据加密解密等。通过掌握这些技巧,用户可以更高效地处理各种数据问题。
384 8
|
23天前
|
监控 安全 Ubuntu
Linux下如何安装配置Fail2ban防护工具
通过以上步骤,可以在Linux系统中成功安装和配置Fail2ban,从而有效保护服务器免受暴力破解等攻击。Fail2ban通过实时监控日志文件,自动更新防火墙规则,为系统安全提供了一层重要的保护。
93 36
|
1月前
|
Rust Ubuntu Java
[Linux工具] Makefile
Makefile是Linux环境下用于自动化编译和链接程序的配置文件,常用于简化大型项目的编译流程。通过定义目标文件、依赖文件及生成命令,Makefile能高效管理编译任务。它不仅适用于C语言项目,还可扩展到其他编程语言和非编程任务中。
58 20
[Linux工具] Makefile
|
9天前
|
Linux API
Linux下载工具wget与curl
`wget` 是一个用于从网络下载文件的命令行工具,支持HTTP、HTTPS和FTP协议。它能自动处理下载中断,并支持递归下载网站内容。基本用法:`wget URL`,可指定文件名(`-O`)、保存目录(`-P`),还支持断点续传(`-c`)、限速(`--limit-rate`)和递归下载(`-r`)。相比之下,`curl` 更侧重于发送各种HTTP请求(如GET、POST),并支持文件上传、自定义请求头和cookie等功能。
43 10
|
6天前
|
存储 Ubuntu 前端开发
Linux软件包管理工具概览
在Linux系统中,dpkg、apt、rpm、yum和dnf是几种常见的包管理工具,它们分别属于不同的Linux发行版或家族,并有着各自的诞生顺序和特点。下面将按照这些工具的诞生顺序,并结合Debian、Red Hat、CentOS、Ubuntu和Kali等系统,进行详细的介绍。
19 4
|
1月前
|
Linux 网络性能优化 网络安全
Linux(openwrt)下iptables+tc工具实现网络流量限速控制(QoS)
通过以上步骤,您可以在Linux(OpenWrt)系统中使用iptables和tc工具实现网络流量限速控制(QoS)。这种方法灵活且功能强大,可以帮助管理员有效管理网络带宽,确保关键业务的网络性能。希望本文能够为您提供有价值的参考。
96 28
|
1月前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
42 11
|
2月前
|
消息中间件 Java Kafka
【手把手教你Linux环境下快速搭建Kafka集群】内含脚本分发教程,实现一键部署多个Kafka节点
本文介绍了Kafka集群的搭建过程,涵盖从虚拟机安装到集群测试的详细步骤。首先规划了集群架构,包括三台Kafka Broker节点,并说明了分布式环境下的服务进程配置。接着,通过VMware导入模板机并克隆出三台虚拟机(kafka-broker1、kafka-broker2、kafka-broker3),分别设置IP地址和主机名。随后,依次安装JDK、ZooKeeper和Kafka,并配置相应的环境变量与启动脚本,确保各组件能正常运行。最后,通过编写启停脚本简化集群的操作流程,并对集群进行测试,验证其功能完整性。整个过程强调了自动化脚本的应用,提高了部署效率。
【手把手教你Linux环境下快速搭建Kafka集群】内含脚本分发教程,实现一键部署多个Kafka节点
|
1月前
|
安全 网络协议 Linux
结合 `nc` 工具利用笑脸漏洞(Smile Bug)攻击 Metasploitable2 Linux
本文介绍如何使用 `nc`(Netcat)工具结合笑脸漏洞(Smiley Bug)攻击 Metasploitable2 Linux 靶机。首先概述了 `nc` 的基本功能和高级用法,包括建立连接、监听端口、文件传输等操作。接着详细描述了笑脸漏洞的原理及其在网络攻防中的应用,展示了通过 `nc` 发送恶意输入检测漏洞的方法。最后结合 Python 脚本实现更复杂的攻击场景,并强调了合法性和环境隔离的重要性。
54 13
|
2月前
|
运维 监控 Linux
推荐几个不错的 Linux 服务器管理工具
推荐几个不错的 Linux 服务器管理工具
207 6