如何在 Linux 上永久挂载一个 Windows 共享

找不到分类 evilven 8个月前 (04-14) 92次浏览 已收录 0个评论 扫描二维码

如果你已经厌倦了每次重启 Linux 就得重新挂载 Windows 共享,读读这个让共享永久挂载的简单方法。

如何在 Linux 上永久挂载一个 Windows 共享

图片: Jack Wallen

在 Linux 上和一个 Windows 网络进行交互从来就不是件轻松的事情。想想多少企业正在采用 Linux,需要在这两个平台上彼此协作。幸运的是,有了一些工具的帮助,你可以轻松地将 Windows 网络驱动器映射到一台 Linux 机器上,甚至可以确保在重启 Linux 机器之后共享还在。

在我们开始之前

要实现这个,你需要用到命令行。过程十分简单,但你需要编辑 /etc/fstab 文件,所以小心操作。还有,我假设你已经让 Samba 正常工作了,可以手动从 Windows 网络挂载共享到你的 Linux 机器,还知道这个共享的主机 IP 地址。

准备好了吗?那就开始吧。

创建你的挂载点

我们要做的第一件事是创建一个文件夹,他将作为共享的挂载点。为了简单起见,我们将这个文件夹命名为 share,放在 /media 之下。打开你的终端执行以下命令:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">sudo</span><span class="pln" style="word-wrap: break-word;"> </span><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">mkdir</span><span class="pln" style="word-wrap: break-word;"> </span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">media</span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">share</span></code></p></li></ol>

安装一些软件

现在我们得安装允许跨平台文件共享的系统;这个系统是 cifs-utils。在终端窗口输入:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">sudo</span><span class="pln" style="word-wrap: break-word;"> </span><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">apt-get</span><span class="pln" style="word-wrap: break-word;"> install cifs</span><span class="pun" style="word-wrap: break-word;">-</span><span class="pln" style="word-wrap: break-word;">utils</span></code></p></li></ol>

这个命令同时还会安装 cifs-utils 所有的依赖。

安装完成之后,打开文件 /etc/nsswitch.conf 并找到这一行:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="pln" style="word-wrap: break-word;">hosts</span><span class="pun" style="word-wrap: break-word;">:</span><span class="pln" style="word-wrap: break-word;"> files mdns4_minimal </span><span class="pun" style="word-wrap: break-word;">[</span><span class="pln" style="word-wrap: break-word;">NOTFOUND</span><span class="pun" style="word-wrap: break-word;">=</span><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">return</span><span class="pun" style="word-wrap: break-word;">]</span><span class="pln" style="word-wrap: break-word;"> dns</span></code></p></li></ol>

编辑这一行,让它看起来像这样:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="pln" style="word-wrap: break-word;">hosts</span><span class="pun" style="word-wrap: break-word;">:</span><span class="pln" style="word-wrap: break-word;"> files mdns4_minimal </span><span class="pun" style="word-wrap: break-word;">[</span><span class="pln" style="word-wrap: break-word;">NOTFOUND</span><span class="pun" style="word-wrap: break-word;">=</span><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">return</span><span class="pun" style="word-wrap: break-word;">]</span><span class="pln" style="word-wrap: break-word;"> wins dns</span></code></p></li></ol>

现在你需要安装 windbind 让你的 Linux 机器可以在 DHCP 网络中解析 Windows 机器名。在终端里执行:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">sudo</span><span class="pln" style="word-wrap: break-word;"> </span><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">apt-get</span><span class="pln" style="word-wrap: break-word;"> install libnss</span><span class="pun" style="word-wrap: break-word;">-</span><span class="pln" style="word-wrap: break-word;">windbind windbind</span></code></p></li></ol>

用这个命令重启网络服务:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">sudo</span><span class="pln" style="word-wrap: break-word;"> service networking restart</span></code></p></li></ol>

挂载网络驱动器

现在我们要映射网络驱动器。这里我们必须编辑 /etc/fstab 文件。在你做第一次编辑之前,用这个命令备份以下这个文件:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">sudo</span><span class="pln" style="word-wrap: break-word;"> </span><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">cp</span><span class="pln" style="word-wrap: break-word;"> </span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">etc</span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">fstab </span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">etc</span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">fstab</span><span class="pun" style="word-wrap: break-word;">.</span><span class="pln" style="word-wrap: break-word;">old</span></code></p></li></ol>

如果你需要恢复这个文件,执行以下命令:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">sudo</span><span class="pln" style="word-wrap: break-word;"> </span><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">mv</span><span class="pln" style="word-wrap: break-word;"> </span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">etc</span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">fstab</span><span class="pun" style="word-wrap: break-word;">.</span><span class="pln" style="word-wrap: break-word;">old </span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">etc</span><span class="pun" style="word-wrap: break-word;">/</span><span class="pln" style="word-wrap: break-word;">fstab</span></code></p></li></ol>

在你的主目录创建一个认证信息文件 .smbcredentials。在这个文件里添加你的用户名和密码,就像这样(USER 和 PASSWORD 替换为实际的用户名和密码):

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;">
<li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="pln" style="word-wrap: break-word;">username</span><span class="pun" style="word-wrap: break-word;">=</span><span class="pln" style="word-wrap: break-word;">USER</span></code></p></li>
<li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"></code></p></li>
<li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="pln" style="word-wrap: break-word;">password</span><span class="pun" style="word-wrap: break-word;">=</span><span class="pln" style="word-wrap: break-word;">PASSWORD</span></code></p></li>
</ol>

你需要知道挂载这个驱动器的用户的组 ID(GID)和用户 ID(UID)。执行命令:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="kwd" style="word-wrap: break-word; color: rgb(226, 137, 100);">id</span><span class="pln" style="word-wrap: break-word;"> USER</span></code></p></li></ol>

USER 是你的实际用户名,你应该会看到类似这样的信息:

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="pln" style="word-wrap: break-word;">uid</span><span class="pun" style="word-wrap: break-word;">=</span><span class="lit" style="word-wrap: break-word; color: rgb(51, 135, 204);">1000</span><span class="pun" style="word-wrap: break-word;">(</span><span class="pln" style="word-wrap: break-word;">USER</span><span class="pun" style="word-wrap: break-word;">)</span><span class="pln" style="word-wrap: break-word;"> gid</span><span class="pun" style="word-wrap: break-word;">=</span><span class="lit" style="word-wrap: break-word; color: rgb(51, 135, 204);">1000</span><span class="pun" style="word-wrap: break-word;">(</span><span class="pln" style="word-wrap: break-word;">GROUP</span><span class="pun" style="word-wrap: break-word;">)</span></code></p></li></ol>

USER 是实际的用户名,GROUP 是组名。在(USER)和(GROUP)之前的数字将会被用在 /etc/fstab 文件之中。

是时候编辑 /etc/fstab 文件了。在你的编辑器中打开那个文件并添加下面这行到文件末尾(替换以下全大写字段以及远程机器的 IP 地址):

<ol class="linenums list-paddingleft-2" style="word-wrap: break-word; margin: 1em 2em; padding: 1px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(0, 153, 0); color: rgb(174, 174, 174); background-image: none;"><li><p><code style="word-wrap: break-word; text-shadow: rgb(0, 0, 0) 0px 1px 1px; color: rgb(184, 255, 184); padding-left: 10px !important; border-radius: 0px !important; margin-top: 1em !important; margin-bottom: 1em !important; border: none !important; background: none;"><span class="com" style="word-wrap: break-word; color: rgb(174, 174, 174); font-style: italic;">//192.168.1.10/SHARE /media/share cifs credentials=/home/USER/.smbcredentials,iocharset=uft8,gid=GID,udi=UID,file_mode=0777,dir_mode=0777 0 0</span></code></p></li></ol>

注意:上面这些内容应该在同一行上。

保存并关闭那个文件。执行 sudo mount -a 命令,共享就会挂载上。看看一下 /media/share,你应该能看到那个网络共享上的文件和文件夹了。

共享很简单

有了 cifs-utils 和 Samba,映射网络共享在一台 Linux 机器上简单得让人难以置信。现在,你再也不用在每次机器启动的时候手动重新挂载那些共享了。


via: http://www.techrepublic.com/article/how-to-permanently-mount-a-windows-share-on-linux/

作者:Jack Wallen[1] 译者:alim0x[2] 校对:wxy[3]

本文由 LCTT[4] 原创编译,Linux中国[5] 荣誉推出


推荐文章

点击标题或输入文章 ID 直达该文章

38 RHCE 系列(六):安装 Samba 并配置 Firewalld 和 SELinux,和 Windows 共享文件

3394 如何在 Linux 终端下创建新的文件系统/分区

3789 LFCS 系列第五讲:如何在 Linux 中挂载/卸载本地文件系统和网络文件系统(Samba 和 NF

如何在 Linux 上永久挂载一个 Windows 共享

将文章分享给朋友是对我们最好的赞赏!


Hacking For Fun , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何在 Linux 上永久挂载一个 Windows 共享
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址