Linux中网络文件系统nfs使用

一、nfs服务

NFS(Network File System) 是一种用于在网络中共享文件的协议,允许不同操作系统(如 Linux、Unix、MacOS 等)之间进行文件共享。

NFS 的工作原理基于客户端-服务器模型,服务器提供共享文件系统,客户端通过挂载这些文件系统来访问共享的文件。

二、安装配置nfs

1、准备两台虚拟机。

本文使用centos7和 fedora 27系统版本

server:192.168.18.134

client:192.168.18.130

确保两台机器都安装了nfs服务

bash 复制代码
# rpm命令查看是否安装,没有安装直接yum安装
# 服务端
[server root ~] # ip a|grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.18.134/24 brd 192.168.18.255 scope global noprefixroute ens33
[server root ~] # rpm -qa|grep nfs-utils
[server root ~] # yum -y install nfs-utils



# 客户端
[nfsclient root ~] # ip a|grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    inet 192.168.18.130/24 brd 192.168.18.255 scope global ens33
[nfsclient root ~] # rpm -qa|grep nfs-utils
nfs-utils-2.1.1-6.rc5.fc27.x86_64

2、准备一个专门用作nfs的磁盘分区,格式化,挂载(server端)

bash 复制代码
# 这里使用sdb6作为nfs挂载点
[server root ~] # lsblk 
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   40G  0 disk 
├─sda1              8:1    0  512M  0 part /boot
├─sda2              8:2    0    4G  0 part [SWAP]
└─sda3              8:3    0 35.5G  0 part /
sdb                 8:16   0   20G  0 disk 
├─sdb1              8:17   0    5G  0 part 
├─sdb2              8:18   0    1K  0 part 
├─sdb5              8:21   0    5G  0 part 
└─sdb6              8:22   0    6G  0 part 
sr0                11:0    1  4.4G  0 rom  
[server root ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  900M     0  900M   0% /dev
tmpfs          tmpfs     910M     0  910M   0% /dev/shm
tmpfs          tmpfs     910M  9.6M  901M   2% /run
tmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/sda3      xfs        36G  3.7G   32G  11% /
/dev/sda1      xfs       509M  138M  371M  28% /boot
tmpfs          tmpfs     182M     0  182M   0% /run/user/0

# 格式化分区
[server root ~] # mkfs.ext4 /dev/sdb6 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
393216 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done                            
Writing inode tables: done                            
  eating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

创建目录,挂载

bash 复制代码
[server root ~] # mkdir /nfs-share
[server root ~] # mount /dev/sdb6  /nfs-share/
[server root ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  900M     0  900M   0% /dev
tmpfs          tmpfs     910M     0  910M   0% /dev/shm
tmpfs          tmpfs     910M  9.6M  901M   2% /run
tmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/sda3      xfs        36G  3.7G   32G  11% /
/dev/sda1      xfs       509M  138M  371M  28% /boot
tmpfs          tmpfs     182M     0  182M   0% /run/user/0
/dev/sdb6      ext4      5.8G   24M  5.5G   1% /nfs-share

编写exports配置文件,直接写192.168.18.0/24整个网段

bash 复制代码
[server root ~] # cat /etc/exports
/nfs-share 192.168.18.0/24(rw)

重启nfs服务,关闭防火墙,selinux设置为disabled

bash 复制代码
[server root ~] # systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: active (exited) since Wed 2024-11-27 16:51:15 CST; 8s ago
  Process: 7636 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 7619 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 7618 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 7619 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

Nov 27 16:51:15 server systemd[1]: Starting NFS server and services...
Nov 27 16:51:15 server systemd[1]: Started NFS server and services.
[server root ~] # systemctl stop firewalld
[server root ~] # setenforce 0
setenforce: SELinux is disabled

或者防火墙开启nfs访问,重新启动nfs服务

server root ~] # firewall-cmd --zone=public --add-service=nfs --permanent

success

[server root ~] # firewall-cmd --reload

success

[server root ~] # systemctl restart nfs-server

[server root ~] #

nfs服务已正常挂载,挂载目录为/nfs-share

bash 复制代码
[server root ~] # cat /etc/exports
/nfs-share 192.168.18.0/24(rw)
[server root ~] # exportfs 
/nfs-share    	192.168.18.0/24
[server root ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  900M     0  900M   0% /dev
tmpfs          tmpfs     910M     0  910M   0% /dev/shm
tmpfs          tmpfs     910M  9.6M  901M   2% /run
tmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/sda3      xfs        36G  3.7G   32G  11% /
/dev/sda1      xfs       509M  138M  371M  28% /boot
tmpfs          tmpfs     182M     0  182M   0% /run/user/0
/dev/sdb6      ext4      5.8G   24M  5.5G   1% /nfs-share

3、客户端访问

首先先创建目录,然后挂载服务端IP+目录

bash 复制代码
[nfsclient root ~] # df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  2.0G     0  2.0G   0% /dev
tmpfs                   tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                   tmpfs     2.0G  4.6M  2.0G   1% /run
tmpfs                   tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/mapper/fedora-root xfs        15G  5.2G  9.9G  35% /
tmpfs                   tmpfs     2.0G     0  2.0G   0% /tmp
/dev/nvme0n1p1          ext4       40G  182M   37G   1% /data
/dev/sda1               ext4      976M  117M  793M  13% /boot
tmpfs                   tmpfs     394M     0  394M   0% /run/user/0

[nfsclient root ~] # mkdir /nfs-mountpoint
[nfsclient root ~] # mount -t nfs 192.168.18.134:/nfs-share /nfs-mountpoint
[nfsclient root ~] # df -TH
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  2.1G     0  2.1G   0% /dev
tmpfs                     tmpfs     2.1G     0  2.1G   0% /dev/shm
tmpfs                     tmpfs     2.1G  4.9M  2.1G   1% /run
tmpfs                     tmpfs     2.1G     0  2.1G   0% /sys/fs/cgroup
/dev/mapper/fedora-root   xfs        17G  5.6G   11G  35% /
tmpfs                     tmpfs     2.1G     0  2.1G   0% /tmp
/dev/nvme0n1p1            ext4       43G  191M   40G   1% /data
/dev/sda1                 ext4      1.1G  123M  831M  13% /boot
tmpfs                     tmpfs     413M     0  413M   0% /run/user/0
192.168.18.134:/nfs-share nfs4      6.3G   26M  5.9G   1% /nfs-mountpoint

4、测试创建文件或者目录是否正常访问

bash 复制代码
# server端正常创建文件
[server root /nfs-share] # ls
lost+found
[server root /nfs-share] # touch test

# 客户端创建失败了
[nfsclient root /nfs-mountpoint] # ll
total 16
drwx------ 2 root root 16384 Nov 27 16:31 lost+found
-rw-r--r-- 1 root root     0 Nov 28 08:48 test
[nfsclient root /nfs-mountpoint] # touch test2
touch: cannot touch 'test2': Permission denied

因为服务端设置的other权限是r+x权限,没有写的权限,所有需要加w权限

bash 复制代码
[server root /nfs-share] # cat /etc/exports
/nfs-share 192.168.18.0/24(rw)
[server root /nfs-share] # ls -ld /nfs-share/
drwxr-xr-x 3 root root 4096 Nov 28 08:48 /nfs-share/
[server root /nfs-share] # ls -ld /nfs-share/
drwxr-xrwx 3 root root 4096 Nov 28 08:48 /nfs-share/


# 客户端重新创建文件
[nfsclient root /nfs-mountpoint] # touch test2
[nfsclient root /nfs-mountpoint] # ls
lost+found  test  test2
[nfsclient root /nfs-mountpoint] # ls -ld /nfs-mountpoint/
drwxr-xrwx 3 root root 4096 Nov 28 08:50 /nfs-mountpoint/
[nfsclient root /nfs-mountpoint] # mkdir KFC
[nfsclient root /nfs-mountpoint] # ls -l
total 20
drwxr-xr-x 2 nobody nobody  4096 Nov 28 09:45 KFC
drwx------ 2 root   root   16384 Nov 27 16:31 lost+found
-rw-r--r-- 1 root   root       0 Nov 28 08:48 test
-rw-r--r-- 1 nobody nobody     0 Nov 28 08:50 test2

5、配置永久挂载

客户端配置/etc/fstab,重启reboot

bash 复制代码
[nfsclient root /nfs-mountpoint] # vim /etc/fstab 

# 
# /etc/fstab
# Created by anaconda on Wed Dec 21 10:49:11 2022
#
# 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/fedora-root /                       xfs     defaults        0 0
UUID=8d0e84a0-5871-47cb-80b1-ccdc548d1443 /boot                   ext4    defaults        1 2
/dev/mapper/fedora-swap swap                    swap    defaults        0 0
/dev/nvme0n1p1 /data                        ext4    defaults        0 0
192.168.18.134:/nfs-share       /nfs-mountpoint nfs     defaults 0 0
~                                                                                              
~   


[nfsclient root /nfs-mountpoint] # reboot
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(fedora) at 08:52:25.

......
Connecting to 192.168.18.130:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Wed Nov 27 16:15:42 2024 from 192.168.18.1
[nfsclient root ~] # df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  2.0G     0  2.0G   0% /dev
tmpfs                     tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                     tmpfs     2.0G  4.6M  2.0G   1% /run
tmpfs                     tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/mapper/fedora-root   xfs        15G  5.2G  9.9G  35% /
tmpfs                     tmpfs     2.0G     0  2.0G   0% /tmp
/dev/nvme0n1p1            ext4       40G  182M   37G   1% /data
/dev/sda1                 ext4      976M  117M  793M  13% /boot
192.168.18.134:/nfs-share nfs4      5.8G   24M  5.5G   1% /nfs-mountpoint
                                                                                   
相关推荐
Elastic 中国社区官方博客16 分钟前
将自定义 AWS S3 快照存储库连接到 Elastic Cloud
大数据·运维·人工智能·elasticsearch·搜索引擎·云计算·aws
九河云21 分钟前
AWS账户是否支持区域划分?
服务器·云计算·aws
工业3D_大熊21 分钟前
如何在AWS中部署HOOPS Communicator?Docker容器化策略!
linux·c++·3d·docker·c#·云计算·aws
施嘉伟2 小时前
基于Linux的citus搭建标准化
linux·运维·数据库·postgresql·citus
誓约酱2 小时前
Linux 内核 调用堆栈打印函数
linux·运维·服务器·c++
->yjy2 小时前
系统性能定时监控Python&Linux
linux·开发语言·python
may-daydayup2 小时前
【Linux】Linux 内存管理机制
linux·运维
吖吖耶3332 小时前
【Linux】网络基本配置命令
linux·运维·网络
楚疏笃2 小时前
linux安全管理-会话安全
linux·前端·安全