如何挂载CIFS共享?

我正在使用Ubuntu 11.10,尝试挂载一个FreeNAS服务器。我已经将服务器设置为使用cifs和nfs进行共享,但没有成功。
我尝试过smbmount //192.168.1.### /mnt/。
我对Ubuntu并不陌生,但也不是高级用户,所以如果有GUI选项的话,我更倾向于使用它。
在11.10中如何挂载一个cifs共享?

有人解释过95错误可能是什么吗?消息显示为“操作不支持”,但没有说明具体是哪个“操作”。 - will
8个回答

有一个名为pyNeighborhood的图形界面,用于挂载Samba共享,并可在软件中心下载。

这里有一篇很好的文章位于此处,介绍如何安装和使用它。

首先安装CIFS工具。

sudo apt-get install cifs-utils

或者,基本的终端命令是:

mount -t cifs -o username=USERNAME,password=PASSWD //192.168.1.88/shares /mnt/share

如果您想在Nautilus中看到您的挂载点,最好先在/media/USERNAME/下创建一个子文件夹,例如:
mkdir /media/paul/cifsShare

此外,在mount命令中可以省略密码,例如(还将演示文件/文件夹模式):
sudo mount -t cifs //nas-server/cifsShare /media/paul/cifsShare -o username=paulOnNAS,iocharset=utf8,file_mode=0777,dir_mode=0777,soft,user,noperm

在这种情况下,您将在挂载时被要求输入密码(实际上是两个密码)。
请阅读此处的Samba文档,了解如何正确设置并在启动时进行挂载等操作。

3有没有一种方法可以在不硬编码密码和不需要成为root用户的情况下挂载Samba共享? - mcExchange
2@mcExchange 需要root权限,并且您可以使用smb凭据文件来保护您的凭据。 - codaamok
4请确保您已安装了cifs-utilssudo apt-get install cifs-utils。有关更多信息,请参阅此Ubuntu帮助文档 - user315004
1在Ubuntu 14.04上通过SSH启动pyNeighborhood时,会导致分段错误。 - Pavel Niedoba
1@MarcoPashkov cifs-utils 是让我能够启动的东西。否则,这一切都不会起作用。这应该直接包含在答案中。 - rubynorails
另外,确保使用SMBv2连接到较新的机器,例如:mount -t cifs ... -o vers=2.0,username=...否则可能会出现主机不可访问的错误。 - David Refoua
我之前收到“权限被拒绝”错误,只是因为我没有在用户名中包含域名。也不妨一提,域名\用户名 - Babak

正如map7所说的那样,但如果您不想每次更改驱动器上的文件时都使用root权限,那么您将需要将其挂载到用户文件夹,并确保gid和uid设置为您的用户名。
设置它们的命令:
mount -t cifs -o username=USERNAME,password=PASSWD,uid=$USER,gid=$USER //192.168.1.88/shares ~/mnt/share

请注意,mnt文件夹被创建在~/mnt/share而不是/mnt/share
此外,如果您希望命令提示您输入密码而不是将其存储在命令中,可以省略password=PASSWD。这样做可能会将密码存储在您的shell历史记录中。
mount -t cifs -o username=USERNAME,uid=$USER,gid=$USER //192.168.1.88/shares ~/mnt/share

1)我的Samba共享在Caja(Ubuntu 16.04的“资源管理器”)中显示为
smb://thinkpad/ddrive/

这是一个很好的试金石,没有连接或路径问题。
(注意:如果你在Windows机器上被caja询问密码凭据,你可能需要将域从WORKGROUP切换到机器的名称,例如'thinkpad'。然后你的驱动器的本地登录凭据应该有效。)
2)如果那样可以,下面是命令:
sudo mount -t cifs -o username=frank //thinkpad/ddrive /mnt/ddrive

请确保在执行命令之前,/mnt/ddrive目录存在且为空。
你也可以在username=后面直接添加,password=supersecret(无空格),但你也可以等待提示再输入该命令。

2我花了一点时间才弄清楚在Nemo / Linux Mint 18中如何输入smb://....路径,但实际上很简单:如果路径输入框不可见,请在“视图”菜单中启用它。 - Peter T.

  1. 当Linux和Windows之间的CIF/SMB版本不兼容时,可能会出现特定问题,这是非常令人沮丧的。在这种情况下,您只需在fstab行中添加"vers=2.1"来进行小的更改。

    因此,如果Windows或SMB服务器的IP地址为192.168.1.1

    /etc/fstab
    
    //192.168.1.1/SharedFolder/   /mnt/linux_smb   cifs   vers=2.1,username=winuser,password=TopSecret   0    0
    
  2. 步骤2、3和4与上一个答案相同。


1.你可以将所有这些细节放在/etc/fstab中,这样你就可以在系统启动时挂载目录。如果Windows或SMB服务器位于IP地址192.168.1.1上。
/etc/fstab //192.168.1.1/SharedFolder/ /mnt/linux_smb cifs username=winuser,password=TopSecret 0 0
2.创建Linux挂载点的目录
mkdir /mnt/linux_smb chmod 755 /mnt/linux_smb
3.第一次手动挂载它
mount -a
4.通过以下命令可以找到可能的错误
dmesg | tail

我不同意根据主张,要使cifs连接正常工作,总是需要root权限。确实,在使用CLI smbmount时,root权限是必需的,但像nautilus这样的文件管理器具有挂载cifs共享的能力,并不需要root权限。
我不使用Gnome,但我仍然安装了Nautilus。在终端中运行以下命令,以防止它试图接管桌面。
$ nautilus --no-desktop &

在Ubuntu 16.04中,左侧的树状菜单底部有一个"连接到服务器"选项。点击该选项,建议输入"smb://foo.example.com"。smb是"cifs"的旧称,如果在服务器和共享之前加上"smb://",连接就会成功!我保证。如果您的共享有一个名称,需要在斜杠后面添加,如"smb://foo.example.com/myshare"。
我曾经用过其他文件管理器,使用相同的方式。协议必须为"smb://"。

为什么Nautilus能够做到这一点:它使用mount.gvfs。 - dahe

我写了一个小脚本(虽然是为Fedora设计的),可以通过命令行挂载CIFS文件系统并创建/删除测试文件。可能会有一些用处。
#!/bin/bash
# Passes https://www.shellcheck.net/

set -o nounset

# See 
#   https://wiki.samba.org/index.php/Mounting_samba_shares_from_a_unix_client
#   https://access.redhat.com/solutions/448263
# and also
#   https://serverfault.com/questions/309429/mount-cifs-credentials-file-has-special-character

# One needs to run "yum install cifs-utils" to have the kernel module, man page
# and other stuff.

rpm --query cifs-utils > /dev/null

if [[ $? != 0 ]]; then
   echo "Package cifs-utils is not installed -- exiting" >&2
   exit 1
else 
   ver=$(rpm --query cifs-utils)
   echo "Package $ver exists ... good!" >&2
fi

# Where to find credentials? Use the "credential file" approach, which
# we call "authfile". Example content (w/o the leading #) below.
# Make sure there are no spaces around '=' (this is different than
# for "smbclient" which can deal with spaces around '='.)
# ----8<------8<----------------
# username=prisoner
# password=KAR120C
# domain=VILLAGE
# ----8<------8<----------------
# Trailing empty lines will lead to (harmless) error messages
# "Credential formatted incorrectly: (null)"

authfile='/etc/smb.passwd' # Make sure read permissions are restricted!!

# Server to contact.
# In the UNC path, we will use DNS name instead of the (more correct?)
# NetBIOS name.
# mount.cifs manpage says: "To mount using the cifs client, a tcp name
# (rather than netbios name) must be specified for the server."

server_dns=thedome.example.com

# The name of the connecting client, just to be sure (probably useless)

client_nbs=$(hostname --short | tr '[:lower:]' '[:upper]')

if [[ -z $client_nbs ]]; then
  client_nbs=UNKNOWN
fi

# Connect to a certain service (which is a fileservice)
# and then switch to the given directory.
# Instead of appending $directory to form the complete UNC
# (Uniform Naming Convention) path, one could also use the option 
# "prefixpath".
# If there is no need to mount a subdirectory of the service,
# the UNC would just be unc="//$server_dns/$service_name"

service_name='information'
directory='PERSONALDATA'

unc="//$server_dns/$service_name/$directory"

# Finally, we will mount the CIFS filesystem here (the
# permissions on that node are not directly of interest)

mntpoint=/mnt/portal

if [[ ! -d "$mntpoint" ]]; then
   mkdir "$mntpoint"
   if [[ $? != 0 ]]; then
      echo "Could not create mountpoint '$mntpoint' -- exiting" >&2
      exit 1
   fi
fi

# Only this user will be able to access the mounted CIFS filesystem

user=number6
group=number6

# Try to mount this so that only user "number6" can access it

mount -t cifs \
   "$unc" \
   "$mntpoint" \
   --read-write \
   --verbose \
   -o "credentials=$authfile,uid=$user,gid=$group,netbiosname=$client_nbs,file_mode=0660,dir_mode=0770"

res=$?

if [[ $res != 0 ]]; then
   echo "Mount failed!" >&2
   echo "Return code $res; more info may be in kernel log or daemon log" >&2
   echo "Try 'journalctl SYSLOG_FACILITY=0' or 'journalctl SYSLOG_FACILITY=3'" >&2
   echo "...exiting" >&2
   exit 1
fi

# Check permissions on the mount point

stat=$(stat --format="group=%G user=%U access=%A" "$mntpoint")
soll="group=$group user=$user access=drwxrwx---"

if [[ $stat != "$soll" ]]; then
   echo "Incorrect permissions on root of '$mntpoint'" >&2
   echo "Expected: $soll" >&2
   echo "Obtained: $stat" >&2
   echo "...exiting" >&2
   umount "$mntpoint"
   exit 1
fi

# CD to the mountpoint to be sure

cd "$mntpoint"

if [[ $? != 0 ]]; then
  echo "Could not cd to '$mntpoint'" >&2
  exit 1
fi

# CD to directory TEST which must exist (change as appropriate)

newcd="$mntpoint/TEST"

if [[ ! -d "$newcd" ]]; then
   echo "Directory '$newcd' not found - can't test!" >&2
   echo "...exiting" >&2
   exit 1
fi

cd "$newcd"

if [[ $? != 0 ]]; then
  echo "Could not cd to '$newcd'" >&2
  exit 1
fi

# Create a file and check the permissions

testfile=$(mktemp --tmpdir="$newcd")

if [[ $? != 0 ]]; then
   echo "Could not create temporary file in '$newcd'" >&2
   exit 1
fi

stat=$(stat --format="group=%G user=%U access=%A" "$testfile")
soll="group=$group user=$user access=-rw-rw----"

if [[ $stat != "$soll" ]]; then
   echo "Incorrect permissions on temporary file '$testfile'" >&2
   echo "Expected: $soll" >&2
   echo "Obtained: $stat" >&2
   echo "...exiting" >&2
   exit 1
fi

/bin/rm "$testfile"

echo "Mounted '$unc' on '$mntpoint'" >&2

如何使用不同的安装方法已经详尽讨论过了,但有一些你可能想考虑的事情。
如果你不想直接将凭据输入到/etc/fstab中,可以使用挂载选项代替: credentials=/your/path/here/.credentials
这个文件应该包含以下内容: username=msusername password=mspassword
保存文件并退出你选择的编辑器。
权限应该更改为: chmod 600
如果你有一个加密的主目录,并且希望在启动时进行挂载,请确保将文件放在你的主目录之外。 在/etc/或/media/中可能是一个合适且容易记住的位置。