mount remote folder in linux

How to Mount Remote Directory or Filesystem in Linux

Sometimes you may need to mount remote directory or filesystem in Linux. In this article, we will learn how to do this using SSHFS client over SSH. You can use these steps to mount remote folders on their local system.


What is SSHFS?

SSHFS stands for Secure Shell Filesystem that allows you to mount remote folders and filesystems via local machine and access them via SFTP (Secure File Transfer Protocol). It comes with FUSE (Filesystem in Userspace) that allows anyone to securely create their filesystem without modifying kernel code.


How to Mount Remote Directory or Filesystem in Linux

Here are the steps to mount remote directory or filesystem in Linux.


1. Install SSHFS Client in Linux Systems

By default, sshfs is not present on most Linux distributions. Open terminal and run the following command to install SSHFS on your system. Here are the commands to install SSHFS on different Linux systems.

# yum install sshfs
# dnf install sshfs              [On Fedora 22+ releases]
$ sudo apt-get install sshfs     [On Debian/Ubuntu based systems]


2. Create SSHFS Mount Directory

Once SSHFS is installed, you need to create a mount point for your folder, where you will mount the remote folder. Here is the command to create mount point at /mnt/test

# mkdir /mnt/test
$ sudo mkdir /mnt/test     [On Debian/Ubuntu based systems]


3. Mount Remote Folder

Once you have created mount point for your folder, run the following command to mount the remote folder as root. We will mount remote folder /home/test to local folder /mnt/test. Replace x.x.x.x with the IP address of your remote folder. Replace test_user with your remote username. Replace /home/test with the location of your remote folder.

# sshfs test_user@x.x.x.x:/home/test/ /mnt/test
$ sudo sshfs -o allow_other test@x.x.x.x:/home/test/ /mnt/test     [On Debian/Ubuntu based systems]

If your Linux server is configured with key-based authorization, then you will need to specify path to public keys.

# sshfs -o IdentityFile=~/.ssh/id_rsa test_user@x.x.x.x:/home/test/ /mnt/test
$ sudo sshfs -o allow_other,IdentityFile=~/.ssh/id_rsa test@x.x.x.x:/home/test/ /mnt/test     [On Debian/Ubuntu based systems]


4. Verify Remote Filesystem is Mounted

Once you have successfully mounted the remote folder, you can access it as if it is a local directory and view its contents.

# cd /mnt/tecmint
# ls

Alternatively, you can check the mount point by check if it is listed when you run df command.

# df -hT


5. Mount Remote Filesystem Permanently

The above steps will mount remote filesystem only until the next reboot. If you want to permanently mount remote folder, then you need to open the file /etc/fstab in a text editor.

# vi /etc/fstab
$ sudo vi /etc/fstab     [On Debian/Ubuntu based systems]  

Add the following line to the bottom of the file. Replace test_user with your remote username, x.x.x.x with remote folder’s IP address, /home/test with remote folder location and /mnt/test with the local mount point of remote folder.

sshfs#test_user@x.x.x.x:/home/test/ /mnt/test fuse.sshfs defaults 0 0

If your SSH is configured to use key-based authentication, add the following command instead of above command, specifying the key location.

sshfs#test@x.x.x.x:/home/test/ /mnt/test fuse.sshfs IdentityFile=~/.ssh/id_rsa defaults 0 0

Save and exit the file. Run the following command to mount the file.

# mount -a
$ sudo mount -a   [On Debian/Ubuntu based systems]


6. Unmount Filesystem

If you want to unmount the remote folder, run the following command.

# umount /mnt/test

In this article, we have learnt how to mount remote filesystem in Linux.

Also read:

How to Check Bad Sectors in Linux
How to Convert Markdown to HTML in Linux
How to Create SFTP User for Specific Folder
How to Open File Dialog Box in Python
How to Share Folders Between Linux Systems

Leave a Reply

Your email address will not be published. Required fields are marked *