是否可以从 /etc/fstab 自动挂载本地 SMB/CIFS 共享?

是否可以从 /etc/fstab 自动挂载本地 SMB/CIFS 共享?

我有一台 Ubuntu 16.04 LTS 服务器,上面配置了公共共享。
我的/etc/samba/smb.conf在下面

[public_share]
    comment = Ubuntu Public Share
    path = /srv/samba/public_share
    guest ok = yes
    read only = no

由于某些原因,我需要在启动时安装此共享/媒体/public_share。所以我把它添加到/etc/fstab

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
//localhost/public_share /media/public_share/ cifs guest,noperm 0 2

但它不会在启动时安装。它只能在sudo mount -a登录后手动安装。

将其添加到/etc/rc.local例如

mount -t cifs //localhost/public_share /media/public_share/ -o guest,noperm

也无济于事。

我做错了什么?
如何自动挂载//本地主机/public_share/媒体/公共共享/在启动过程中?
我需要特殊的 systemd 配置还是其他什么特别的东西?

更新 1: 为了本地主机服务器名称我有以下日志:

$ systemctl status media-public_share.mount 
● media-public_share.mount - /media/public_share
   Loaded: loaded (/etc/fstab; bad; vendor preset: enabled)
   Active: failed (Result: exit-code) since Вт 2017-12-19 22:15:05 MSK; 16min ago
    Where: /media/public_share
     What: //localhost/public_share
     Docs: man:fstab(5)
           man:systemd-fstab-generator(8)
  Process: 1461 ExecMount=/bin/mount //localhost/public_share /media/public_share -t cifs -o guest,noperm (code=exited, status=32)

дек 19 22:15:05 hostname systemd[1]: Mounting /media/public_share...
дек 19 22:15:05 hostname mount[1461]: Unable to find suitable address.
дек 19 22:15:05 hostname systemd[1]: media-public_share.mount: Mount process exited, code=exited status=32
дек 19 22:15:05 hostname systemd[1]: Failed to mount /media/public_share.
дек 19 22:15:05 hostname systemd[1]: media-public_share.mount: Unit entered failed state.

将地址更改为 127.0.0.1,或使用选项设置 127.0.0.1username=guest,password=,noperm不会删除“无法找到合适的地址”消息。

我搬家了更新 2更新 3回答。

答案1

可以使用下列解决方案之一来解决问题:

  • 快速破解/etc/network/if-up.d

    cat <<EOF | sudo tee /etc/network/if-up.d/zz-mount-all
    #!/bin/sh
    mount -a
    
    EOF
    
    sudo chmod a+x /etc/network/if-up.d/zz-mount-all
    

经过此更改后,它可以正常安装:

$ systemctl status media-public_share.mount 
● media-public_share.mount - /media/public_share
   Loaded: loaded (/etc/fstab; bad; vendor preset: enabled)
   Active: active (mounted) (Result: exit-code) since Вт 2017-12-19 23:17:34 MSK; 7s ago
    Where: /media/public_share
     What: //127.0.0.1/public_share
     Docs: man:fstab(5)
           man:systemd-fstab-generator(8)
  Process: 1481 ExecMount=/bin/mount //127.0.0.1/public_share /media/public_share -t cifs -o guest,noperm (code=exited, status=32)

即使使用本地主机地址它也能工作。

  • 如果我x-systemd.automount,noauto添加/etc/fstab作为

     //localhost/public_share /media/public_share/ cifs guest,noperm,x-systemd.automount,noauto 0 255
    

它会创建两个 systemd 单元:

$ systemctl status media-public_share.*
● media-public_share.mount - /media/public_share
   Loaded: loaded (/etc/fstab; bad; vendor preset: enabled)
   Active: active (mounted) since Ср 2017-12-20 20:33:10 MSK; 3min 4s ago
    Where: /media/public_share
     What: //localhost/public_share
     Docs: man:fstab(5)
           man:systemd-fstab-generator(8)
  Process: 2461 ExecMount=/bin/mount //localhost/public_share /media/public_share -t cifs -o guest,noperm,x-systemd.automount (code=exited, status=0/SUCCESS)

дек 20 20:33:10 hostname systemd[1]: Mounting /media/public_share...
дек 20 20:33:10 hostname systemd[1]: Mounted /media/public_share.

● media-public_share.automount
   Loaded: loaded (/etc/fstab; bad; vendor preset: enabled)
   Active: active (running) since Ср 2017-12-20 20:33:02 MSK; 3min 12s ago
    Where: /media/public_share
     Docs: man:fstab(5)
           man:systemd-fstab-generator(8)

дек 20 20:33:09 hostname systemd[1]: media-public_share.automount: Got automount request for /media/public_share, triggered by 2456 (pool)
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavail

但是这个解决方案在我的 Caja 文件管理器窗口中添加了两个共享图标。

因此我将使用if-up.dhack。

相关内容