如何在启动时挂载sshfs?


12

我将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。我需要的是在启动时但在建立网络连接后挂载它的某种方法。


您如何进行身份验证设置?手动安装密码时是否提示您输入密码?
Zoredache

密钥对验证 不是最安全,但可能足够。
jldugger

Answers:


8

当前,Ubuntu中的Upstart不会生成网络事件。相反,它称为传统sysvinit。默认情况下,NetworkManager已安装并正在运行;它包含一个运行部件分派器(/etc/NetworkManager/dispatcher.d/),而不是向启动者发出网络事件,它本身仅依赖于ifupdown的运行部件分派器(/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; }

2
这对我来说很棒。我会注意到,我将echo输出到stdout的logger -t mountsshfs命令改为命令,因此输出将进入syslog。
马修

3

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

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


3

我相信_netdev作为安装选项应该可以解决此问题


我知道,ubuntu和centos并不相同...但是无论如何在centos中,这是让/etc/init.d/netfs处理sshfs挂载的正确方法。网络启动后将被调用。
匿名一

1

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


0

如果您没有来自远程主机的证书,而必须使用登录名/密码,这是另一种解决方案。在此示例中,我使用了与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
    
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.