Coolest Linux Trick Ever!
While I hate to get too excited about anything, I have to say that this trick has been really helpful. Pretty much all of this can be done by using scp, but using SSHFS to remote mount a filesystem over SSH, really makes it simple to modify a remote file, copy, diff, compare, anything you need. This is a cool trick, the Coolest Linux Trick Ever!
NFS or Samba not Available
When you need to copy, compare, or edit files on a remote machine it can sometimes be easier to mount the remote directories and use and manipulate the files locally than to ssh over to the other machine. This is when SSHFS becomes very useful. While NFS and Samba require time and effort to share a directory or filesystem, SSHFS doesn’t require any changes on the hosting machine at all, and the only changes you need to make on the client machine are insignificant and minor.
All of this is easier to see by showing an quick example. Let’s suppose that in the home directory of a user named mary on machine2, I have a file named “file1” that I want to use on machine1. I can quickly mount the remote directory with sshfs and have access to it.
Install Fuse-sshfs Locally (machine1)
The only thing that needs to be installed is fuse-sshfs which is quickly done with yum:
# yum install fuse-sshfs |
Create a MountPoint on the Client Machine (machine1)
We need to create a local directory on machine1 that we can use for mounting machine2’s directory:
# mkdir $HOME/machine2 |
Mount a Remote Directory with SSHFS
Now, we are ready to mount the remote directory
[root@machine1 ~]# sshfs mary@machine2.uptimemadeeasy.com:/home/mary $HOME/othermachine The authenticity of host ‘machine2.uptimemadeeasy.com (x.y.q.z)’ can’t be established. RSA key fingerprint is ac:1b:ba:d5:1b:28:49:56:a0:df:60:bc:e3:ae:06:48. Are you sure you want to continue connecting (yes/no)? yes mary@machine2.uptimemadeeasy.com’s password: <type mary’s password here> |
Verify the SSHFS Mount
A quick “df” will prove if we have successfully mounted the remote filesystem with SSHFS:
[root@machine1 ~]# df -h /root/machine2 mary@machine2.collegeatlas.org:/home/mary 158G 2.0G 148G 2% /root/machine2 |
Yes. It mounted fine. Now I can see and use file1 from machine2:
[root@machine1 ~]# ls /root/machine2 file1 |
Unmount the Remote Filesystem
When you are done with the files on the other machine, unmount the filesystem and remote the mount point:
[root@machine1 /]# umount /root/machine2 [root@machine1 /]# rmdir /root/othermachine |
That’s it! It was very simple to mount a remote directory on my machine so that I could compare, edit, or otherwise files on machine2 on machine1. It was nearly instant and was much simpler than NFS or Samba.
Latest posts by Jeff Staten (see all)
- Configure Your HP Procurve Switch with SNTP - May 5, 2015
- Configuring HP Procurve 2920 Switches - May 1, 2015
- Troubleshooting Sendmail - November 28, 2014
I wasnt able to donwload the sshfs package until i updated my repository with RHEL EPEL repo:
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
after that, everything worked great! excellent tutorial!