Friday, July 31, 2009

Remotely mounting network disk with ssh

*** This should work on most distros that are based on debian ***

The name basically says everything. This came as a i meet someone who
needed to somehow remotely mount a network disk for back ups music store
and such.

So i did some research and found out that this is easily done with the
use of sshfs so i thought even thought there are some guides already
for this why not write my own.

So here it goes.

First on the machine that has the network disk what you want to do is
to install ssh that can be done by

sudo apt-get install ssh

Now onto the client side. We need a mount point and
permission to read and write on this mount point. That could be like
this

sudo mkdir /mnt/remote
sudo chown (user) /mnt/remote
chmod og-rwx /mnt/remote


Now we need to install sshfs on the client this is simply done by

sudo apt-get install sshfs

Now we need to load the kernel info and that

sudo modprobe fuse

(note if u want this on start-up add it to /etc/modules

That was all the configuring now to connect. Here is what to do.

sudo sshfs jon@this.is.test:/mnt/sda1 /mnt/remote -o allow_other

Basically this connects to the server at 'this.is.test' with the
user name 'jon' once you enter the pass it takes the contents
of '/mnt/sda1' and puts them in /mnt/remote on your machine the '-o
allow_other' is to allow other users to read the directory.

Now if you entered that correctly it should be mounted and changes you
make in the directory will also be reflected on the server. Now to
unmount it you would enter.

sudo fusermount -u /mnt/remote

Now this is all very useful but would be a pain to have to enter in
all these commands when one wants to use the network disk so i have
wrote a small bash script.

(Disclaimer use at own risk make sure you have made
the files and that they have the right permissions on them.)

Ok for the script just put the following in a file named mountssh.sh
also be sure to change the values to suit yourself

#!/bin/sh
# Simple script for connecting to a file system on a remote host using
# Ssh
sudo sshfs jon@this.is.test:/mnt/sda1 /mnt/remote -o allow_other
echo "CONNECTING"

Then do this to the file

chmod +x mountssh.sh

Then to run the script you can either write

./mountssh.sh

Or you can just execute it normally from X

It would probably be a good idea to also make it so the disk auto mounts
whenever it is connected to the server.

Well thanks for reading that hope it helps someone out there.

No comments:

Post a Comment