NAS 상자를 연중 무휴 파일 서버로 사용하여 sshfs를 사용하여 Ubuntu 9.04 데스크톱에서 연결하고 싶습니다. 현재 데스크톱의 fstab에 다음 줄이 있습니다.
sshfs#[email protected]:/mnt/HD_a2/ /mnt/storage Fuse comment=sshfs,auto,users,exec,uid=1000,gid=1000,allow_other,reconnect,transform_symlinks,BatchMode=yes,fsname=sshfs#[email protected]/mnt/HD_a2/ 0 0
마운트 /mnt/storage
에서 작동하는지 확인할 수 있습니다. 필요한 것은 시작할 때 마운트하는 방법이지만 네트워크 연결이 설정된 후입니다.
현재 Ubuntu의 Upstart는 네트워크 이벤트를 생성하지 않습니다. 대신 전통적인 sysvinit이라고 부릅니다. 기본적으로 NetworkManager가 설치되어 실행 중입니다. 네트워크 이벤트를 upstart로 내보내는 대신 ifupdown의 run-parts dispatcher (/ etc/network/*. d /)에만 의존하는 run-parts dispatcher (/etc/NetworkManager/dispatcher.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
Wi-Fi 또는 기타 불안정한 연결이있는 경우 /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; }
마운트 옵션으로 _netdev가이 문제를 해결해야한다고 생각합니다.
pstart 는 현재 Ubuntu에서 시작 스크립트 또는 서비스를 발행하는 데 선호되는 방법이지만 /etc/rc.local
여전히 작동합니다. Upstart를 사용하면 서비스가 실행되는시기를 제어하여 네트워크 연결을 시작한 후에 발생하는지 확인할 수 있습니다.
/etc/rc.X.d의 심볼릭 링크를 직접 편집하고 (사용중인 실행 수준을 X로 대체) S99mount와 같은 이름을 추가하여 네트워크 설정 후에 실행되도록 할 수도 있습니다. 요청하는 sshfs를 마운트하는 스크립트 파일을 가리켜 야합니다.
생각 만했지만 이것을 파일 서버로 사용하는 경우 NFS 또는 Samba가 ssh보다 나은 솔루션이 될 수 있습니다.
다음은 원격 호스트의 인증서가없고 대신 로그인/암호를 사용해야하는 경우에 대한 또 다른 솔루션입니다. 이 예제에서는 혼동을 피하기 위해 jldugger에서 사용하는 것과 동일한 사용자 이름과 디렉토리를 사용하고 있습니다.
홈 디렉토리에 비밀번호가 포함 된 파일을 만들고 보호합니다.
echo 'YourRemoteUserPassword' > ~jldugger/.credentials
chmod 600 ~jldugger/.credentials
/etc/rc.local
파일을 편집하고 맨 아래에 "exit 0"앞에 다음 명령을 삽입하십시오.
sshfs -o password_stdin -o nonempty [email protected]:/mnt/HD_a2/ /mnt/storage < ~jldugger/.credentials