Jay Taylor's notes
back to listing indexHow To Use SSHFS to Mount Remote File Systems Over SSH | DigitalOcean
[web search]How To Use SSHFS to Mount Remote File Systems Over SSH
UpdatedNovember 9, 2016 1.1m views Linux BasicsIntroduction
In many cases it can become cumbersome to transfer files to and from a droplet. Imagine a development usage scenario where you are coding apps remotely and find yourself uploading a script repeatedly to your virtual server to test. This can become quite a hassle in a very short period of time. Luckily there is a way to mount your VPS file system to your local computer so you can make changes on the fly and treat your droplet as local storage. In this article, we will show you how to do exactly that.
Installing SSHFS
On Ubuntu/Debian
SSHFS is Linux based software that needs to be installed on your local computer. On Ubuntu and Debian based systems it can be installed through apt-get.
sudo apt-get install sshfs
On Mac OSX
You can install SSHFS on Mac OSX. You will need to download FUSE and SSHFS from the osxfuse site
On Windows
To install SSHFS in Windows you will need to grab the latest win-sshfs package from the google code repository. A direct download link can be found below. After you have downloaded the package, double click to launch the installer. You may be prompted to download additional files, if so the installer will download the .NET Framework 4.0 and install it for you.
https://win-sshfs.googlecode.com/files/win-sshfs-0.0.1.5-setup.exe
Mounting the Remote File System
The following instructions will work for both Ubuntu/Debian and OSX. Instructions for Windows systems can be found at the bottom of the tutorial.
To start we will need to create a local directory in which to mount the droplet's file system.
sudo mkdir /mnt/droplet <--replace "droplet" whatever you prefer
Now we can use sshfs to mount the file system locally with the following command. If your VPS was created with a password login the following command will do the trick. You will be asked for your virtual server's root password during this step.
sudo sshfs -o allow_other,defer_permissions root@xxx.xxx.xxx.xxx:/ /mnt/droplet
If your droplet is configured for login via ssh key authorization, you will need to tell sshfs to use your public keys with the following command. You will be asked to enter the passphrase you used during the creation of your keys with ssh-keygen.
sudo sshfs -o allow_other,defer_permissions,IdentityFile=~/.ssh/id_rsa root@xxx.xxx.xxx.xxx:/ /mnt/droplet
Now you can work with files on your droplet as if it were a physical device attached to your local machine. For instance, if you move to the /mnt/droplet directory on your local machine you can create a file locally and the file will appear on your virtual server. Likewise you can copy files into the /mnt/droplet folder and they will be uploaded to your droplet in the background.
It is important to note that this process provides only a temporary mount point to your droplet. If the virtual server or local machine is powered off or restarted, you will need to use the same process to mount it again.
Unmounting the Remote File System
When you no longer need the mount point you can simply unmount it with the command
sudo umount /mnt/droplet
Permanently Mounting the Remote File System
SSHFS also allows for setting up permanent mount points to remote file systems. This would set a mount point that would persist through restarts of both your local machine and droplets. In order to set up a permanent mount point, we will need to edit the /etc/fstab
file on the local machine to automatically mount the file system each time the system is booted.
First we need to edit the /etc/fstab
file with a text editor.
sudo nano /etc/fstab
Scroll to the bottom of the file and add the following entry
sshfs#root@xxx.xxx.xxx.xxx:/ /mnt/droplet
Save the changes to /etc/fstab
and reboot if necessary.
It should be noted that permanently mounting your VPS file system locally is a potential security risk. If your local machine is compromised it allows for a direct route to your droplet. Therefore it is not recommended to setup permanent mounts on production servers.
Using Win-SSHFS to Mount Remote File Systems on Windows
After launching the win-sshfs program, you will be presented with a graphical interface to make the process of mounting a remote file share simple.
Step 1: Click the Add button in the lower left corner of the window.
Step 2: Enter a name for the file share in the Drive Name field.
Step 3. Enter the IP of your droplet in the Host field.
Step 4. Enter your SSH port. (Leave as port 22 unless you have changed the SSH port manually).
Step 5. Enter your username in the Username field. (Unless you have set up user accounts manually you will enter
root
in this field).Step 6. Enter your SSH password in the password field. (Note on Windows you will need to have your droplet configured for password logins rather than ssh-key-authentication).
Step 7. Enter your desired mount point in the Directory field. (Enter
/
to mount the file system from root. Likewise you can enter/var/www
or~/
for your home directory).Step 8. Select the drive letter you would like Windows to use for your droplets file system.
Step 9. Click the Mount button to connect to the droplet and mount the file system.
Now your virtual server's file system will be available through My Computer as the drive letter you chose in step 8.
Usage of the Remote Mount Point
The remote mount behaves similarly to locally mounted storage: you are able to create, copy, move, edit, compress or perform any file system operations you would be able to do on the droplet, but you are not able to launch programs or scripts on the remote server.
One typical usage of this would be if you host a website on your VPS and need to make changes to the website on a regular basis. Mounting the file system locally allows you to launch whatever code editor, IDE, or text editor you wish to edit the site, and any changes you make will reflect on the virtual server as soon as they are made on your local machine.
Similarly, on droplets used for testing purposes of coding projects, it allows for much simpler code modifications which can be tested immediately without the need to modify the code locally as well as remotely (and eliminates the hassle of uploading new copies of files for small code changes).
Write about Docker - We'll donate to a tech nonprofit
Partner with us to publish an article on Docker and we’ll donate up to $300 to a charity of your choice.
Learn More53 Comments
- B
- I
- UL
- OL
- Link
- Code
- Highlight
- Table
-
Are there any good (non-abandoned) GUI for mounting remote file systems over SSH on OS X, from a menubar icon? Panic Transmit does it very well, but it would be great to have a free alternative that would simply runs the command shown in this article in the background to mount a file system.
-
-
Not a new way , and particularly sucks given the slow speed !!! I think you must enable some options for sync reads and write .
-
-
Using a lighter encryption algorythm makes it much faster, I recommend this one if you're not worried about security too much
-o ssh_command="ssh -Cc arcfour256"
-
-
for OSX , SSHFS GUI hosted on code.google.com works fine ... else even MacFusion used to work .. but the recent version seems not to work on at-least Mavericks . For SFTP , think this does most of the job, CyberDuck the best option .
-
you are confusing things. sftp is completely different program with different ideas behind. you can't run emacs on your computer in order to edit remote file with sftp, while with sshfs you can.
-
-
If you don't want to add SSHFS settings in /etc and prefer a GUI and per-user configurable accesses, you should check Qasim (contributions are welcome) : http://glenux.github.io/qasim/
-
Failed on Mavericks. After the sshfs command was executed locally, the password was continually rejected.
-
if you use KDE, launch dolphin and just type as path "fish://myserver.com/mypath", it will ask your username and password and that's it. It cannot be simpler than that
-
@ruben: Thanks for the tip! You can do the same in GNOME and other desktops that use nautilus as their file manager using:
nautilus sftp://root@162.243.197.120
-
This is some great information, and I'm glad I read through all the comments. Now that I see I can use nautilus to open files, I'm wondering when you would mount a file system instead... file transfer faster?
-
I have just tried to do exactly what's said here but all I get is "read: connection reset by peer" EVERY single time. Why isn't it working for me??
-
@timothyholt123: Try passing ```-o sshfsdebug
to
sshfs``` to make it print debugging information. Does it output any errors? -
@timothyholt123: I just got this because I was using sudo and it wasn't passing my
SSH_AGENT_PID
to the sudo environment. So I just dropped the sudo, since it was overkill. You could also pass the environment to sudo with -E option. -
Thanks a million to the author of this excellent article. It was quite helpful for me, because I was looking for a long time for a solution to the problem of mounting remote files to my laptop, which is easier than NFS or OpenAFS.
Everything works just fine. I do not agree with the previous comment that it is slow. I believe that if your connection allows you work comfortably with ssh then sshfs will be fast enough.
A couple of comments: author is calling all commands using sudo. I think it may be not very convenient to add every program, which you want to use to edit mounted files, to sudoers. So I have just changed the owner of the directory /mnt/droplet to my username and group and can do everything directly, without sudo.
And I decided that it is better to call the directory /mnt/sshfs/droplet in order to remember, for which program this mountpoint is created. So, in case I use other programs for mounting in future, I can create for them a different mount point, for example, /mnt/openafs/droplet
-
I reckon this is the best option.
sshfs -o IdentityFile=~/.ssh/id_rsa,auto_cache,reconnect,defer_permissions,negative_vncache,volname=Deploy root@linw.live:/opt/wildfly-8.1.0.Final/standalone/deployments ~/Deploy
I like the folder ~/Deploy could be anything you want though, just change the volume name as well.
-
How can I make this work while root access to ssh is rejected? I'd assume I could use user@$REMOTE_SERVER , bit would I have to use a script to connect and su - ? I've added that user to the sudoers file, but still does not want to work?
-
Try passing
-o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server"
tosshfs
:sudo sshfs -o IdentityFile=~/.ssh/id_rsa,sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" root@xxx.xxx.xxx.xxx:/ /mnt/droplet
-
-
I am able to mount my servers dev site docroot just fine, but locally I cannot see any hidden files like .htaccess, .git, .gitignore, etc. Any else have this issue and/or a solution?
-
-
sshfs works very well for mounting my VMs across my LAN, don't like it for remote mounts, seems pretty unstable in those cases.
The windows client is particularly bad, I only trust it to mount VMs shares on the same pc.The only problem I have on the mounted drives is that I can't commit with git.
-
I followed these instructions exactly and still had connection problems.
I was trying to connect my Ubuntu 14.04 laptop to an Ubuntu 14.04 droplet that I have setup SSH access without password and without a password for the key.
I was getting "permission denied" at the mount folder, even with sudo.
What fixed it is adding my account to the fuse group:
sudo gpasswd -a USERNAME fuse
And also editing /etc/fuse.conf on my laptop and enabling a flag by uncommenting the second line here:
# Allow non-root users to specify the allow_other or allow_root mount options. user_allow_other
Lastly the mount command needs to be modified with an extra option:
sudo sshfs -o allow_other -o IdentityFile=~/.ssh/id_rsa root@xxx.xxx.xxx.xxx:/ /mnt/droplet
-
-
It works after I follow this...why is this not posted more prominently??
Thanks.-
I guess the article is written for a certain use case, the more common one. Because of the special circumstance of not using a password for both the key and server, a special workaround is needed. I agree the article should be amended but for these cases the comments work wonders! @aschuss @jayliu50
-
-