CentOS 7

CentOS7下的各种应用


CentOS 7中搭建NFS文件共享存储服务

<p>[TOC]</p> <p>CentOS 7中搭建NFS文件共享存储服务的完整步骤</p> <h3>一 前言</h3> <p>NFS(Network File System)为网络文件系统,它最大的功能就是可以通过网络,让不同的机器不同的操作系统可以共享彼此的文件。简单的讲就是可以挂载远程主机的共享目录到本地,就像操作本地磁盘一样,非常方便的操作远程文件。 本文将给大家讲解如何在CentOS7上安装和配置NFS服务器。</p> <h3>二 准备</h3> <p>我们需要两台CentOS7机器,我们用虚拟机做测试,分别做NFS服务器和客户端,配置如下:</p> <pre><code>NFS服务器ip:192.168.1.230 客户端ip:192.168.1.231 # CentOS 7 客户端ip: 192.168.1.10 # Windows Server 2008 客户端ip: 172.16.3.8 # Windows Server 2012</code></pre> <p>我们要实现的目标是:在NFS服务器上共享一个目录,在客户端上可以直接操作NFS服务器上的这个共享目录下的文件。</p> <h3>三、NFS服务器配置</h3> <h4>1.安装NFS服务</h4> <p>首先使用yum安装nfs服务:</p> <pre><code class="language-bash">yum -y install nfs-utils</code></pre> <p>rpcbind包会自动被安装</p> <h4>2.创建共享目录</h4> <p>在服务器上创建共享目录,并设置权限。</p> <pre><code class="language-bash">mkdir -pv /data/8share mkdir -pv /data/10share mkdir -pv /data/8share #chmod 775 -R /data/</code></pre> <h4>3.配置NFS</h4> <p>nfs的配置文件是 /etc/exports ,在配置文件中加入一行:</p> <pre><code class="language-bash">/data/8share 192.168.1.8(no_subtree_check,rw,sync,all_squash) /data/10share 192.168.1.10(no_subtree_check,rw,sync,all_squash) /data/231share 192.168.1.231(no_subtree_check,rw,sync,all_squash)</code></pre> <p>注意:NFS服务端共享的目录不能太长,否则会报错,例如:</p> <pre><code class="language-bash">mount.nfs: access denied by server while mounting 192.168.30.123:/xtptdatavolumes</code></pre> <p>这行代码的意思是把共享目录/data/share/共享给192.168.1.231这个客户端ip,后面括号里的内容是权限参数,其中:</p> <pre><code>rw 表示设置目录可读写。 sync 表示数据会同步写入到内存和硬盘中,相反 rsync 表示数据会先暂存于内存中,而非直接写入到硬盘中。 all_squash 无论NFS客户端以哪种用户身份访问,均映射为NFS服务器的nfsnobody用户。</code></pre> <p>如果有多个共享目录配置,则使用多行,一行一个配置。保存好配置文件后,需要执行以下命令使配置立即生效:</p> <pre><code class="language-bash">exportfs -arv #启动nfs-server服务后才可执行此命令 或重启服务rpcbind和nfs</code></pre> <h4>4.将所有客户端用户均映射为nfsnobody</h4> <p>在NFS服务器执行如下命令:</p> <pre><code class="language-bash">setfacl -m u:nfsnobody:rwx /data/8share setfacl -m u:nfsnobody:rwx /data/10share setfacl -m u:nfsnobody:rwx /data/231share</code></pre> <h4>5.设置防火墙</h4> <pre><code class="language-bash">firewall-cmd --permanent --add-service=nfs firewall-cmd --permanent --add-service=rpc-bind firewall-cmd --permanent --add-service=mountd firewall-cmd --reload firewall-cmd --list-all #查看firewalld方向的资源</code></pre> <h4>6.启动服务</h4> <p>按顺序启动rpcbind和nfs服务:</p> <pre><code class="language-bash">systemctl start nfs 或 systemctl start nfs-server systemctl start rpcbind</code></pre> <p>加入开机启动:</p> <pre><code class="language-bash">systemctl enable rpcbind systemctl enable nfs</code></pre> <p>nfs服务启动后,可以使用命令 rpcinfo -p 查看端口是否生效。</p> <h4>7.查看服务端已共享的目录</h4> <p>以使用 showmount 命令来查看服务端(本机)是否可连接:</p> <pre><code class="language-bash">[root@localhost data]# showmount -e localhost Export list for localhost: /data/231share 192.168.1.231 /data/10share 192.168.1.10 /data/8share 192.168.1.8</code></pre> <h4>.8 修改/etc/exports文件后,生效命令</h4> <pre><code class="language-bash">exportfs -rv</code></pre> <p>出现上面结果表明NFS服务端配置正常。</p> <h3>四.客户端配置</h3> <h4>1.CentOS7下客户端安装</h4> <p>客户端只需要安装rpcbind服务即可,无需安装nfs或开启nfs服务。</p> <pre><code class="language-bash">yum -y install nfs-utils</code></pre> <h4>2.CentOS7下挂载远程nfs文件系统</h4> <p>(1)在服务端查看服务端已共享的目录:</p> <pre><code class="language-bash">[root@localhost data]# showmount -e localhost Export list for localhost: /data/231share 192.168.1.231 /data/10share 192.168.1.10 /data/8share 192.168.1.8</code></pre> <p>或在客户端查看服务器已经共享的目录,命令:</p> <pre><code class="language-bash">[root@localhost dir]# showmount -e 192.168.1.230 Export list for 192.168.1.230: /data/231share 192.168.1.231 /data/10share 192.168.1.10 /data/8share 192.168.1.8</code></pre> <p>(2)建立挂载目录,执行挂载命令:</p> <pre><code class="language-bash">mkdir -p /mnt/dir mount -t nfs 192.168.1.230:/data/share /opt/dir/ -o nolock,nfsvers=3,vers=3</code></pre> <p>如果不加 -nolock,nfsvers=3 则在挂载目录下的文件属主和组都是nobody,如果指定nfsvers=3则显示root。 如果要解除挂载,可执行命令:</p> <pre><code class="language-bash">umount /mnt/dir</code></pre> <h4>3.CentOS7下开机自动挂载</h4> <p>如果按本文上面的部分配置好,NFS即部署好了,但是如果你重启客户端系统,发现不能随机器一起挂载,需要再次手动操作挂载,这样操作比较麻烦,因此我们需要设置开机自动挂载。我们不要把挂载项写到/etc/fstab文件中,因为开机时先挂载本机磁盘再启动网络,而NFS是需要网络启动后才能挂载的,所以我们把挂载命令写入到/etc/rc.d/rc.local文件中即可。 (1)在客户端(172.16.3.8)添加:</p> <pre><code class="language-bash">[root@localhost ~]# vim /etc/rc.d/rc.local #在文件最后添加一行: mount -t nfs 192.168.1.230:/data/8share /opt/dir/ -o nolock,nfsvers=3,vers=3</code></pre> <p>(2)在客户端(192.168.1.10)添加:</p> <pre><code class="language-bash">[root@localhost ~]# vim /etc/rc.d/rc.local #在文件最后添加一行: mount -t nfs 192.168.1.230:/data/10share /opt/dir/ -o nolock,nfsvers=3,vers=3</code></pre> <p>(3)在客户端(192.168.1.231)添加:</p> <pre><code class="language-bash">[root@localhost ~]# vim /etc/rc.d/rc.local #在文件最后添加一行: mount -t nfs 192.168.1.230:/data/231share /opt/dir/ -o nolock,nfsvers=3,vers=3</code></pre> <p>(4) 给rc.local授可执行权限 chmod +x /etc/rc.d/rc.local</p> <p>保存并重启机器验证。</p> <h4>4.Windows 下安装NFS客户端</h4> <p>(1)在Windows Server 2008 下安装NFS客户端</p> <pre><code class="language-bash">servermanagercmd.exe -install FS-NFS-Services</code></pre> <p>(2)在Windows Server 2012/2016 下安装NFS客户端</p> <pre><code class="language-bash">添加角色和功能-&amp;gt;添加打印和文件服务-&amp;gt;选择nfs客户端</code></pre> <p>(3)在Windows 7和Windwos 10下直接添加系统服务</p> <pre><code class="language-bash">开始-&amp;gt;控制面板-&amp;gt;程序和功能-&amp;gt;打开或关闭Windows功能-&amp;gt;在功能列表中选择NFS服务-&amp;gt;勾选该选项下的所有选项,点击“确定”按钮,保存设置。</code></pre> <h4>5.Windows下挂载远程nfs文件系统</h4> <pre><code class="language-bash">mount \\192.168.1.230\data\share z: 或: mount 192.168.1.230:/data/share z:</code></pre> <h4>6.修改Windows对nfs分区只读属性</h4> <p>说明:Windows Server 2016 不需要做这一步 Windows的NFS客户端使用mount命令挂载NFS服务之后,文件系统对Win7只读,无法写入文件,无法新建文件夹,此时使用mount命令可以查看到如下状态: <img src="https://www.showdoc.cc/server/api/common/visitfile/sign/46d3672bb17c12a5d8a84b368f9161dd?showdoc=.jpg" alt="" /> UID=-2在存储设备上共享出来的NFS文件系统归属于root权限,无法修改该所属用户,Windows通过UID=-2的用户去写,写不进去。</p> <pre><code class="language-bash">regedit(打开注册表)</code></pre> <p>让Windows在挂载NFS的时候将UID和GID改成0即可:</p> <pre><code class="language-bash">打开注册表:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default, 增加两项:AnonymousUid,AnonymousGid,如图:</code></pre> <p><img src="https://www.showdoc.cc/server/api/common/visitfile/sign/e151381b37d4e3c75fa4418e26cdc2bf?showdoc=.jpg" alt="" /></p> <h4>7.重启计算机</h4> <p>上述设置后,需重启Windows.</p> <h4>8.在cmd内输入mount 查看属性</h4> <p><img src="https://www.showdoc.cc/server/api/common/visitfile/sign/c0c8db41721dd84edf140a2daa3746c8?showdoc=.jpg" alt="" /> UID=0,GID=0.</p> <h4>9.在Window下设置随机挂载NFS</h4> <p>这些方法都需要登录windwos才会执行。所以,在备份数据时,把nfs-mount.bat加入到计划任务中。在nfs-mount.bat脚本中,先执行mount,再执行robocopy操作。 (1)方法一:写一个bat文件,nfs-mount.bat 内容如下:</p> <pre><code class="language-bash">mount \\192.168.1.230\data\8share z: robocopy D:\SCM_SVN\Repositories\dm Z:\DM /E umount z:</code></pre> <p>(3)注意: 下面的方法只有在登录windows后,才会去挂载,不建议。 方法二: 开始-&gt;运行中输入:shell:startup 拷贝需要开机启动的程序的快捷方式或bat文件到此文件夹即可。</p> <p>方法二:映射网络驱动器</p> <pre><code class="language-bash">若要开机自动挂载可以点击“计算机” → 点击“映射网络驱动器” → “输入网络共享文件路径“ → “完成”</code></pre> <h3>五.测试验证</h3> <p>查看挂载结果,在客户端输入 df -h</p> <pre><code class="language-bash">[root@localhost dir]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 8.9M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/mapper/cl-root 50G 1.8G 49G 4% / /dev/sda1 1014M 224M 791M 23% /boot /dev/mapper/cl-home 441G 33M 441G 1% /home tmpfs 783M 0 783M 0% /run/user/0 192.168.1.230:/data/8share 48G 1.5G 47G 4% /opt/dir tmpfs 783M 0 783M 0% /run/user/1000</code></pre> <p>看到最后一行了没,说明已经挂载成功了。 接下来就可以在客户端上进入目录/opt/dir下,新建/删除文件,然后在服务端的目录/data/8share查看是不是有效果了. 同样反过来在服务端操作在客户端对应的目录下看效果。</p> <h3>六、关机</h3> <p>在关机时,一直关不掉,并报错,在尝试多次后发现,在停止Linux时,要先把存储挂的目录卸载。</p> <h4>1、创建 自定义服务随机停止 脚本:</h4> <pre><code class="language-bash">vim /etc/systemd/system/shutdown.target.wants/stopSvc.service</code></pre> <h4>2、自定义服务随机停止内容如下:</h4> <pre><code class="language-bash">[Unit] Description=close services before reboot and shutdown DefaultDependencies=no #Before=shutdown.target reboot.target halt.target Before=network.target iscsi.service iscsid.service # This works because it is installed in the target and will be # executed before the target state is entered # Also consider kexec.target [Service] Type=oneshot ExecStart=/opt/.umntNASNFS.sh [Install] WantedBy=network.target iscsi.service iscsid.service</code></pre> <h4>3、卸载存储设置的脚本(.umntNASNFS.sh)内容如下:</h4> <pre><code class="language-bash">#! /bin/bash umount /myNAS umount /myNFS</code></pre> <h3>七、附参考资料</h3> <h4>1、CentOS7配置nfs共享存储服务</h4> <p><a href="https://www.cnblogs.com/linuxprobe/p/11532859.html">https://www.cnblogs.com/linuxprobe/p/11532859.html</a></p> <h4>2、CentOS 7中搭建NFS文件共享存储服务的完整步骤</h4> <p><a href="https://www.centos.bz/2018/11/centos-7%E4%B8%AD%E6%90%AD%E5%BB%BAnfs%E6%96%87%E4%BB%B6%E5%85%B1%E4%BA%AB%E5%AD%98%E5%82%A8%E6%9C%8D%E5%8A%A1%E7%9A%84%E5%AE%8C%E6%95%B4%E6%AD%A5%E9%AA%A4/">https://www.centos.bz/2018/11/centos-7%E4%B8%AD%E6%90%AD%E5%BB%BAnfs%E6%96%87%E4%BB%B6%E5%85%B1%E4%BA%AB%E5%AD%98%E5%82%A8%E6%9C%8D%E5%8A%A1%E7%9A%84%E5%AE%8C%E6%95%B4%E6%AD%A5%E9%AA%A4/</a></p> <h4>3、Windows Client</h4> <p>3.1、Windows挂载NFS文件系统 <a href="https://www.cgtblog.com/wljs/1537.html">https://www.cgtblog.com/wljs/1537.html</a> 3.2、Windows Server 2008 访问NFS共享存储 <a href="https://blog.csdn.net/gis_luq/article/details/50686823">https://blog.csdn.net/gis_luq/article/details/50686823</a> 3.3、windows server 2008r2 访问NFS存储 <a href="https://blog.csdn.net/congtantan4465/article/details/100472514">https://blog.csdn.net/congtantan4465/article/details/100472514</a></p> <h4>4、权限问题:</h4> <p>4.1、【已解决】NFS客户端写入NFS共享文件夹出错:Permission denied <a href="https://blog.csdn.net/xiyang_1990/article/details/78284093">https://blog.csdn.net/xiyang_1990/article/details/78284093</a> <a href="https://blog.51cto.com/14375810/2427482">https://blog.51cto.com/14375810/2427482</a></p> <p>4.2、NFS服务的用户身份映射 <a href="https://blog.51cto.com/kusorz/2407716?source=dra">https://blog.51cto.com/kusorz/2407716?source=dra</a> 从安全角度考虑,建议使用nfsnobody身份映射。</p> <h4>5、robocopy使用说明</h4> <p>在Windows系统中添加计划任务,让系统定时的时候自动进行操作。 5.1、创建目录D:\SVNBackUp、存放日志的目录D:\SVNBackUp\Log_RobocopyTOmyNFS 5.2、创建批处理文件:D:\SVNBackUp\RobocopyTOmyNFS.bat,内容如下:</p> <pre><code class="language-bash">:: 挂载盘符 mount \\172.16.3.3\myNFS\YunMaSVN013 z:\ :: 备份文件 robocopy D:\SCM_SVN\Repositories z:\Repositories /v /mir /LOG:D:\Log_RobocopyTOmyNFS\%date:~0,4%-%date:~5,2%-%date:~8,2%.log /TEE /R:10 /w:10 /NP :: 保留14天日志 forfiles /p &amp;quot;D:\Log_RobocopyTOmyNFS&amp;quot; /s /m *.log /d -14 /c &amp;quot;cmd /c del @path&amp;quot; :: 卸载盘符 umount z:\</code></pre> <h3>八、报错信息</h3> <p>关机时报错-01,如下: <img src="https://www.showdoc.cc/server/api/common/visitfile/sign/fc0407f2e7976c43e9156ddca7a2e64b?showdoc=.jpg" alt="" /> 解决办法: 在主机的/etc/hosts文件中增加一行名称解析內容: 增加下面这句:目标板IP, nfs共享文件夹 172.16.3.1 /myNFS DELL存储服务器 172.16.5.99 /myNAS 威联通NAS</p> <p>比如:我的/etc/hosts增加后变为: [root@NFS ~]# cat /etc/hosts 172.16.3.3 NFS.localdomain 172.16.3.1 /myNFS 172.16.5.99 /myNAS</p> <h3>九、其它</h3> <p>1、Ubuntu 16.04 NFS搭建 <a href="https://www.cnblogs.com/vincenshen/p/7911765.html">https://www.cnblogs.com/vincenshen/p/7911765.html</a></p>

页面列表

ITEM_HTML