Automatically mount a remote folder on boot

1

1

I'm trying to mount a Windows folder on my Ubuntu machine on start up. I've tried following this page here, modifying /etc/fstab and appending sshfs#my_user@remote_host:/path/to/directory <local_mount_point> fuse user 0 0 to it, but it fails; on start up, I get an error saying that the mounting failed, and I can press S to skip or M to recover manually.

I also tried following this page here, appending /usr/bin/sshfs -o idmap=user my_user@remote_host:/path/to/directory <local_mount_point> to the /etc/rc.local file, but this doesn't help either; Ubuntu just boots up normally without mounting.

I have Cygwin installed on my Windows machine, and I can run everything smoothly, such as sshing without passwords, and mounting it manually. I've also tried to run the modified rc.local file $ /etc/rc.local, and it works perfectly, but I just can't seem to get the folder mounted on start up.

Can someone help me?

EDIT: by remote, I meant that the folder I'm trying to mount from my Windows machine (which has a public ip), is not on the same network. I am using sshfs, not nfs.

Andrew

Posted 2013-10-29T07:42:32.693

Reputation: 365

Could you please check whether the solution that works on my Arch system, which I just added to my initial answer as an edit, works for you too? – MariusMatutiae – 2013-10-30T19:03:39.410

Answers

0

On my Ubuntu machine, I modified my /etc/fstab by adding this line:

 //192.168.xx.yy/Public /mnt/PUBNAS nfs rw,_netdev,credentials=/home/myname/.smbcredentials 0 0

The file .smbcredentials is very simple,

 username=myusername                                                                            
 password=mypassword

Edit:

After realizing that you use sshfs, I have tested the /etc/fstab entry suggested by the Arch Linux wiki page. For me, this entry worked:

USERNAME@HOSTNAME_OR_IP:/REMOTE/DIRECTORY  /LOCAL/MOUNTPOINT  fuse.sshfs  defaults,_netdev  0  0

It is important that you have passwordless access to the server (i.e., with cryptographic key). I hope this makes up for my initial blunder.

Edit 2:

If you want the remote folder to be mounted by you, not by root, which is admittedly a nuisance, you will have to change the /etc/fstab line to this:

 USERNAME@HOSTNAME_OR_IP:/REMOTE/DIRECTORY  /LOCAL/MOUNTPOINT  fuse.sshfs _netdev,user,idmap=user,transform_symlinks,identityfile=/home/USERNAME/.ssh/id_rsa,allow_other,default_permissions,uid=USER_ID_N,gid=USER_GID_N 0 0

MariusMatutiae

Posted 2013-10-29T07:42:32.693

Reputation: 41 321

He's using sshfs, not nfs. Relevant is just _netdev. – week – 2013-10-29T09:44:39.620

Absolutely:) Do you know if that option "_netdev" is used by some service later, to actually mount that path after network is established? – week – 2013-10-29T09:52:50.550

Is this still relevant to me, as I am using sshfs, not nfs? Also, my Windows machine is remote; it is not on the same network. – Andrew – 2013-10-30T02:01:21.023

I modifed my /etc/fstab to include the line like this now: sshfs#my_user@remote_host:/path/to/directory <local_mount_point> fuse user,_netdev 0 0, but it doesn't work. I want to highlight though, that if I use the graphical method, and click the mount icon on my left panel, it works. I can now access it through terminal. However before using the graphical method, I couldn't access through terminal. – Andrew – 2013-10-30T06:03:14.587

Also, is there a way to include the sshfs option -o idmap=user in the /etc/fstab file? When I move files from local to the mounted folder, I get the mv: failed to preserve ownership message. – Andrew – 2013-10-30T06:06:00.113

I have passwordless access to server. I tried what you suggested, and the result is the same as the second comment above this one -- when I use terminal to try and access the mount point, its empty. But after I click the mount icon on the navigation panel on the left in the Ubuntu GUI, it mounts successfully. After which, when I try to access the mount point, all the files are there. Also, I'm mounting my server to my home folder in Ubuntu: ~/windows, and not in /mnt. Does this matter? – Andrew – 2013-10-31T01:47:15.297

Strictly speaking, it works; however, I'm prompted for my password to the remote server (even though I can ssh in without any password). The remote folder is successfully mounted, but seems like I need root access to do anything. Trying to cd into the mounted folder gives me Permission denied. – Andrew – 2013-10-31T04:23:40.883

@MariusMatutiae Are USER_ID_N and USER_GID_N variables I need to change? Also, does the identityfile have to be rsa? My key is a dsa private key. Should I generate an rsa one? – Andrew – 2013-10-31T06:43:25.870

It doesn't work now. When I click the mount icon on the navigation panel on the left, nothing happens too. – Andrew – 2013-10-31T07:15:09.127

testsvr@203.125.xxx.yyy:/cygdrive/c/images /home/andrew/windows fuse.sshfs noauto,x-systemd.automount,_netdev,user,idmap=user,transform_symlinks,identityfile=/home/andrew/.ssh/key,allow_other,default_permissions,uid=1000,gid=1000 0 0 – Andrew – 2013-10-31T07:44:03.987

After typing in sudo mount -a it works!! Is there a way to do sudo mount -a on start up automatically, so I don't have to log in and run it myself? – Andrew – 2013-10-31T08:06:33.453

Awesome, it works like a charm now! Just 2 quick questions - if say I were to do this with a Linux machine rather than a Windows machine, after changing the username/ip address/path to folder, would it still work? Secondly, how can I unmount the folder? I tried fusermount -u ~/windows and I get fusermount: entry for /home/andrew/windows not found in /etc/mtab. – Andrew – 2013-10-31T08:21:14.977

@MariusMatutiae I've accepted this as answer, and I'd upvote if I could, but I have insufficient rep ;( Anyway, thanks so much for all your help! – Andrew – 2013-11-01T01:05:05.983

0

I actually looked closer at your scenario here, and I am wondering if you made sure to add the execute bit to that /etc/rc.local file you created originally (at the end of the howtoforge.com article you followed)

chmod +x /etc/rc.local

Now reboot.

jredd

Posted 2013-10-29T07:42:32.693

Reputation: 776

It seems like the file is already executable $ ls -l /etc/rc.local -rwxr-xr-x 1 root root 497 Oct 29 18:07 /etc/rc.local andrew@Andrew-Ubuntu:~$ sudo chmod +x /etc/rc.local andrew@Andrew-Ubuntu:~$ ls -l /etc/rc.local -rwxr-xr-x 1 root root 497 Oct 29 18:07 /etc/rc.local nothing changed when I used your command, but I'll try and reboot now anyway and see if it helps. – Andrew – 2013-10-30T05:32:19.480

It doesn't help. – Andrew – 2013-10-30T05:45:41.393

0

Can you verify rc.local is being executed at boot? Put something in it:

echo 'test' >> /tmp/my.log

Next, see if there's any error output, append to your mount command:

&> /tmp/my.log

Perhaps when you manually execute the file, you're running it as a different user than the kernel is running it as. In that case a message would be written to STDERR, which you would typically see in /var/log/messages unless you redirect it elsewhere with the above syntax appended to the command.

JoshRibs

Posted 2013-10-29T07:42:32.693

Reputation: 1 151

I've done what you have suggested, and after rebooting, the file /tmp/my.log is created, but it's empty. test isn't inside the file, and ls -l /tmp/my.log gives me -rw-r--r-- 1 root root 0 Oct 31 09:31 /tmp/my.log. I also have no file named /var/log/messages. – Andrew – 2013-10-31T01:34:25.607

The user its running as may not have access to /tmp/, can you chmod 777 a folder, touch a file in it, chmod that 777 and try logging to a file there? – JoshRibs – 2013-11-01T00:53:49.297

Yes, rc.local is executed at boot. Anyway, I've solved the problem. Thank you! – Andrew – 2013-11-01T01:05:37.000