Tag Archives: umount

How to make SSHFS mount with SSH key and password

Let’s talk today about SSHFS mount, sometimes you just can’t do NFS or CIFS mount just because ^^

So one solution could be to use SSHFS. I won’t argue about speed, security or benchmarking the thing, I just had no other choice than doing SSHFS so here’s a way to do it with an SSH key and with only a password. (yea that’s a terrible idea but again, sometimes you have no choice)

Obviously if you have things to say about that method, drop a comment I’ll be glad.

I decided to show up only the fstab mount, if you need to do it on the fly then just adapt it šŸ™‚

Prelude

Both example will be self explanatory for the most, you just have to change theĀ words that I put uppercase.

uid/gid fields are set for the local server’s user (check /etc/passwd), set it to the proper user.

idmap=user is THE trick to keep the correct uid/gid mapping on both servers !

Before to get started, we will be mountingĀ /home/REMOTE_USER/data/ from the remote server inĀ /mnt/data/ on the local server therefore make sure to mkdir the local directory /mnt/data as your mount point.

SSHFS mount in /etc/fstab with a SSH key

Here it’s the IdentifyFile parameter that’s the most important, it must be the SSH private key, don’t forget to put the SSH pub key on the other server.

sshfs#REMOTE_USER@REMOTE_HOST:/home/REMOTE_USER/data/ /mnt/data/ fuse            IdentityFile=/home/LOCAL_USER/.ssh/THEKEY,uid=LOCAL_UID,gid=LOCAL_GID,users,idmap=user,noatime,allow_other,_netdev,auto_cache,reconnect     0 0
sshfs fstab mount with SSH key

Just do:

# mount -a
Time to mount !

SSHFS mount in /etc/fstab with a password using sshpass

First install sshpass, on debian (for other OS do a research, it shouldn’t be hard):

apt-get update && apt-get install sshpass
Install sshpass on debian

This time the important parameter isĀ ssh_command=/home/LOCAL_USER/passwd.sh it’s just a simple script that will do the trick.

sshfs#REMOTE_USER@REMOTE_HOST:/home/REMOTE_USER/data/ /mnt/data/ fuse            ssh_command=/home/LOCAL_USER/passwd.sh,uid=LOCAL_UID,gid=LOCAL_GID,users,idmap=user,noatime,allow_other,_netdev,auto_cache,reconnect     0 0
sshfs fstab mount with only a password

Make sure now to edit the file for the ssh_command, hereĀ /home/LOCAL_USER/passwd.sh, simply changeĀ REMOTE_PASSWORD to the right password.

#!/bin/bash

sshpass -p REMOTE_PASSWORD ssh $*
Content of /home/LOCAL_USER/passwd.sh

Make it safer !

# chown LOCAL_USER:LOCAL_USER /home/LOCAL_USER/passwd.sh && chmod 700 /home/LOCAL_USER/passwd.sh
Make it safer !

And now it’s time to mount:

# mount -a
Time to mount !

Debugging

Probably it won’t all go well, wether you set the wrong password, the wrong key or whatever, if so just add the following options debug,sshfs_debug into your fstab:

sshfs#REMOTE_USER@REMOTE_HOST:/home/REMOTE_USER/data/ /mnt/data/ fuse            debug,sshfs_debug,ssh_command=/home/user/passwd.sh,uid=LOCAL_UID,gid=LOCAL_GID,users,idmap=user,noatime,allow_other,_netdev,auto_cache,reconnect     0 0
sshfs example with debug options

Conclusion

Well, not much to add, it’s pretty much useful to use SSHFS but it can be tricky, obviously, do prefer the SSH key method.

How to mount via autofs

Hey,

here’s how to use autofs, it’s quite a good soft to be sure you mount filesystems anytime, it’s also useful to unmount them when you don’t need them (less uptime for the disk and you can save the planet this way).
First install the correct package:

# aptitude install autofs
Install autofs

Then create the few directories:

# mkdir /etc/auto.map.d /etc/auto.master.d /mnt/autofs
Create directories

Now it’s time to do some configuration, so edit /etc/auto.master.d/master.autofs:

/mnt/autofs /etc/auto.map.d/master.autofs
Content of /etc/auto.master.d/master.autofs

Finally, simply set the mount(s) you want in /etc/auto.map.d/master.autofs (one per line) :

# <directory name (will be in /mnt/autofs)> <options such as filesystem, uid/gid ...> <what you mount>
boxshare -fstype=cifs,defaults,_netdev,uid=1000,gid=1000,user=nobody,password= ://192.168.1.1/myshare
Content of /etc/auto.map.d/master.autofs

There you can notice I just have one mount, a cifs one, the mount directory will be /mnt/autofs/boxshare.

Now restart autofs and check if it’s mounted!

# service autofs restart
# ls /mnt/autofs/boxshare
Start it up !

If when you “ls” the directory it fails, then stop autofs and troubleshoot it this way:

# service autofs stop
# automount -f -v
Troubleshooting

You’ll probably get this error:

Starting automounter version 5.0.7, master map /etc/auto.master
using kernel protocol version 5.02
lookup(file): failed to read included master map auto.master
mounted indirect on /mnt/autofs with timeout 300, freq 75 seconds
Error

To fix it, simply open /etc/auto.master an comment the last line, so it should look like that:

#
# Sample auto.master file
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
#
#/misc /etc/auto.misc
#
# NOTE: mounts done from a hosts map will be mounted with the
# "nosuid" and "nodev" options unless the "suid" and "dev"
# options are explicitly given.
#
#/net -hosts
#
# Include /etc/auto.master.d/*.autofs
#
+dir:/etc/auto.master.d
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
#+auto.master
Fixing it

Now you restart autofs and it should work !

If not, do the troubleshooting again šŸ™‚
Cheers !