Previous Page
PCLinuxOS Magazine
Article List
Next Page

Mounting SSH/SFTP Shares As Local Drives

by AndrzejL

I have a machine that runs an ssh server. That's nothing new. Neither is it worth mentioning under normal circumstances. Recently, I 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 was not going to setup samba or nfs. I didn't want to make it a "network" drive. I wanted 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 the 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 makes things much easier for me.

In my case the drive is mounted on the server (IP 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 a terminal and gain root's privileges by issuing:


and giving 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 closed this terminal window and opened 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/

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

sshfs -p 20202 andrzejl@ /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 modified by your user.

Now ...

If you want the data to be automounted at start up without typing in the password, follow this post in my blog: Passwordless SSH authentication. Using authentication keys

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

Here is how I do it under KDE4.

Open a terminal. Type in:

touch ~/.config/autostart/
chmod +x ~/.config/autostart/mountsshfsshare.shecho "sshfs -p 20202andrzejl@ /media/1862_GB_X-Ternal/ /media/1862_GB_X-Ternal/" > ~/.config/autostart/

Don't forget to modify the sshfs line to suit your needs.

Just to check, run this:

cat ~/.config/autostart/

It should spit out:

sshfs -p 20202 andrzejl@ /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 the next time you boot up your machine.

Edit: Sometimes .sh script will not work. If it doesn't, try creating a .desktop file instead.

Remove the .sh file first.

rm -f ~/.config/autostart/

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.
Exec=sshfs -p 20202 
/media/1862_GB_X-Ternal/ /media/1862_GB_X-Ternal/ &

Do not forget to change the sshfs line. Now save the file and reboot for testing. I like this setup very much for a good few reasons. Here are just a few:

a) The 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 a 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 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.

Previous Page              Top              Next Page
Copyright (c) 2013, The PCLinuxOS Magazine. All Rights Reserved.