Automatically mount a folder over ssh
For my xen server I was looking for a nice and automatic way to mount a folder from one virtual machine in the other. Xen itself does not have an environment for this, luckily we can deploy ssh for it.
So how do we do this? When doing a search (on Google) it came with with methods to do this via the fstab (/etc/fstab). My problem was however, that I just could not get that to work. I have no idea why, but it didn't in the end. Therefore I searched for and found a different method to do so!
Note: I did this on my Debian Lenny server and I am not sure if and how it will work on other GNU/linux distributions.
Method has a few steps to take in order to make it work. In short these steps are:
- Install sshfs
- Enable ssh to login without providing a password
- Write a small init script to automatically mount and umount the ssh folder
1. Install sshfs
In Debian we use the standard command for this:
apt-get install sshfs
2. Enable passwordless ssh login
In order to use the ssh link automatically we should not be forced to type our password very often. Therefore we will use a passwordless login. Istead of a password we will use a public key to login (securely). The following steps should be done in order to achieve this (source).
1) If you don't already have a keypair generated you'llneed to create one. Otherwise you can skip this step (however keep in mind that the path to your keyfile may differ in the following steps).
If you accept the defaults you'll have a pair of files created, as shown above, with no passphrase. This means that the key files can be used as they are, without being "unlocked" with a password first. If you're wishing to automate things this is what you want.
Now generate the keyfile:
ssh-keygen -t rsa
2) Now that you have a pair of keyfiles generated, or pre-existing, you need to append the contents of the .pub file to the correct location on the remote server.
Assuming that you wish to login to the machine called mystery from your current host with the id_rsa and id_rsa.pub files you've just generated you should run the following command:
ssh-copy-id -i ~/.ssh/id_rsa.pub username@mystery
This will prompt you for the login password for the host, then copy the keyfile for you, creating the correct directory and fixing the permissions as necessary.
3. Init.d script
This is the more interesting section as it describes teh mount method. For the mounting procedure I will use the an init.d script (source). Please note that I am aware of the fact that my script does not comply with the Debian standards for these script (see: /etc/init.d/skeleton). Maybe I will fix this in the future. For now however, this script works and is in use by me!
First we create a file named sshfs (or whatever name you choose). Then we fill this file with some code. Here I present you the code I used:
#! /bin/sh
# /etc/init.d/sshfs
# Written by: Patrick Hanckmann
# License: BSD/GPL
# Some things that run always
#touch /var/lock/blah
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting script sshfs (mount)"
sshfs 192.168.2.201:/home/data/Torrentflux/ /home/patrick/torrentflux/ -o allow_other
;;
stop)
echo "Stopping script sshfs (umount)"
umount /home/patrick/torrentflux/
;;
restart)
echo "Restarting script sshfs (umount and mount)"
umount /home/patrick/torrentflux/
sshfs 192.168.2.201:/home/data/Torrentflux/ /home/patrick/torrentflux/ -o allow_other
;;
*)
echo "Usage: /etc/init.d/sshfs {start|stop|restart}"
exit 1
;;
esac
exit 0
Now save this file. Next we copy the file to the correct location and make sure that the rights are correct:
cp ./sshfs /etc/init.d/sshfs chmod 755 /etc/init.d/sshfs
Finally we have to make Debian aware of the new script and make sure it is started automatically at each time Debian boots. The simplest way of doing this is to use the Debian-specific command update-rc.d:
update-rc.d sshfs defaults
And that is it. The script should work and the ssh folder should be mounted automatically after each reboot.
|
If you wish to remove the script from the startup sequence in the future run: update-rc.d -f sshfs remove |
- patrick's blog
- Add new comment
- 9341 reads

Recent comments
1 year 33 weeks ago
1 year 34 weeks ago
1 year 35 weeks ago
1 year 48 weeks ago
2 years 1 week ago
2 years 14 weeks ago