How do I mount sshfs at boot?

Asked by Justin Dugger on 2009-05-18

Using a NAS box as 24/7 file server, I'd like to use sshfs to connect to it from an Ubuntu 9.04 desktop. Currently, I have this line in the desktop's 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

I can confirm it works with mount `/mnt/storage`. What I need is some method of mounting it at startup, but after the network connection is established. Suggestions?

Question information

English Edit question
Ubuntu Edit question
No assignee Edit question
Last query:
Last reply:
Steven Danna (ssd7) said : #1

There are a number of ways to accomplish this. However, the easiest way may be to do the following:

On your desktop, add the following line to the entry in /etc/network/interfaces related to the network interface that needs to be up before you can connect to your NAS box:

post-up mount /mnt/storage

The only downside to this is that if the mount fails, the interface will not be marked as up.

Justin Dugger (jldugger) said : #2

Does this work in the presence of NetworkManager?

Steven Danna (ssd7) said : #3

No, that method will not work with NetworkManager since any device connected with NetworkManager doesn't use /etc/network/interfaces. If the desktop isn't a laptop and doesn't use wireless, it may be easiest to just configure it using /etc/network/interfaces rather than network manager. Otherwise you might need to write a script that checks to see if the connection is up and mounts the drive if it is. Other than those two options though, I don't have other ideas without doing research.

You may want to respond to this so this email just so the question goes back to an open status.

Justin Dugger (jldugger) said : #4

Indeed, this is a desktop with wireless. I just find wireless easier to deal with in home deployment, and sufficiently fast for normal use.

Steven Danna (ssd7) said : #5

Ok. I wrote a small script that should do what you need to do; however, there might be a way to do this without a script that I don't know of. The script will keep pinging the server (sleeping 5 seconds between pings) until the server can be reached, once it can be reached, it attempts to mount the drive. The MAX_ATTEMPTS variable is the maximum number of time it will ping the server before giving up. I'm not really a programmer so I'm sure this could be improved. Here is the script:


SERVER="" #Put the ip address or host name of backup server here
MAX_ATTEMPTS=30 #Max number of times to see if the server is reachable

ping -q -c 1 $SERVER > /dev/null &> /dev/null

if [ $? = 0 ]; then

while [ $if_up = "no" ];
    sleep 5
    ATTEMPTS=$(( $ATTEMPTS + 1 ))
    ping -c 1 $SERVER > /dev/null &> /dev/null
    if [ "$?" = "0" ]; then
    if [ "$ATTEMPTS" = "$MAX_ATTEMPTS" ]; then
 exit 1

if [ $if_up = "yes" ]; then
    mount /mnt/storage

To use this, do the following:

1) Open a terminal (Applications->Accessories->Terminal). Use this terminal to enter the following commands.
2) cd ~
3) mkdir bin
4) cd bin
5) gedit
6) Copy and paste above script into gedit and save the file.
7) Back in the terminal: chmod +x
8) Go to: System->Preferences->Startup Applications
9) Click the Add Button
10) Fill the Name field with something like: Mount Network Drive
11) Hit the Browse button next to the "command" field and then find and select the file you made.
12) Hit the Add button

If you use this method, the script will run after you login to your GNOME session. That is usually the same time that NetworkManager is finding and connecting to your wireless network so it should work the way you desire. Good luck!

Can you help with this problem?

Provide an answer of your own, or ask Justin Dugger for more information if necessary.

To post a message you must log in.