千家信息网

Linux Centos7 --- nfs共享存储空间

发表于:2024-09-23 作者:千家信息网编辑
千家信息网最后更新 2024年09月23日,本章目的:使用NFS共享存储空间,并搭建HTTPD服务去使用这个空间本章环境:VM虚拟机,一台服务器Centos7作为服务端提供NFS服务,一台服务器Centos7 -1作为客户端1.给服务端加一块硬
千家信息网最后更新 2024年09月23日Linux Centos7 --- nfs共享存储空间

本章目的:使用NFS共享存储空间,并搭建HTTPD服务去使用这个空间

本章环境:VM虚拟机,一台服务器Centos7作为服务端提供NFS服务,一台服务器Centos7 -1作为客户端

1.给服务端加一块硬盘(重启服务端磁盘才有效)

2.查看磁盘是否生效

[root@localhost ~]# ls /dev/agpgart          lp0                 sda5      tty22  tty48    usbmon0autofs           lp1                 sdb  //这个是我们新加的     tty23  tty49    usbmon1block            lp2                 sg0       tty24  tty5     usbmon2bsg              lp3                 sg1       tty25  tty50    vcsbtrfs-control    mapper              sg2       tty26  tty51    vcs1bus              mcelog              shm       tty27  tty52    vcs2cdrom            mem                 snapshot  tty28  tty53    vcs3char             mqueue              snd       tty29  tty54    vcs4console          net                 sr0       tty3   tty55    vcs5core             network_latency     stderr    tty30  tty56    vcs6cpu              network_throughput  stdin     tty31  tty57    vcsacpu_dma_latency  null                stdout    tty32  tty58    vcsa1

3.配置磁盘,格式化

[root@localhost ~]# fdisk /dev/sdb欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。使用写入命令前请三思。Device does not contain a recognized partition table使用磁盘标识符 0xe5bb6879 创建新的 DOS 磁盘标签。命令(输入 m 获取帮助):nPartition type:   p   primary (0 primary, 0 extended, 4 free)   e   extendedSelect (default p): p分区号 (1-4,默认 1):起始 扇区 (2048-41943039,默认为 2048):将使用默认值 2048Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):将使用默认值 41943039分区 1 已设置为 Linux 类型,大小设为 20 GiB命令(输入 m 获取帮助):wThe partition table has been altered!Calling ioctl() to re-read partition table.正在同步磁盘。[root@localhost ~]# mkfs.xfs /dev/sdb1meta-data=/dev/sdb1              isize=512    agcount=4, agsize=1310656 blks         =                       sectsz=512   attr=2, projid32bit=1         =                       crc=1        finobt=0, sparse=0data     =                       bsize=4096   blocks=5242624, imaxpct=25         =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=1log      =internal log           bsize=4096   blocks=2560, version=2         =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =none                   extsz=4096   blocks=0, rtextents=0

4.自动挂载并且查看挂载状态

[root@client html]# vim /etc/fstab

/etc/fstab Created by anaconda on Sat Aug 10 00:09:52 2019 Accessible filesystems, by reference, are maintained under '/dev/disk' See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more infoUUID=6885a615-3022-4e62-8685-1dc9aaa0b657 /                       xfs     defaults        0 0UUID=91d30663-7fe2-4989-9ad9-9f9c7086a493 /boot                   xfs     defaults        0 0UUID=121c8f14-d41e-4da5-a3eb-f973cde89040 /home                   xfs     defaults        0 0UUID=89bc11a2-b2cc-4816-9638-8f061beae396 swap                    swap    defaults        0 0/dev/sdb1       /mnt    xfs     defaults        0 0设备目录         挂载点    文件类型   ~                                                                                                 ~                                                                                                 ~                                                                                                 ~                                                                                                 ~                                                                                                 ~                                                                                                 ~                                                                                                 ~                                                                                                 ~                                                                                                 ~                                                                                                 ~                                                                                                 :wq  //保存退出
[root@localhost ~]# mount -a[root@localhost ~]# df -hT文件系统       类型      容量  已用  可用 已用% 挂载点/dev/sda2      xfs        10G  3.9G  6.2G   39% /devtmpfs       devtmpfs  898M     0  898M    0% /devtmpfs          tmpfs     912M     0  912M    0% /dev/shmtmpfs          tmpfs     912M  9.0M  903M    1% /runtmpfs          tmpfs     912M     0  912M    0% /sys/fs/cgroup/dev/sda5      xfs        10G   37M   10G    1% /home/dev/sda1      xfs       6.0G  174M  5.9G    3% /boottmpfs          tmpfs     183M  4.0K  183M    1% /run/user/42tmpfs          tmpfs     183M   16K  183M    1% /run/user/0/dev/sdb1      xfs        20G   33M   20G    1% /mnt

5.装NFS共享和远程调用工具包

[root@localhost ~]# yum install nfs-utils rpcbind -y   //NFS共享和远程调用工具包已加载插件:fastestmirror, langpacksLoading mirror speeds from cached hostfile * base: mirrors.163.com * extras: centos.ustc.edu.cn * updates: mirrors.163.com正在解决依赖关系--> 正在检查事务---> 软件包 nfs-utils.x86_64.1.1.3.0-0.48.el7 将被 升级---> 软件包 nfs-utils.x86_64.1.1.3.0-0.61.el7 将被 更新---> 软件包 rpcbind.x86_64.0.0.2.0-42.el7 将被 升级---> 软件包 rpcbind.x86_64.0.0.2.0-47.el7 将被 更新

6.编辑共享配置文件

[root@localhost ~]# vim /etc/exports  //到共享配置文件中开始配置/mnt    192.168.17.0/24(rw,sync,no_root_squash)   //可读可写,同步,不降级处理你共享出去的挂载点目录                                                      

7.开启所有功能关闭防火墙,查看有没有被共享出去

[root@localhost ~]# systemctl start nfs  //开启共享功能[root@localhost ~]# systemctl start rpcbind  //开启远程调用[root@localhost ~]# netstat -ntap | grep rpc   查看TCP端口tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      3035/rpc.mountd     tcp        0      0 0.0.0.0:45274           0.0.0.0:*               LISTEN      3033/rpc.statd      tcp6       0      0 :::38508                :::*                    LISTEN      3033/rpc.statd      tcp6       0      0 :::20048                :::*                    LISTEN      3035/rpc.mountd     [root@localhost ~]# systemctl stop firewalld.service //关闭防火墙[root@localhost ~]# setenforce 0  //关闭增强功能[root@localhost ~]# showmount -e  //查看我们有没有共享出去Export list for localhost.localdomain:/mnt 192.168.17.0/24

8.到客户段中,关闭所有功能,安装HTTPD服务

[root@client ~]# systemctl stop firewalld.service [root@client ~]# setenforce 0[root@client ~]# yum insatll httpd -y已加载插件:fastestmirror, langpacks

9.把服务器的挂载点,挂载到我们HTTPD的站点下

[root@client ~]# mount 192.168.17.128:/mnt /var/www/html/  //站点[root@client ~]# df -hT文件系统            类型      容量  已用  可用 已用% 挂载点/dev/sda2           xfs        20G  4.5G   16G   23% /devtmpfs            devtmpfs  898M     0  898M    0% /devtmpfs               tmpfs     912M     0  912M    0% /dev/shmtmpfs               tmpfs     912M  9.0M  903M    1% /runtmpfs               tmpfs     912M     0  912M    0% /sys/fs/cgroup/dev/sda3           xfs       9.4G   37M  9.3G    1% /home/dev/sda1           xfs       5.6G  174M  5.5G    4% /boottmpfs               tmpfs     183M  4.0K  183M    1% /run/user/42tmpfs               tmpfs     183M   24K  183M    1% /run/user/0/dev/sr0            iso9660   4.3G  4.3G     0  100% /run/media/root/CentOS 7 x86_64192.168.17.128:/mnt nfs4       20G   32M   20G    1% /var/www/html

10.使用另一种方法:自动挂载,配置

[root@client ~]# umount /var/www/html/   //先把之前的解挂[root@client ~]# df -hT文件系统       类型      容量  已用  可用 已用% 挂载点/dev/sda2      xfs        20G  4.5G   16G   23% /devtmpfs       devtmpfs  898M     0  898M    0% /devtmpfs          tmpfs     912M     0  912M    0% /dev/shmtmpfs          tmpfs     912M  9.0M  903M    1% /runtmpfs          tmpfs     912M     0  912M    0% /sys/fs/cgroup/dev/sda3      xfs       9.4G   37M  9.3G    1% /home/dev/sda1      xfs       5.6G  174M  5.5G    4% /boottmpfs          tmpfs     183M  4.0K  183M    1% /run/user/42tmpfs          tmpfs     183M   24K  183M    1% /run/user/0/dev/sr0       iso9660   4.3G  4.3G     0  100% /run/media/root/CentOS 7 x86_64[root@client ~]# vim /etc/fstab
 /etc/fstabCreated by anaconda on Fri Aug  9 19:16:53 2019 Accessible filesystems, by reference, are maintained under '/dev/disk' See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more infoUUID=0857ae22-72c1-4ad4-bd8d-333df1d5c1a4 /                       xfs     defaults        0 0UUID=2d335fb0-e4af-4a90-8777-1ce16ac58065 /boot                   xfs     defaults        0 0UUID=d28abede-d7a4-439a-b830-64f95069ec17 /home                   xfs     defaults        0 0UUID=1bc36e2d-3ba8-4980-9bf8-7b56280bb434 swap                    swap    defaults        0 0192.168.17.128:/mnt     /var/www/html   nfs     defaults,_netdev        0 0     [root@client ~]# mount -a[root@client ~]# df -hT文件系统            类型      容量  已用  可用 已用% 挂载点/dev/sda2           xfs        20G  4.5G   16G   23% /devtmpfs            devtmpfs  898M     0  898M    0% /devtmpfs               tmpfs     912M     0  912M    0% /dev/shmtmpfs               tmpfs     912M  9.0M  903M    1% /runtmpfs               tmpfs     912M     0  912M    0% /sys/fs/cgroup/dev/sda3           xfs       9.4G   37M  9.3G    1% /home/dev/sda1           xfs       5.6G  174M  5.5G    4% /boottmpfs               tmpfs     183M  4.0K  183M    1% /run/user/42tmpfs               tmpfs     183M   24K  183M    1% /run/user/0/dev/sr0            iso9660   4.3G  4.3G     0  100% /run/media/root/CentOS 7 x86_64192.168.17.128:/mnt nfs4       20G   32M   20G    1% /var/www/html

11.在站点中写一个网页,并开启HTTPD服务

[root@client ~]# cd /var/www/html/[root@client html]# ls[root@client html]# vim index.htmlh2>this is nfs~                                                                                      ~                          [root@client html]# cat index.html 

this is nfs

[root@client html]# systemctl start httpd

12.我们登录网站试一下(客户端的IP地址)

以上就是我们NFS共享存储的所有内容了

0