参考来源:
nfs安装配置-Debian/Ubuntu
什么是NFS?NFS挂载
NFS详解(概念+实验演示)
快速回顾:
nfs服务器搭建:
1>.服务器端:
1)创建共享目录
2)修改配置文件/etc/exports
3)重启服务 nfs-server
2>.客户端
1)挂载服务器共享目录 showmount -e
NFS服务的用途、场景,优缺点–pass
NFS服务的实践记录
服务端侧环境信息:(操作系统版本、软件版本、ip地址信息)
root@debian:/data/share/pv1# uname -a
Linux debian 5.10.0-32-amd64 #1 SMP Debian 5.10.223-1 (2024-08-10) x86_64 GNU/Linux
root@debian:/data/share/pv1# dpkg -l | grep nfs
ii libnfs13:amd64 4.0.0-1 amd64 NFS client library (shared library)
ii libnfsidmap2:amd64 0.25-6 amd64 NFS idmapping library
ii nfs-common 1:1.3.4-6 amd64 NFS support files common to client and server
ii nfs-kernel-server 1:1.3.4-6 amd64 support for NFS kernel server
root@debian:/data/share/pv1# dpkg -l | grep rpc
ii libtirpc-common 1.3.1-1+deb11u1 all transport-independent RPC library - common files
ii libtirpc3:amd64 1.3.1-1+deb11u1 amd64 transport-independent RPC library
ii rpcbind 1.2.5-9 amd64 converts RPC program numbers into universal addresses
root@debian:/data/share/pv1# ip a | grep 130
inet 192.168.255.130/24 brd 192.168.255.255 scope global dynamic noprefixroute ens33
客户端侧环境信息:
[root@centos7 ~]# uname -a
Linux centos7 3.10.0-1160.114.2.el7.x86_64 #1 SMP Wed Mar 20 15:54:52 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
[root@centos7 ~]# ip a | grep 129
inet 192.168.255.129/24 brd 192.168.255.255 scope global noprefixroute dynamic ens33
[root@centos7 ~]#
服务端侧配置
安装软件包
# 安装 NFS服务器端
apt-get install -y nfs-kernel-server
# 验证
dpkg -l | grep nfs # 查看是否安装成功
dpkg -l | grep rpcbind # 安装nfs时,默认会安装NFS服务依赖:rpcbind
创建共享文件目录并赋予权限(其他人others)
# 创建共享目录
mkdir /data/share
# 赋予可读写执行权限
chmod 777 /data/share/
# 验证
ls -ldZ /data/share/
配置文件/etc/exports中添加共享目录的权限信息
root@debian:/home/a# echo "/data/share *(rw,sync,insecure,no_root_squash)" >> /etc/exports
root@debian:/home/a# cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/data/share *(rw,sync,insecure,no_root_squash)
补充:配置文件中关键字的解释
rw # 读写权限
sync # 只读权限
root_squash # 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户
no_root_squash # 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员
all_squash # 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户
sync # 同时将数据写入到内存与硬盘中,保证不丢失数据。 同步,效率低,但可以保证数据的一致性;
async # 优先将数据保存到内存,必要时才写入磁盘,效率高,但有丢失数据的风险。 异步
/data/nfs *(rw,sync,no_root_squash) # * 表示允许任何网段 IP 的系统访问该 NFS 目录
服务状态检查和共享文件状态检查;
服务状态检查
root@debian:/home/a# systemctl status nfs-server
● nfs-server.service - NFS server and services
Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
Active: active (exited) since Wed 2024-08-14 13:04:59 CST; 53min ago
Process: 2342 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Process: 2343 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Main PID: 2343 (code=exited, status=0/SUCCESS)
CPU: 5ms
8月 14 13:04:58 debian systemd[1]: Starting NFS server and services...
8月 14 13:04:58 debian exportfs[2342]: exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' s>
8月 14 13:04:58 debian exportfs[2342]: Assuming default behaviour ('no_subtree_check').
8月 14 13:04:58 debian exportfs[2342]: NOTE: this default has changed since nfs-utils version 1.0.x
8月 14 13:04:59 debian systemd[1]: Finished NFS server and services.
nfs服务上共享文件状态检查
root@debian:/home/a# showmount -e
Export list for debian:
/data/share *
客户端侧配置
安装nfs客户端软件包,并开启服务
//查询是否安装nfs-utils和rpcbind,未装则安装
rpm -q nfs-utils|| yum -y install nfs-utils
rpm -q rpcbind || yum -y install rpcbind
[root@centos7 ~]# rpm -q nfs-utils
nfs-utils-1.3.0-0.68.el7.2.x86_64
[root@centos7 ~]# rpm -q rpcbind
rpcbind-0.2.0-49.el7.x86_64
//启动rpcbind和nfs
systemctl start rpcbind
systemctl start nfs
检查服务端共享目录状态
//查看NFS服务端共享的目录情况,假设服务端IP为192.168.255.129
[root@centos7 ~]# showmount -e 192.168.255.130
Export list for 192.168.255.130:
/data/share *
创建挂载点,将nfs共享目录挂在到本地,通过df命令检查是否挂载成功
[root@centos7 ~]# mkdir /opt/share #创建挂载点
[root@centos7 ~]# mount 192.168.255.130:/data/share/pv1 /opt/share/
[root@centos7 ~]# df -h /opt/share/
文件系统 容量 已用 可用 已用% 挂载点
192.168.255.130:/data/share/pv1 48G 6.7G 39G 15% /opt/share
文件共享效果验证
1.在服务侧/opt/share/pv1目录下创建一个文件,在客户端挂载点上查询是否能看到并读取对应文件内容(共享权限验证)
结果:符合预期,同步状态正常;
root@debian:/home/a# cd /data/share/pv1/
root@debian:/data/share/pv1# ls
root@debian:/data/share/pv1# echo this is nfs share test > nfs_test.txt
root@debian:/data/share/pv1# ls -l nfs_test.txt
-rw-r--r-- 1 root root 23 8月 14 14:15 nfs_test.txt
root@debian:/data/share/pv1#
[root@centos7 ~]# cd /opt/share/
[root@centos7 share]# ls
[root@centos7 share]# ls -l
总用量 0
[root@centos7 share]# ls -l
总用量 4
-rw-r--r-- 1 root root 23 8月 14 14:15 nfs_test.txt
[root@centos7 share]# cat nfs_test.txt
this is nfs share test
2.客户端切换为普通用户,尝试读取共享目录中的内容;
结果:符合预期,读写正常,同步状态正常;
[root@centos7 share]# su user1
[user1@centos7 share]$ ls
nfs_test.txt
[user1@centos7 share]$ cat nfs_test.txt
this is nfs share test
[user1@centos7 share]$ ls -l
总用量 4
-rw-r--r-- 1 root root 23 8月 14 14:15 nfs_test.txt
3.创建共享文件,检查在服务端侧是否同步;
[user1@centos7 share]$ echo "this is nfs client create txt" >nfs_client.txt
[user1@centos7 share]$ ls
nfs_client.txt nfs_test.txt
[user1@centos7 share]$
root@debian:/data/share/pv1# ls -l nfs_test.txt
-rw-r--r-- 1 root root 23 8月 14 14:15 nfs_test.txt
root@debian:/data/share/pv1# ls
nfs_client.txt nfs_test.txt
root@debian:/data/share/pv1# cat nfs_client.txt
this is nfs client create txt
root@debian:/data/share/pv1# ls -l
总用量 8
-rw-rw-r-- 1 a a 30 8月 14 14:20 nfs_client.txt
-rw-r--r-- 1 root root 23 8月 14 14:15 nfs_test.txt
root@debian:/data/share/pv1#