How to Mount a Remote Linux FileSystem using SSHFS on Ubuntu 18.04
In this wiki article, we will learn how to create a link between two servers to share the Disk / FileSystem / even Directories using Secure Shell File System.
We assume you have an SSH Key-based Authentication configured on both the servers. This will provide the SSHFS to reconnect and automount the FileSystem after a brief disconnection caused by either network loss or if the system is unreachable.
In this article,
- host1 will be the local system and host2 will be the remote system
- We will mount the FileSystem from host2 to host1 and have access to data on both servers.
Step 1: Install SSHFS Client
Run the following command on both servers:
apt-get install sshfs
Step 2: Create a Mount Directory on Local System
We will create a mount point directory so we can mount the remote FileSystem.
mkdir /mnt/backup
Of-course in your case, you can have the mount directory as required.
Step 3: Mounting Remote FileSystem using SSHFS
On our Host2 or the Remote Server, we have a directory /root/remoteFiles
which we will use for mounting.
You can have a whole Disk or Partition or a specific Directory to mount.
sshfs -o allow_other,IdentityFile=~/.ssh/id_rsa root@host2:/root/remoteFiles /mnt/backup
Since we have SSH Key-based Authentication configured, we will have to include the path to the public key with option IdentityFile
Usage:
- allow_other: Allow access to other users.
- IdentityFile=/path/to/.ssh/keyfile : Path to your SSH key file (normally, it's "/.ssh/id_rsa").
Step 4: Verifying the Remote FileSystem
Since we have mounted the remote FileSystem to /mnt/backup
, you can change directory and list out the files present on the mounted FS.
ls /mnt/backup/
In case you do not have any files or data, simply create one and it should be accessible on both the servers.
Another way to confirm would be using df -Th
,
root@host1:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 463M 0 463M 0% /dev
tmpfs tmpfs 99M 620K 98M 1% /run
/dev/vda2 xfs 25G 2.2G 23G 9% /
tmpfs tmpfs 493M 0 493M 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 493M 0 493M 0% /sys/fs/cgroup
root@host2:/root/remoteFiles fuse.sshfs 25G 3.0G 22G 13% /mnt/backup
tmpfs tmpfs 99M 0 99M 0% /run/user/0
Step 5: Making the Mount Permanent and Automatic.
To avoid any disconnections from the Remote server and eventually loose the mounted FileSystem.
We will create a way to mount the FileSystem if the remote server gets disconnected by any sort of network issue.
We will fix it by updating the /etc/fstab
file.
vi /etc/fstab
At the end of the file, add the following line,
root@host2:/root/remoteFiles /mnt/backup fuse.sshfs delay_connect,_netdev,IdentityFile=~/.ssh/id_rsa,allow_other,reconnect,ServerAliveInterval=45,ServerAliveCountMax=2 0 0
Save and exit the file and run the following command to update and confirm there are no errors.
mount -a
We will see what the options used in the above command does,
- _netdev: tells it is a network device, not a block device.
- IdentityFile=/path/to/.ssh/keyfile : Path to your SSH key file (normally, it's "/.ssh/id_rsa").
- allow_other: Allow other users than the mounter (i.e. root) to access the share.
- reconnect,ServerAliveInterval=45,ServerAliveCountMax=2: is the part where it'll reconnect to the remote FileSystem after being disconnected.
Step 6: Unmounting the Remote FileSystem
To unmount a FileSystem, run the following command and also remove the entry from /etc/fstab
umount /mnt/backup
This would wrap up with Mounting FileSystem on the local server using SSHFS.
CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME
for 10% off!
1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month
Available Locations: LAX | MIA | ATL | FRA | AMS