web analytics

How to mount remote directory on Ubuntu 11.10 using SSHFS

Written by config on . Posted in Linux, Ubuntu

Advertisement

In this tutorial I will teach you how to mount a complete directory to a local server from a remote server using SSHFS i.e. securely. SSHFS stands for Secure Shell FileSystem. This kind of filesystem provides the directories and files securely over SSH and all the local users can access those files as if they are local files or directories. The remote share is mounted using FUSE which means FileSystem in Userspace.

Advertisement

Preliminaries:

Local System: server1.example.com (192.168.0.100)

Remote System: server2.example.com (192.168.0.101)

You must be logged in as root user.

Installing SSHFS

On server1 use this command to install sshfs

apt-get install sshfs

Now if you want to mount the remote directory /home/backup to the local /backup directory then first of all you will have to add the root user to fuse group by using the command given below:

adduser root fuse

Mounting

Now create the local /backup directory and change the owner of the directory to root.

mkdir /backup
chown root /backup

After that mount the remote directory /home/backup to /backup as:

Full path method:

sshfs –o idmap=user root@192.168.0.101:/home/backup /backup

Relative path method:

sshfs –o idmap=user root@192.168.0.101:backup /backup

This corresponds to /root/backup.

You can also omit the remote directory by

sshfs –o idmap=user root@192.168.0.101: /backup

And in this case it would be /root only.

Notice the –o idmap=user in the command. This means that it would not affect if the local system and the remote system are using different user ID. If you omit this chunk of code then there may be some permission related problems.

If everything goes well then the directory will get mounted. You can check it using the command

mount

Unmount

If you want to unmount the directory then use:

fusermount –u /backup

Creating key pair on server1

Every time we share a remote directory it will ask for the password for verification. We usually don’t want it to ask password every time so we will generate a key and will send it to server2 so that it will not ask for the password every time.

For this, just follow the steps given below:

ssh-keygen

Some options will come. Just press enter 3 times to skip the steps and let it be default settings.

Now we have to copy the key to the remote server which is server2.

ssh-copy-id –i $HOME/.ssh/id_rsa.pub root@192.168.0.101

Check on server2 if it has been copied or not by:

cat $HOME/.ssh/authorized_keys

Mounting at boot time

If you want to mount the directories at the boot time so you don’t have to mount it manually then just open the /etc/rc.local file and add the mounting command at the end of the file as:

vi /etc/rc.local

add the line given below at the end of the file.

/usr/sbin/sshfs –o idmap=user root@192.168.0.101:/home/backup /backup

That is all.

Now you can share the files remotely at boot time without having to enter password each time you share.

Tags: , , ,

Trackback from your site.

Leave a comment

*