Linux--详解NFS服务的原理与配置
一:NFS共享存储服务
二:服务端配置
2.1:准备一块硬盘作为共享目录
2.2:更改共享配置文件
2.3:启动nfs服务
2.4:查看共享目录
三:客服端配置
3.1:挂载共享目录
3.2:创建文件
3.3:验证文件位置是否在共享目录
一:NFS共享存储服务
NFS(Network File System)网络文件系统
依赖于RPC(远程过程调用)
需安装nfs-utils、rpcbind软件包
系统服务:nfs、rpcbind
共享配置文件:/etc/exports
二:服务端配置
版本:CentOS7
2.1:准备一块硬盘作为共享目录
[root@server ~]# fdisk -l
磁盘 /dev/sda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ca0e8
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 104857599 51379200 8e Linux LVM
磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-root:50.5 GB, 50457477120 字节,98549760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@server ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x012f175a 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
分区号 (1-4,默认 1):
起始 扇区 (2048-10485759,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759):
将使用默认值 10485759
分区 1 已设置为 Linux 类型,大小设为 5 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@server ~]# mkfs.
mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.fat mkfs.minix mkfs.msdos mkfs.vfat mkfs.xfs
[root@server ~]# mkfs.xfs /dev/sd
sda sda1 sda2 sdb sdb1
[root@server ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=327616 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310464, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@server ~]# mkdir /share
[root@server ~]# vim /etc/fstab
[root@server ~]# mount -a
[root@server ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 47G 8.7G 39G 19% /
devtmpfs devtmpfs 470M 0 470M 0% /dev
tmpfs tmpfs 487M 0 487M 0% /dev/shm
tmpfs tmpfs 487M 8.5M 478M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 166M 849M 17% /boot
tmpfs tmpfs 98M 24K 98M 1% /run/user/0
/dev/sr0 iso9660 4.3G 4.3G 0 100% /run/media/root/CentOS 7 x86_64
/dev/sdb1 xfs 5.0G 33M 5.0G 1% /share
[root@server ~]#
2.2:更改共享配置文件
[root@server ~]# vim /etc/exports
/share 192.168.209.0/24(rw,sync,no_root_squash)
2.3:启动nfs服务
[root@server ~]# systemctl stop firewalld
[root@server ~]# setenforce 0
[root@server ~]# systemctl start rpcbind
[root@server ~]# systemctl start nfs
[root@server ~]# systemctl enable rpcbind
[root@server ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
2.4:查看共享目录
[root@server share]# showmount -e
Export list for server:
/share 192.168.209.0/24
三:客服端配置
3.1:挂载共享目录
[root@client ~]# vim /etc/fstab
[root@client ~]# systemctl stop firewalld.service
[root@client ~]# setenforce 0
[root@client ~]# mount -a
[root@client ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 17G 3.4G 14G 20% /
devtmpfs devtmpfs 977M 0 977M 0% /dev
tmpfs tmpfs 993M 0 993M 0% /dev/shm
tmpfs tmpfs 993M 8.9M 984M 1% /run
tmpfs tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sr0 iso9660 3.8G 3.8G 0 100% /mnt
/dev/sda1 xfs 1014M 161M 854M 16% /boot
tmpfs tmpfs 199M 24K 199M 1% /run/user/0
192.168.209.134:/share nfs4 5.0G 33M 5.0G 1% /var/www/html
3.2:创建文件
[root@client ~]# cd /var/www/html
[root@client html]# vim index.html
this is nfs web
[root@client html]# systemctl start httpd
3.3:验证文件位置是否在共享目录
[root@server ~]# cd /share
[root@server share]# ls
index.html