NFS共享存储的搭建

搭建NFS所用配置清单

  1. rehl9虚拟机 ip: 192.168.96.100 【Server】
  2. rehl9虚拟机 ip: 192.168.96.101 【Client】
  3. 准备好本地yum源仓库

注意:本人使用的xshell远程连接,所以使用远程连接配置的伙计们,记得将/etc/ssh/sshd_config中PermitRootLogin 一行修改为yes

[root@Client ~]# vim /etc/ssh/sshd_config
#PermitRootLogin prohibit-password
PermitRootLogin yes
【rehl9的特殊之处】

使用NFS,绝大部分配置都在服务端,客户端操作较少,来吧,让我们开始吧


服务端操作


配置软件仓库 【注意保证镜像文件处于已连接的状态下】
[root@Server ~]# df  【由此可得,我们的镜像文件并没有挂载上去】
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs         1414992       0   1414992   0% /dev
tmpfs            1447688       0   1447688   0% /dev/shm
tmpfs             579076    8812    570264   2% /run
/dev/nvme0n1p3 102197500 6650956  95546544   7% /
/dev/nvme0n1p1    506528  270960    235568  54% /boot
tmpfs             289536     104    289432   1% /run/user/0
[root@Server ~]# echo "/dev/cdrom /mnt/cdrom auto defaults 0 0">> /etc/fstab 
【直接将挂载写入永久配置】
[root@Server ~]# mkdir /mnt/cdrom
[root@Server ~]# mount -a 
mount: /mnt/cdrom: WARNING: source write-protected, mounted read-only.
得到只读信息后,可通过df再次查看
[root@Server ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs         1414992       0   1414992   0% /dev
tmpfs            1447688       0   1447688   0% /dev/shm
tmpfs             579076    8812    570264   2% /run
/dev/nvme0n1p3 102197500 6650936  95546564   7% /
/dev/nvme0n1p1    506528  270960    235568  54% /boot
tmpfs             289536     104    289432   1% /run/user/0
/dev/sr0         8377364 8377364         0 100% /mnt/cdrom

由此可以看到,镜像文件已经被我们挂载在/mnt/cdrom目录上了
此时可以进入到/etc/yum.repos.d/下编写.repo结尾的仓库文件
[root@Server ~]# vim /etc/yum.repos.d/dvd.repo
[Baseos]
name=BaseOS
baseurl=file:///mnt/cdrom/BaseOS
enabled=1
gpgcheck=0
[AppStream]
name=AppStream
baseurl=file:///mnt/cdrom/AppStream
enabled=1
gpgcheck=0 
【此处密钥校验若想开启,只需在/mnt/cdrom/下找到密钥文件即可
RPM-GPG-KEY-redhat-beta
RPM-GPG-KEY-redhat-release
添加时,只需使用一个即可,推荐使用RPM-GPG-KEY-redhat-release
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release】

当软件仓库配置完毕的时候,就可以开始正式的NFS搭建工作了,配置NFS共享就是将当前服务端的某一个目录共享给客户端使用,使得客户端可以访问,编辑

搭建NFS需要使用软件包: nfs-utils
[root@Server ~]# dnf clean all && dnf makecache 【清除之前的缓存,加载新的缓存】
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

15 files removed
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

AppStream                                                                                                                             33 MB/s | 5.8 MB     00:00    
BaseOS                                                                                                                                16 MB/s | 1.7 MB     00:00    
Metadata cache created.

安装nfs-utils 
[root@Server ~]# dnf install nfs-utils -y
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

BaseOS                                                                                                                               550 kB/s | 2.7 kB     00:00    
AppStream                                                                                                                            1.3 MB/s | 2.8 kB     00:00    
Dependencies resolved.
=====================================================================================================================================================================
 Package                                    Architecture                       Version                                      Repository                          Size
=====================================================================================================================================================================
Installing:
 nfs-utils                                  x86_64                             1:2.5.4-10.el9                               Baseos                             454 k
Installing dependencies:
 gssproxy                                   x86_64                             0.8.4-4.el9                                  Baseos                             114 k
 keyutils                                   x86_64                             1.6.1-4.el9                                  Baseos                              68 k
 libev                                      x86_64                             4.33-5.el9                                   Baseos                              56 k
 libverto-libev                             x86_64                             0.3.2-3.el9                                  Baseos                              15 k
 rpcbind                                    x86_64                             1.2.6-2.el9                                  Baseos                              62 k

Transaction Summary
=====================================================================================================================================================================
Install  6 Packages

Total size: 769 k
Installed size: 1.8 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                             1/1 
  Running scriptlet: rpcbind-1.2.6-2.el9.x86_64                                                                                                                  1/6 
  Installing       : rpcbind-1.2.6-2.el9.x86_64                                                                                                                  1/6 
  Running scriptlet: rpcbind-1.2.6-2.el9.x86_64                                                                                                                  1/6 
Created symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service → /usr/lib/systemd/system/rpcbind.service.
Created symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket → /usr/lib/systemd/system/rpcbind.socket.

  Installing       : libev-4.33-5.el9.x86_64                                                                                                                     2/6 
  Installing       : libverto-libev-0.3.2-3.el9.x86_64                                                                                                           3/6 
  Installing       : gssproxy-0.8.4-4.el9.x86_64                                                                                                                 4/6 
  Running scriptlet: gssproxy-0.8.4-4.el9.x86_64                                                                                                                 4/6 
  Installing       : keyutils-1.6.1-4.el9.x86_64                                                                                                                 5/6 
  Running scriptlet: nfs-utils-1:2.5.4-10.el9.x86_64                                                                                                             6/6 
  Installing       : nfs-utils-1:2.5.4-10.el9.x86_64                                                                                                             6/6 
  Running scriptlet: nfs-utils-1:2.5.4-10.el9.x86_64                                                                                                             6/6 
  Verifying        : gssproxy-0.8.4-4.el9.x86_64                                                                                                                 1/6 
  Verifying        : keyutils-1.6.1-4.el9.x86_64                                                                                                                 2/6 
  Verifying        : libev-4.33-5.el9.x86_64                                                                                                                     3/6 
  Verifying        : libverto-libev-0.3.2-3.el9.x86_64                                                                                                           4/6 
  Verifying        : nfs-utils-1:2.5.4-10.el9.x86_64                                                                                                             5/6 
  Verifying        : rpcbind-1.2.6-2.el9.x86_64                                                                                                                  6/6 
Installed products updated.

Installed:
  gssproxy-0.8.4-4.el9.x86_64    keyutils-1.6.1-4.el9.x86_64    libev-4.33-5.el9.x86_64    libverto-libev-0.3.2-3.el9.x86_64    nfs-utils-1:2.5.4-10.el9.x86_64   
  rpcbind-1.2.6-2.el9.x86_64    
Complete

安装完毕后,在/etc/目录下,会出现一个exports的文件,从这里,将正式迈入NFS的大门

[root@Server ~]# vim /etc/exports
/rehl9_Server   192.168.96.101/32(rw)

/rehl9_Server 表示想要共享的目录
192.168.96.101/32(rw)表示只允许192.168.96.101这个ip访问,因为我子网掩码设置的是32,(rw)表示共享出去后,是可以读写

注:这里的子网掩码32表示只要允许一个ip访问,24表示允许一个网络段访问,*表示任何都可以访问
例:192.168.96.0/24 表示192.168.96这个网络段任何一个ip都可以
   *(rw) 表示所有主机都可以访问
   
   
创建要共享出去的目录   
[root@Server ~]# mkdir /rehl9_Server

重启nfs-server服务使配置文件生效
[root@Server ~]# systemctl restart nfs-server.service

此时,服务端配置基本配置完毕,但还有两个问题需要解决

1、防火墙

2、文件权限

我们先来解决第一个问题— 防火墙

如果对防火墙没有了解的,可以直接执行以下这条命令关闭防火墙
systemctl stop firewalld

如果对防火墙有所了解的,可以选择放行服务
【注意:放行服务一定要满足所有条件,少一个服务,客户端都无法访问服务端】
[root@Server ~]# firewall-cmd --list-all 【查看当前zone放行服务】
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@Server ~]# firewall-cmd --add-service=nfs --permanent 
success
[root@Server ~]# firewall-cmd --add-service=rpc-bind --permanent 
success
[root@Server ~]# firewall-cmd --add-service=mountd --permanent 
success
[root@Server ~]# firewall-cmd --reload
success
[root@Server ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client mountd nfs rpc-bind ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
放行服务后,不用特意放行端口,因为放行服务已经等于放行了端口


防火墙的问题解决后,第二个问题放缓,我们放到最后再说,这样可以更加直观的体现出来


客户端配置


第一步,我们可以先查看服务端哪些目录是共享的(此步骤可以略过)

重复配置软件仓库并安装nfs-utils软件包,此处不再阐述,具体步骤请参考服务端配置

通过showmount(nfs-utils软件包提供)可以查看服务端共享的目录
[root@Client ~]# showmount -e 192.168.96.100
Export list for 192.168.96.100:
/rehl9_Server 192.168.96.101/32

创建目录/Server_share作为挂载点

[root@Client ~]# mkdir /Server_share
[root@Client ~]# mount 192.168.96.100:/rehl9_Server /Server_share/
[root@Client ~]# df
Filesystem                   1K-blocks    Used Available Use% Mounted on
devtmpfs                       1863332       0   1863332   0% /dev
tmpfs                          1892788       0   1892788   0% /dev/shm
tmpfs                           757116   13892    743224   2% /run
/dev/nvme0n1p3               102197500 4690684  97506816   5% /
/dev/nvme0n1p1                  506528  250928    255600  50% /boot
tmpfs                           378556     132    378424   1% /run/user/0
/dev/sr0                       8377364 8377364         0 100% /mnt/cdrom
192.168.96.100:/rehl9_Server 102197760 6596096  95601664   7% /Server_share


到此,nfs已经基本搭建完成,这时,服务端的第二个问题也将在此体现

“权限”

[root@Client ~]# cd /Server_share/
[root@Client Server_share]# ls
[root@Client Server_share]# touch file1
touch: cannot touch 'file1': Permission denied

为什么此处会报权限拒绝呢?让我们回到客户端来看一下

[root@Server ~]# ll -d /rehl9_Server/
drwxr-xr-x. 2 root root 6 Jul  8 01:36 /rehl9_Server/

我们可以看到,此时的共享目录其他人并没有写的权限,所以,在我们赋予他写的权限后,便可以进行操作了

[root@Server ~]# chmod o+w /rehl9_Server/
[root@Server ~]# ll -d /rehl9_Server/
drwxr-xrwx. 2 root root 6 Jul  8 01:36 /rehl9_Server/

[root@Client Server_share]# touch file1
[root@Client Server_share]# mkdir dir1
[root@Client Server_share]# ll
total 0
drwxr-xr-x. 2 nobody nobody 6 Jul  8 01:58 dir1
-rw-r--r--. 1 nobody nobody 0 Jul  8 01:58 file1

如此,我们便完成了一整套nfs共享存储的搭建。

补充:若想让NFS在开机时挂载,可以选择写入进/etc/fstab目录下

[root@Client ~]# echo "192.168.96.100:/rehl9_Server /Server_share auto defaults,_netdev 0 0  ">> /etc/fstab 


如果开机时,通过df查看未自动挂载,那么,检查nfs-server服务是否启动

[root@Client ~]# 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: inactive (dead)

如果同此情况一样,开启服务即可
[root@Client ~]# systemctl enable --now nfs-server.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
【开机自启,并现在执行】

 服务端也可以执行上述代码,保证nfs顺利运行。

个人原创,请勿抄袭。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值