AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 9605
Accepted
jldugger
jldugger
Asked: 2009-05-19 11:23:48 +0800 CST2009-05-19 11:23:48 +0800 CST 2009-05-19 11:23:48 +0800 CST

如何在启动时挂载 sshfs?

  • 772

使用 NAS 盒作为 24/7 文件服务器,我想使用 sshfs 从 Ubuntu 9.04 桌面连接到它。目前,我在桌面的 fstab 中有这一行:

sshfs#jldugger@storage:/mnt/HD_a2/    /mnt/storage    fuse   comment=sshfs,auto,users,exec,uid=1000,gid=1000,allow_other,reconnect,transform_symlinks,BatchMode=yes,fsname=sshfs#jldugger@storage/mnt/HD_a2/ 0 0

我可以确认它适用于 mount /mnt/storage。我需要的是一些在启动时安装它的方法,但是在建立网络连接之后。

ubuntu file-sharing boot sshfs init
  • 5 5 个回答
  • 15805 Views

5 个回答

  • Voted
  1. Best Answer
    jldugger
    2009-05-22T10:17:23+08:002009-05-22T10:17:23+08:00

    目前,Ubuntu 中的 Upstart 不生成网络事件。相反,它调用传统的 sysvinit。默认情况下 NetworkManager 已安装并运行;它不是向新贵发出网络事件,而是包含一个 run-parts 调度程序 (/etc/NetworkManager/dispatcher.d/),它本身仅依赖于 ifupdown 的 run-parts 调度程序 (/etc/network/*.d/)。特别是您关心 /etc/network/if-up.d/ 和 /etc/network/if-down.d/

    首先设置一个未加密的 ssh 密钥对,这样就可以在没有提示的情况下挂载点。编写一个脚本,将其放入 /etc/network/if-up.d/ 并使其可执行。以下是在 UbuntuForums 上发现的,对我来说已经足够了:

    #!/bin/sh
    ## http://ubuntuforums.org/showthread.php?t=430312
    ## The script will attempt to mount any fstab entry with an option
    ## "...,comment=$SELECTED_STRING,..."
    ## Use this to select specific sshfs mounts rather than all of them.
    SELECTED_STRING="sshfs"
    
    # Not for loopback
    [ "$IFACE" != "lo" ] || exit 0
    
    ## define a number of useful functions
    
    ## returns true if input contains nothing but the digits 0-9, false otherwise
    ## so realy, more like isa_positive_integer 
    isa_number () {
        ! echo $1 | egrep -q '[^0-9]'
        return $?
    }
    
    ## returns true if the given uid or username is that of the current user
    am_i () {
            [ "$1" = "`id -u`" ] || [ "$1" = "`id -un`" ]
    }
    
    ## takes a username or uid and finds it in /etc/passwd
    ## echoes the name and returns true on success
    ## echoes nothing and returns false on failure 
    user_from_uid () {
        if isa_number "$1"
        then
                    # look for the corresponding name in /etc/passwd
            local IFS=":"
            while read name x uid the_rest
            do
                    if [ "$1" = "$uid" ]
                            then 
                                    echo "$name"
                                    return 0
                            fi
            done </etc/passwd
        else
            # look for the username in /etc/passwd
            if grep -q "^${1}:" /etc/passwd
            then
                    echo "$1"
                    return 0
            fi
        fi
        # if nothing was found, return false
            return 1
    }
    
    ## Parses a string of comma-separated fstab options and finds out the 
    ## username/uid assigned within them. 
    ## echoes the found username/uid and returns true if found
    ## echoes "root" and returns false if none found
    uid_from_fs_opts () {
            local uid=`echo $1 | egrep -o 'uid=[^,]+'`
            if [ -z "$uid" ]; then
                    # no uid was specified, so default is root
                    echo "root"
                    return 1
            else
                    # delete the "uid=" at the beginning
                    uid_length=`expr length $uid - 3`
                    uid=`expr substr $uid 5 $uid_length`
                    echo $uid
                    return 0
            fi
    }
    
    # unmount all shares first
    sh "/etc/network/if-down.d/umountsshfs"
    
    while read fs mp type opts dump pass extra
    do
        # check validity of line
        if [ -z "$pass" -o -n "$extra" -o "`expr substr ${fs}x 1 1`" = "#" ]; 
        then
            # line is invalid or a comment, so skip it
            continue
    
        # check if the line is a selected line
        elif echo $opts | grep -q "comment=$SELECTED_STRING"; then
    
            # get the uid of the mount
            mp_uid=`uid_from_fs_opts $opts`
    
            if am_i "$mp_uid"; then
                            # current user owns the mount, so mount it normally
                            { sh -c "mount $mp" && 
                                    echo "$mp mounted as current user (`id -un`)" || 
                                    echo "$mp failed to mount as current user (`id -un`)"; 
                            } &
                    elif am_i root; then
                            # running as root, so sudo mount as user
                            if isa_number "$mp_uid"; then
                                    # sudo wants a "#" sign icon front of a numeric uid
                                    mp_uid="#$mp_uid"
                            fi 
                            { sudo -u "$mp_uid" sh -c "mount $mp" && 
                                    echo "$mp mounted as $mp_uid" || 
                                    echo "$mp failed to mount as $mp_uid"; 
                            } &
                    else
                            # otherwise, don't try to mount another user's mount point
                            echo "Not attempting to mount $mp as other user $mp_uid"
    :
                            echo "Not attempting to mount $mp as other user $mp_uid"
                    fi
        fi
        # if not an sshfs line, do nothing
    done </etc/fstab
    
    wait
    

    如果您有 wifi 或其他不可靠的连接,请将以下内容放在 /etc/network/if-down.d/ 中:

    #!/bin/bash
    # Not for loopback!
    [ "$IFACE" != "lo" ] || exit 0
    
    # comment this for testing
    exec 1>/dev/null # squelch output for non-interactive
    
    # umount all sshfs mounts
    mounted=`grep 'fuse.sshfs\|sshfs#' /etc/mtab | awk '{ print $2 }'`
    [ -n "$mounted" ] && { for mount in $mounted; do umount -l $mount; done; }
    
    • 8
  2. Dave K
    2009-05-19T12:32:28+08:002009-05-19T12:32:28+08:00

    Upstart是现在在 Ubuntu 中发布启动脚本或服务的首选方法,尽管编辑/etc/rc.local仍然有效。Upstart 允许您控制服务何时运行,确保它在启动网络连接后发生。

    也可以直接编辑 /etc/rc.Xd 中的符号链接(将 X 替换为您正在使用的运行级别)并添加诸如 S99mount 之类的名称以确保它在网络设置后运行。这将需要指向一个脚本文件,该文件挂载您请求的 sshfs。

    • 3
  3. benjaminc
    2011-08-18T22:18:52+08:002011-08-18T22:18:52+08:00

    _netdev 作为挂载选项应该可以解决这个问题,我相信

    • 3
  4. Brian
    2009-05-19T13:53:58+08:002009-05-19T13:53:58+08:00

    只是一个想法,但是如果您将其用作文件服务器,那么 NFS 或 Samba 可能是比 ssh 更好的解决方案。

    • 1
  5. Celso Pires
    2014-11-10T03:22:51+08:002014-11-10T03:22:51+08:00

    如果您没有来自远程主机的证书并且必须使用登录名/密码,这是另一种解决方案。在此示例中,我使用 jldugger 使用的相同用户名和目录以避免增加混淆。

    1. 在主目录中创建一个包含密码的文件,并保护它:

      echo 'YourRemoteUserPassword' > ~jldugger/.credentials
      chmod 600 ~jldugger/.credentials
      
    2. 编辑您的/etc/rc.local文件并在底部插入以下命令,但在“exit 0”之前:

      sshfs -o password_stdin -o nonempty jldugger@storage:/mnt/HD_a2/ /mnt/storage < ~jldugger/.credentials
      
    • 0

相关问题

  • 如何在 Ubuntu 上设置简单的防火墙?

  • 设置没有密码的用户

  • 在 Ubuntu 上设置电子邮件服务器

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve