自动挂载NFS文件系统到云服务器(Linux)
<p>为了使云服务器重启时仍然能够挂载NFS类型文件系统,您可以配置Linux系统的自动挂载。</p>
<p><strong><span style="font-size:18px">背景信息</span></strong></p>
<p>目前,有两种方式可以实现文件系统的自动挂载,您可以根据实际系统的情况选择更加适合您的挂载方式:</p>
<ul>
<li>方式一:将挂载信息写入/etc/fstab中,文件系统就会自动随着云服务器开机而自动挂载。此种方式操作简单便捷,但如果挂载的文件系统很多,会消耗服务器资源。如果长时间不使用文件系统,会造成资源的浪费,并且影响整个系统的性能。</li>
<li>方式二:使用Autofs方式挂载。使用这种方式挂载,在默认情况下,文件系统是处于卸载状态,直到用户访问该挂载点,才会将其自动挂载。如果用户一段时间内不再使用该文件系统,它又将自动进入卸载状态,有效地节省了系统资源。</li>
</ul>
<p><strong><span style="font-size:18px">前提条件</span></strong></p>
<p>在自动挂载前,您需要确保已经完成如下操作:</p>
<p>1. 创建ECS云服务器。</p>
<p>2. 创建CloudNAS文件系统。</p>
<p>3. 连接ECS云服务器。</p>
<p><strong><span style="font-size:18px">使用/etc/fstab方式实现自动挂载</span></strong></p>
<p>1. 以<strong>root</strong>用户登录ECS云服务器。</p>
<p>如果当前用户不是<strong>root</strong>用户,执行 <strong>sudo su </strong>命令,然后输入当前用户的密码。</p>
<p>2. 执行<strong>vi /etc/fstab</strong>命令打开 <strong>/etc/fstab </strong>文件。</p>
<p>3. 打开文件后,输入“<strong>i</strong>”,在 <strong>/etc/fstab </strong>文件下方,添加如下信息。</p>
<pre>
<code><挂载路径> <本地挂载目录> nfs vers=3,timeo=600,nolock 0 0</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></p>
<p>4. 单击ESC,并输入“<strong>:wq</strong>”,保存文件并退出。</p>
<p>5. 执行 <strong>cat /etc/fstab </strong>检查修改后的 <strong>/etc/fstab </strong>文件中是否已经有了挂载信息。</p>
<p>完成上述配置后,当ECS云服务器重启时,系统会从 <strong>/etc/fstab </strong>文件中读取挂载信息,完成文件系统的自动挂载。</p>
<p><strong><span style="font-size:18px">使用autofs方式实现自动挂载(Centos6.7)</span></strong></p>
<p>1. 以<strong>root</strong>用户登录ECS云服务器。</p>
<p>如果当前用户不是<strong>root</strong>用户,执行 <strong>sudo su </strong>命令,然后输入当前用户的密码。</p>
<p>2. 检查是否配置过automount。</p>
<pre>
<code>cat /etc/auto.nfsc</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></p>
<p> </p>
<p>查看云服务器是否挂载过其他文件系统。</p>
<pre>
<code>df –t nfs -Ph</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></p>
<p> </p>
<p>如果上述都没有配置过自动挂载,则需要执行步骤3,否则,直接步骤7。</p>
<p>3. 配置相关参数进行操作系统性能调优。</p>
<p>执行cat /etc/sysctl.conf命令查看/etc/sysctl.conf中是否存在如下配置。如果没有相关配置,则需要先备份文件,然后写入配置,并使配置生效。</p>
<pre>
<code>//备份sysctl.conf文件
cp -p /etc/sysctl.conf /etc/sysctl.conf.date +%F_%T
//写入如下配置
echo “net.core.rmem_default = 2621440” >> /etc/sysctl.conf
echo “net.core.wmem_default = 2621440” >> /etc/sysctl.conf
echo “net.core.rmem_max = 2621440” >> /etc/sysctl.conf
echo “net.core.wmem_max = 2621440” >> /etc/sysctl.conf
echo “net.ipv4.tcp_rmem = 4096 655360 2621440” >> /etc/sysctl.conf
echo “net.ipv4.tcp_wmem = 4096 655360 2621440” >> /etc/sysctl.conf
//使配置生效
sysctl -p</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>4. 检查portmap(rpcbind)和nfslock服务并配置</p>
<pre>
<code>//启动rpcbind服务
service rpcbind start
//查看rpcbind服务
service rpcbind status
//设置rpcbind服务开机自启动
chkconfig rpcbind on
//列出rpcbind服务信息
chkconfig --list rpcbind</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>5. 设置自动挂载。</p>
<pre>
<code>//备份自动挂载配置文件
cp -p /etc/auto.master /etc/auto.master.date +%F_%T
//写入配置
echo “/nfsc/etc/auto.nfsc –timeout=600” >> /etc/auto.master
//查看并启用配置:
chkconfig --list autofs #列出autofs服务信息
chkconfig autofs on #设置autofs服务开机自启动
chkconfig --list autofs #列出autofs服务信息
//开启服务
service autofs start</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>6. 配置auto.nfsc文件。</p>
<pre>
<code>//打开auto.nfsc文件
vi auto.nfsc
//输入i,写入如下信息
<本地挂载目录> -rw,soft,intr,rsize=32768,wsize=32768,tcp,timeo=300 <挂载路径 >
// 单击ECS,并输入“:wq”,保存文件并退出。
// 使配置生效
service autofs reload</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>7. 触发automount自动挂载。</p>
<pre>
<code>ls –dl <本地挂载目录></code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>8. 验证挂载是否成功。</p>
<pre>
<code>df –t nfs –Ph</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><strong><span style="font-size:18px">使用autofs方式实现自动挂载(CentOS7.2)</span></strong></p>
<p>1. 以 <strong>root </strong>用户登录ECS云服务器。</p>
<p>如果当前用户不是 <strong>root </strong>用户,执行 <strong>sudo su </strong>命令,然后输入当前用户的密码。</p>
<p>2. 检查是否配置过automount。</p>
<pre>
<code>cat /etc/auto.nfsc</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p> 查看云服务器是否挂载过其他文件系统。</p>
<pre>
<code>df –t nfs -Ph</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>如果上述都没有配置过,则需要执行步骤3,如果已经配置,则直接执行步骤7。</p>
<p>3. 执行cat /etc/sysctl.conf命令查看/etc/sysctl.conf中是否存在如下配置。如果没有相关配置,则需要先备份文件,然后写入配置,并使配置生效。</p>
<pre>
<code>//备份sysctl.conf文件
cp -p /etc/sysctl.conf /etc/sysctl.conf.date +%F_%T
//写入如下配置
echo “net.core.rmem_default = 2621440” >> /etc/sysctl.conf
echo “net.core.wmem_default = 2621440” >> /etc/sysctl.conf
echo “net.core.rmem_max = 2621440” >> /etc/sysctl.conf
echo “net.core.wmem_max = 2621440” >> /etc/sysctl.conf
echo “net.ipv4.tcp_rmem = 4096 655360 2621440” >> /etc/sysctl.conf
echo “net.ipv4.tcp_wmem = 4096 655360 2621440” >> /etc/sysctl.conf
//使配置生效
sysctl -p</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>4. 检查portmap(rpcbind)和nfslock服务并配置</p>
<pre>
<code>//启动rpcbind服务
systemctl start rpcbind
//查看rpcbind服务状态
systemctl status rpcbind
//设置开机启动rpcbind服务
systemctl enable rpcbind
//列出所有rpcbind类#型的unit
systemctl list-units –type=service grep rpcbind</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>5. 设置自动挂载。</p>
<pre>
<code>//备份自动挂载配置文件
cp -p /etc/auto.master /etc/auto.master.date +%F_%T
//写入配置
echo “/cloudnasclient /etc/auto.nfsc –timeout=60” >> /etc/auto.master
//查看并启用配置:
systemctl list-units –type=service grep autofs #列出autofs服务信息
systemctl enable autofs #设置autofs服务开机自启动
systemctl list-units –type=service grep autofs #列出autofs服务信息
//开启服务
systemctl autofs start</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>6. 配置auto.nfsc文件。</p>
<pre>
<code>//打开auto.nfsc文件
vi auto.nfsc
//输入i,写入如下信息
<本地挂载目录> -rw,soft,intr,rsize=32768,wsize=32768,tcp,timeo=300 <挂载路径>
//单击ECS,并输入“:wq”,保存文件并退出。
//使配置生效
systemctl restart autofs</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>7. 触发automount自动挂载。</p>
<pre>
<code>ls –dl <本地挂载目录></code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p>8. 验证挂载是否成功。</p>
<pre>
<code>df –t nfs -Ph</code></pre>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background-color:rgba(220,220,220,0.5)"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" style="height:15px; width:15px" /></span></p>
<p> </p>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!