Boy do I love sshfs… Mounting ssh / sftp share as a local drives.

Hi folks.

I have a machine that runs ssh server. That’s nothing new. Neither is it worth mentioning under normal circumstances… Recently I have purchased a 2 TB Western Digital MyBook USB 3.0 hard drive and I was going to use it to backup all my data. Why not make it a network shared drive I thought. It will make my life much easier if I could access the data from all my machines? Not a bad idea… I know… but I am not going to setup samba or nfs. I don’t want to make it a “network” drive. I want to have it mounted as a local drive on every machine that I use without a big fuss… How do I go about it?

I assume that You have drive attached to the ssh server running machine and that it’s mounted and that You have read and write permissions granted to Your user. I am using static IPs in my network – this is making things much easier for me as well.

In my case the drive is mounted on the server (IP 192.168.0.1 and port 20202) as /media/1862_GB_X-Ternal/ and my user andrzejl is the only user that is allowed to write to and read from it.

Now it’s time to prep the client machine. It’s really simple…

I want to have my drive mounted on my ssh client machines in /media/1862_GB_X-Ternal/ folder but I want to mount it as user (andrzejl) – not as root.

First I had to open terminal and gain root’s privileges by issuing:

su

and giving a root’s password.

Next I had to create my mount point:

mkdir -p /media/1862_GB_X-Ternal/

and make andrzejl owner of it:

chown -Rf andrzejl:andrzejl /media/1862_GB_X-Ternal/

Now that I had the folder ready I needed a package that would allow me to work with sshfs / sftp file system so in the same terminal I ran:

apt-get install sshfs-fuse

After the package was downloaded and installed I could close this terminal window and open another one. I needed to drop the root’s privileges as I want to do the rest of this as a user.

The syntax of the command looks like this:

sshfs -p sshSERVERport loginTOtheSSHserver@IPorHOSTNAMEofTHEsshServer:/where/is/the/drive/mounted/on/the/server/ /where/to/mount/on/local/machine/

Now… if I will start filling the data in this command I get this:

sshfs -p 20202 andrzejl@192.168.0.1:/media/1862_GB_X-Ternal/ /media/1862_GB_X-Ternal/

After running this command and typing in the password (if You got the syntax right) You should find all Your data on Your ssh client machine mounted in /media/1862_GB_X-Ternal/ ready to be read and modify by Your user.

Now…

IF You want the data to be automounted at start up without typing in the password follow this post. Passwordless SSH authentication. Using authentication keys

You also need to create a mountsshfsshare.sh script in your ~/.config/autostart folder and make it executable.

Here is how I do it under KDE4.

Open terminal. Type in:

touch ~/.config/autostart/mountsshfsshare.sh

chmod +x ~/.config/autostart/mountsshfsshare.sh

echo "sshfs -p 20202 andrzejl@192.168.0.1:/media/1862_GB_X-Ternal/ /media/1862_GB_X-Ternal/" > ~/.config/autostart/mountsshfsshare.sh

Don’t forget to modify the sshfs line to suite Your needs.

Just to check run this:

cat ~/.config/autostart/mountsshfsshare.sh

It should spit out:

sshfs -p 20202 andrzejl@192.168.0.1:/media/1862_GB_X-Ternal/ /media/1862_GB_X-Ternal/

or whatever command You use to mount the sshfs share. Now You can reboot the ssh client machine for testing purposes. If You did everything properly – You will have a mounted drive waiting for You next time You boot up Your machine.

Edit 01: Sometimes .sh script will not work. Try creating .desktop file then instead.

Remove the .sh file first.

rm -f ~/.config/autostart/mountsshfsshare.sh

Now create the .desktop file.

touch ~/.config/autostart/mountsshfsshare.desktop

Now edit the file using Your favorite editor. I will use mcedit here. Paste this into it:

[Desktop Entry]
Comment[en_US]=Mount SSHFS automagically.
Comment=Mount SSHFS automagically.
Encoding=UTF-8
Exec=sshfs -p 20202 andrzejl@192.168.0.1:/media/1862_GB_X-Ternal/ /media/1862_GB_X-Ternal/ &
GenericName[en_US]=
GenericName=
Icon=xterm-terminal
MimeType=
Name[en_US]=sshfs_mount
Name=sshfs_mount
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=

Do not forget to change the sshfs line. Now save the file and reboot for testing.

Edit 02: IF a startup script nor a desktop file works for You add (as root using Your favorite text editor) lines like this at the end of Your /etc/rc.local file:

echo "Mounting SSHFS share as andrzejl"
su andrzejl -c "sshfs -p 20202 andrzejl@192.168.0.1:/media/1862_GB_X-Ternal/ /media/1862_GB_X-Ternal/ &"

Don’t forget to leave one empty line at the end of the file. Also You will need to modify the lines to Your needs of course.

I like this setup very much for a good few reasons. Here are just a few:

a) hard drive is being shared over the network but it feels and acts like a local drive
b) it’s not accessible by the windows machines without specific setup
c) it’s easy to setup permissions to the drive so only one user or group can have full access to the drive. You can have some folks to see the drive as read only while You keep the privileges to write to it.
d) like everything that runs via ssh the traffic between you and the hdd is encrypted

Regards.

Andy

Advertisements