Gangmax Blog

自由之思想,独立之精神

Mount Remote SSH Server as Local Directory via 'Sshfs'

| Comments

这个想法的灵感来自这篇文章。该文提到了在Mac OS下使用FUSE for OS XMacfusion实现了“将远程服务器的文件系统映射成本地文件系统的一部分(通过SSH)”。

于是很自然地,我相信在Linux下也有对应的软件可以做同样的事情。

This idea was inspired by this article, which mentioned that the author used FUSE for OS X and Macfusion to map the remote server as a local directory via SSH.

So I searched the alternative on Linux and find the ”sshfs” package is just what I need, which is just a 40KB package file. Here’s how to do so:

1
2
3
4
# If you want to create an user on the SSH server side with which to login the SSH server from a client, you can do like below on the server side:

sudo useradd -u 0 -g 0 -o -d /home/auser -m auser # Create "auser" account with root permission.
sudo passwd auser # Set the password of "auser" account.
1
2
3
4
5
6
7
# In this example, it assume your current local user account is "auser", and you have already created the "auser" account on the remote SSH server. All the following commands run on the local computer and the remote server should be accessable via SSH.
sudo apt-get install sshfs # Install the sshfs package.
sudo mkdir /media/ssh71 # Create the mount point directory.
sudo chown auser /media/ssh71/ # Change the owner of the directory to the current user.
sudo chgrp auser /media/ssh71/ # Change the group of the directory to the current user's group
chmod 700 /media/ssh71/ # Change the permissions of the mount point directory.
sshfs auser@172.16.10.71:/home/auser /media/ssh71/ # Mount the remote SSH server to the given directory.

Then you should see a mounted “disk” named “ssh71” in your file manager(such as Nautilus), just like what you see after you plug a flash drive in your USB port. Then you can use remote file system as a local flash drive, which is very handy.

And of cource, it won’t work after you restart your computer. You can run the “sshfs” command after you restart your computer, or save it as a script and run it.

Added@20130218:

You can use the “ssh-copy-id” command from the client computer to let the SSH server knows the client and it won’t need the client input the password anymore(from here):

1
2
ssh-copy-id auser@172.16.10.71
# You'll be prompted the password. After that the server side will store your public key in it's "~/.ssh/authorized\_keys" file.

Comments