Mounting Remote Directories With SSHFS On Debian Squeeze
|
Submitted by falko (Contact Author) (Forums) on Tue, 2011-09-20 17:50. :: Debian | Security | Storage
Mounting Remote Directories With SSHFS On Debian SqueezeVersion 1.0 This tutorial explains how you can mount a directory from a remote server on the local server securely using SSHFS. SSHFS (Secure SHell FileSystem) is a filesystem that serves files/directories securely over SSH, and local users can use them just as if the were local files/directories. On the local computer, the remote share is mounted via FUSE (Filesystem in Userspace). I will use Debian Squeeze for both the local and the remote server. I do not issue any guarantee that this will work for you!
1 Preliminary NoteI'm using the following two systems in this tutorial:
I will show how to use SSHFS as root and also as a normal user.
2 Installing SSHFSserver1: On the local system, SSHFS must be installed as follows: apt-get install sshfs Afterwards, make sure that the fuse kernel module is loaded: lsmod | grep fuse root@server1:~# lsmod | grep fuse
3 Using SSHFS As rootserver1: Now I want to mount the remote directory /home/backup (on server2, owned by server2's root user) to the local /backup directory as the local root user. First add root to the fuse group: adduser root fuse Create the local /backup directory and make sure it's owned by root (it should be anyway as you are running these commands as root): mkdir /backup Then mount the remote /home/backup directory to /backup: sshfs -o idmap=user root@192.168.0.101:/home/backup /backup (You can use a full path for the remote system, as sown above, or a relative path, like this: sshfs -o idmap=user root@192.168.0.101:backup /backup If you use a relative path, this path is relative to the remote user's home directory, so in this case it would be /root/backup. You can even leave out the remote directory, as follows: sshfs -o idmap=user root@192.168.0.101: /backup This would then translate to the remote user's home directory - /root in this case. ) -o idmap=user makes that it does not matter if the local and the remote system use different user IDs - files owned by the remote user are also owned by the local user. If you don't use this, you might get permission problems. If you connect to the remote host for the first time, you will see a warning about the authenticity of the remote host (if you have connected to the remote host before using ssh or scp, you will not see the warning). In any case, you will be asked for the root password for server2: root@server1:~# sshfs -o idmap=user root@192.168.0.101:/home/backup /backup Let's check if the remote directory got mounted to /backup: mount root@server1:~# mount df -h root@server1:~# df -h Looks good! To unmount the share, run fusermount -u /backup
3.1 Creating A Private/Public Key Pair On server1Of course, we don't want to type in a password every time we try to mount the remote share. Therefore we create a private/public key pair and transfer the public key to server2 so that we will not be asked for a password anymore. server1: Create a private/public key pair on server1.example.com: ssh-keygen root@server1:~# ssh-keygen It is important that you do not enter a passphrase otherwise mounting will not work without human interaction so simply hit ENTER! Next, we copy our public key to server2.example.com: ssh-copy-id -i $HOME/.ssh/id_rsa.pub root@192.168.0.101 Now check on server2 if server1's public key has correctly been transferred: server2: cat $HOME/.ssh/authorized_keys
Now back on server1, try to mount the remote share again (make sure it's unmounted before you run the command): server1: sshfs -o idmap=user root@192.168.0.101:/home/backup /backup If all goes well, you should not be prompted for a password: root@server1:~# sshfs -o idmap=user root@192.168.0.101:/home/backup /backup
3.2 Mounting The Remote Share Automatically At Boot Timeserver1: If you don't want to mount the remote share manually, it is possible to have it mounted automatically when the system boots (provided you have followed chapter 3.1 because otherwise an automatic mount is not possible because you will be asked for a password). Normally we would modify /etc/fstab to achieve this, but unfortunately the network isn't up yet when /etc/fstab is processed in the boot process, which means that the remote share cannot be mounted. To circumvent this, we simply add our mount command to /etc/rc.local, which is the last file to be processed in the boot process, and at that time the network is up and running: vi /etc/rc.local
You can test this by simply rebooting your system: reboot After the reboot, you can check with mount and df -h if the remote share got mounted.
|




Recent comments
12 hours 41 min ago
15 hours 36 min ago
16 hours 50 min ago
18 hours 14 min ago
19 hours 52 min ago
21 hours 20 min ago
22 hours 34 min ago
1 day 14 hours ago
1 day 15 hours ago
1 day 19 hours ago