Linux Centos7 --- nfs共享存儲空間

Linux  Centos7 --- nfs共享存儲空間

本章目的:使用NFS共享存儲空間,並搭建HTTPD服務去使用這個空間

本章環境:VM虛擬機,一臺服務器Centos7作爲服務端提供NFS服務,一臺服務器Centos7 -1作爲客戶端

1.給服務端加一塊硬盤(重啓服務端磁盤纔有效)

Linux  Centos7 --- nfs共享存儲空間

2.查看磁盤是否生效

[root@localhost ~]# ls /dev/
agpgart          lp0                 sda5      tty22  tty48    usbmon0
autofs           lp1                 sdb  //這個是我們新加的     tty23  tty49    usbmon1
block            lp2                 sg0       tty24  tty5     usbmon2
bsg              lp3                 sg1       tty25  tty50    vcs
btrfs-control    mapper              sg2       tty26  tty51    vcs1
bus              mcelog              shm       tty27  tty52    vcs2
cdrom            mem                 snapshot  tty28  tty53    vcs3
char             mqueue              snd       tty29  tty54    vcs4
console          net                 sr0       tty3   tty55    vcs5
core             network_latency     stderr    tty30  tty56    vcs6
cpu              network_throughput  stdin     tty31  tty57    vcsa
cpu_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 獲取幫助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分區號 (1-4,默認 1):
起始 扇區 (2048-41943039,默認爲 2048):
將使用默認值 2048
Last 扇區, +扇區 or +size{K,M,G} (2048-41943039,默認爲 41943039):
將使用默認值 41943039
分區 1 已設置爲 Linux 類型,大小設爲 20 GiB

命令(輸入 m 獲取幫助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盤。
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=1310656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242624, 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

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 info

UUID=6885a615-3022-4e62-8685-1dc9aaa0b657 /                       xfs     defaults        0 0
UUID=91d30663-7fe2-4989-9ad9-9f9c7086a493 /boot                   xfs     defaults        0 0
UUID=121c8f14-d41e-4da5-a3eb-f973cde89040 /home                   xfs     defaults        0 0
UUID=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% /dev
tmpfs          tmpfs     912M     0  912M    0% /dev/shm
tmpfs          tmpfs     912M  9.0M  903M    1% /run
tmpfs          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% /boot
tmpfs          tmpfs     183M  4.0K  183M    1% /run/user/42
tmpfs          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, langpacks
Loading 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% /dev
tmpfs               tmpfs     912M     0  912M    0% /dev/shm
tmpfs               tmpfs     912M  9.0M  903M    1% /run
tmpfs               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% /boot
tmpfs               tmpfs     183M  4.0K  183M    1% /run/user/42
tmpfs               tmpfs     183M   24K  183M    1% /run/user/0
/dev/sr0            iso9660   4.3G  4.3G     0  100% /run/media/root/CentOS 7 x86_64
192.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% /dev
tmpfs          tmpfs     912M     0  912M    0% /dev/shm
tmpfs          tmpfs     912M  9.0M  903M    1% /run
tmpfs          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% /boot
tmpfs          tmpfs     183M  4.0K  183M    1% /run/user/42
tmpfs          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/fstab
Created 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 info

UUID=0857ae22-72c1-4ad4-bd8d-333df1d5c1a4 /                       xfs     defaults        0 0
UUID=2d335fb0-e4af-4a90-8777-1ce16ac58065 /boot                   xfs     defaults        0 0
UUID=d28abede-d7a4-439a-b830-64f95069ec17 /home                   xfs     defaults        0 0
UUID=1bc36e2d-3ba8-4980-9bf8-7b56280bb434 swap                    swap    defaults        0 0
192.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% /dev
tmpfs               tmpfs     912M     0  912M    0% /dev/shm
tmpfs               tmpfs     912M  9.0M  903M    1% /run
tmpfs               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% /boot
tmpfs               tmpfs     183M  4.0K  183M    1% /run/user/42
tmpfs               tmpfs     183M   24K  183M    1% /run/user/0
/dev/sr0            iso9660   4.3G  4.3G     0  100% /run/media/root/CentOS 7 x86_64
192.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.html
h1>this is nfs</h1>
~                                                                                      
~                          
[root@client html]# cat index.html 
<h1>this is nfs</h1>
[root@client html]# systemctl start httpd

12.我們登錄網站試一下(客戶端的IP地址)

Linux  Centos7 --- nfs共享存儲空間

以上就是我們NFS共享存儲的所有內容了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章