Automatyczne montowanie zasobów SSH/SFTP w Ubuntu 10.04
Z pomocą przyszedł Narcis Garcia. Rozwiązanie nie jest w 100% tym, co chciałoby się osiągnąć, bo niestety bazuje na hasłach, a nie kluczach prywatnych. Ale lepszy rydz, niż nic, serwery montują się, wszystko działa jak powinno. A więc co musimy zrobić?
Najpierw należy upewnić się, że pakiet sshfs jest zainstalowany (z tego co pamiętam jest on dołączany do Ubuntu w standardowej konfiguracji, ale sprawdzić warto). Następnie musimy włączyć możliwość montowania zasobów przez zwykłych użytkowników, w tym celu musimy wyedytować plik /etc/fuse.conf i odkomentować (jeśli jest zakomentowana znakiem #) linijkę: user_allow_other.
Kolejnym krokiem jest utworzenie pliku /usr/local/bin/sshfs-mount.sh o zawartości:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #!/bin/sh # Connect an SFTP mount point # Version 2010.12.28 # Copyright (GNU GPL) Narcis Garcia LocalPath="$1" RemotePassword="$2" RestrictToUser="$3" Connect () { Result=0 AlreadyMounted="$(mount | grep " $LocalPath ")" if [ "$AlreadyMounted" = "" ] ; then if [ "$(which sshfs)" = "" ] ; then apt-get install -qq -y sshfs fi if [ "$(which mount.fuse.sshfs)" = "" ] ; then ln -s $(which mount.fuse) /sbin/mount.fuse.sshfs fi if [ ! -d "$LocalPath" ] ; then # Create the mount point with permissions mkdir -p "$LocalPath" if [ "$RestrictToUser" = "" ] ; then chmod ugo=rwX "$LocalPath" else LocalGroup="$(id -ng $RestrictToUser)" chown $RestrictToUser:$LocalGroup "$LocalPath" chmod u=rwX,g=rX,o= "$LocalPath" fi fi echo "$RemotePassword" | mount "$LocalPath" -o password_stdin,allow_other,StrictHostKeyChecking=no Result=$? if [ $Result -eq 0 ] ; then echo " Right" else echo "($Result) The command was:" echo "echo "\$RemotePassword" | mount "$LocalPath" -o password_stdin,allow_other,StrictHostKeyChecking=no" fi else echo "The directory was already mounted on $(ReturnWord () { echo $3; }; ReturnWord $AlreadyMounted)" Result=1 fi return $Result } TempLog="/tmp/sshfs_$(echo "$LocalPath" | tr -s "/" "-" | tr -s " " "_").log" FirstMessage="Connecting $LocalPath to the server$(if [ -f $TempLog ] ; then echo " (was interrupted)" ; fi)... " printf "$FirstMessage" Line1Log="$(date +'%Y-%m-%d %H:%M:%S') $FirstMessage" Connect > $TempLog 2>&1 cat $TempLog if [ $(id -g) -eq 0 ] ; then echo "$Line1Log$(cat $TempLog)" >>/var/log/sshfs.log fi rm $TempLog |
oraz nadanie mu odpowiednich praw: chmod a+x /usr/local/bin/sshfs-mount.sh
Później musimy utworzyć kolejny plik: /etc/network/if-down.d/sshfs-umounts (bez rozszerzenia .sh) o zawartości:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #!/bin/sh # Disconnect all the SFTP mount points # Version 2010.10.10 # Copyright (GNU GPL) Narcis Garcia # Note: Only works with local and remote paths without spaces Disconnect () { local LocalPath="$1" umount "$LocalPath" Result=$? if [ $Result -eq 0 ] ; then echo " Right" else echo "($Result)" fi return $Result } CurrentText="$(date +'%Y-%m-%d %H:%M:%S') The SFTP accesses will be disconnected:" echo "$CurrentText" if [ $(id -g) -eq 0 ] ; then echo "$CurrentText" >>/var/log/sshfs.log fi MountedList="$(mount | grep -e " fuse\.sshfs ")" if [ "$MountedList" != "" ] ; then IFS=$(printf "\n\b") ; for CurrentLine in $MountedList ; do unset IFS LocalPath="$(ReturnWord () { echo $3; }; ReturnWord $CurrentLine)" TempLog="/tmp/sshfs_$(echo "$LocalPath" | tr -s "/" "-" | tr -s " " "_").log" FirstMessage="Disconnecting $LocalPath from server $(if [ -f $TempLog ] ; then echo "(was interrupted)" ; fi)... " printf "$FirstMessage" Line1Log="$(date +'%Y-%m-%d %H:%M:%S') $FirstMessage" Disconnect "$LocalPath" > $TempLog 2>&1 cat $TempLog if [ $(id -g) -eq 0 ] ; then echo "$Line1Log$(cat $TempLog)" >>/var/log/sshfs.log fi rm $TempLog done else CurrentText="There was no mounted SFTP point." echo "$CurrentText" if [ $(id -g) -eq 0 ] ; then echo "$CurrentText" >>/var/log/sshfs.log fi fi |
i również nadajemy mu odpowiednie prawa: chmod a+x /etc/network/if-down.d/sshfs-umounts
Kategoria:Użyteczne
Przekaż dalej: