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 服务器


相关文章
|
17天前
|
存储 Linux Android开发
Volatility3内存取证工具安装及入门在Linux下的安装教程
Volatility 是一个完全开源的工具,用于从内存 (RAM) 样本中提取数字工件。支持Windows,Linux,MaC,Android等多类型操作系统系统的内存取证。针对竞赛这块(CTF、技能大赛等)基本上都是用在Misc方向的取证题上面,很多没有听说过或者不会用这款工具的同学在打比赛的时候就很难受。以前很多赛项都是使用vol2.6都可以完成,但是由于操作系统更新,部分系统2.6已经不支持了,如:Win10 等镜像,而Volatility3是支持这些新版本操作系统的。
|
8天前
|
存储 Prometheus 监控
Linux技术工具:bpftrace介绍
Linux技术工具:bpftrace介绍
15 7
|
9天前
|
关系型数据库 MySQL Linux
Linux下怎么快速部署MySQL服务,并使用
Linux下怎么快速部署MySQL服务,并使用
26 5
|
10天前
|
监控 算法 Linux
探索Linux中的lz4命令:高效的数据压缩工具
**探索Linux中的LZ4工具:快速数据压缩。LZ4算法提供高速压缩与解压缩,适合实时数据处理。命令行工具如`lz4c`用于文件压缩(`lz4c file.txt compressed.lz4`)和解压缩(`lz4c -d compressed.lz4 decompressed.txt`)。特点是速度快、低内存占用,可选压缩级别。注意命令的实际形式取决于安装的实现,使用前应查阅文档。**
|
7天前
|
Linux 数据处理
Linux中的numfmt命令:数字格式化的强大工具
**numfmt命令在Linux中用于数字格式化,如转换进制、添加千位分隔符、处理字节单位。它支持从文件读取数字并能自定义分隔符、小数位数。例如:`numfmt 12345` 输出12,345(十进制),`numfmt -b 255` 输出11111111(二进制),`numfmt --to=iec 1000000` 输出976.6K(字节单位)。使用时注意选项组合及单位标准。**
|
13天前
|
Linux 数据处理
Linux命令`install`详解:不仅仅是安装工具
`install`命令在Linux中并非仅用于安装软件,而是用于精确复制文件和目录,设置权限及所有权。它能创建目标目录、处理符号链接并保留时间戳。例如,`install -m 644 source.txt /dest`用于复制文件并设置权限,`install -d -m 755 /dest/dir`创建目录。使用时要注意权限设置,避免误操作,并记录命令以备恢复。
|
14天前
|
Linux Shell 数据处理
探索Linux的`infokey`命令:终端键绑定的查询工具
探索Linux的`infokey`命令用于查询终端键绑定。它显示按键如何映射到命令,帮助用户理解终端行为。`infokey`与`ncurses`库配合,提供查询、清除和定制输出的功能。例如,`infokey -d`显示所有键绑定,`infokey -k &quot;\e[11~&quot;`检查F1键绑定。使用前确保安装了`ncurses`库,注意终端兼容性和权限问题。定期检查键绑定,备份配置,阅读文档以优化终端体验。
|
17天前
|
Linux 测试技术 开发者
【Docker项目实战】使用Docker部署instantbox临时Linux系统环境搭建工具
【6月更文挑战第13天】使用Docker部署instantbox临时Linux系统环境搭建工具
28 3
|
22天前
|
Linux Shell API
深入探索 `dbus-run-session`:Linux下的D-Bus会话管理工具
`dbus-run-session`是Linux下管理D-Bus会话的工具,它确保桌面环境和应用间的通信。当登录图形桌面时,D-Bus会话自动创建,支持应用和服务间的消息传递。通常不需要直接使用,但在特定情况下,可以通过`dbus-run-session bash`启动shell会话运行D-Bus相关命令。注意避免重复启动会话,勿以root运行,确保环境变量正确,可使用`--verbose`选项进行调试。了解其工作原理有助于解决D-Bus相关问题。
|
19天前
|
算法 数据挖掘 Linux
探索Linux中的awk命令:强大的文本分析工具
探索Linux中的`awk`命令,一个强大的文本分析工具,用于模式扫描、数据提取与报告生成。本文介绍`awk`的用途、工作原理、特点及应用示例。`awk`基于&quot;模式-动作&quot;框架,从输入数据中匹配模式并执行相应操作。其特点包括:强大的文本处理能力、灵活的I/O及简洁的语法。示例涵盖了打印特定行、处理字段、计算统计值等场景。使用`awk`时要注意理解输入数据、测试脚本、优化性能和添加注释。深入学习以提升数据处理技能。