mount remote file system

How to Mount Remote Filesystem or Directory in Linux

Typically, we mount local filesystem or directory in our Linux system. But sometimes you may need to mount remote filesystem or directory in Linux. In this article, we will learn how to mount remote filesystem or directory in Linux. This is useful for system administrators who need to work with remote systems. We will be using SSHFS (Secure Shell File system) for this purpose. It allows you to access & transfer files & directories over SSH. It also allows you to create a remote filesystem, without affecting kernel code.

How to Mount Remote Filesystem or Directory in Linux

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

1. Install SSHFS Client in Linux

First of all, open terminal and run the following command to install SSHFS on your local client system.

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

2. Create Mount Directory

Once SSHFS is installed, create a mount directory with the following command.

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

3. Mount Remote Filesystem

Once you have created the mount point, run the following command as root to mount your remote file system at /mnt/test. Replace user_name as remote SSH username, and x.x.x.x with the remote SSH IP address. The following command will mount the remote directory /home/test at local mount point /mnt/test.

# sshfs user_name@x.x.x.x:/home/test/ /mnt/test [RHEL/CentOS/Fedora]
$ sudo sshfs -o allow_other user_name@x.x.x.x:/home/test/ /mnt/test [Ubuntu/Debian]  

You will be asked for SSH password which you need to enter. If your remote SSH user is configured to use key-based authentication then you need to use the following command instead.

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

4. Verify Remote Filesystem

Once the remote filesystem is mounted you can access its contents as if it were a local folder /mnt/test.

# cd /mnt/tecmint
# ls

You will also be able to see the remote filesystem using the following command.

# df -hT

5. Mount Remote Filesystem Permanently

The above steps will mount the remote filesystem only until the next reboot. Once you reboot your local system, the above mounting will be lost. If you want to permanently mount a remote filesystem, you need to make the following changes to /etc/fstab file.

Open it in a text editor with the following command.

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

Add the following line to the bottom of the file. This command will be auto executed every time your system boots. Replace user_name with your SSH username and x.x.x.x with the remote IP address.

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

Make sure you have enabled passwordless SSH login in your remote server. Otherwise, the mounting will fail, since it did not receive a password.

In case your remote server accepts key-based authentication, add the following command instead.

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

After you add the above line, add the following command.

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

Save and close the file.

6. Unmount the file system

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

# umount /mnt/test

In this article, we have learnt how to mount remote file system in Linux. It is a great way to easily access remote files from your local system.

Also read:

How to Copy Files from Linux to Windows
How to Set Process Priority in Linux
How to Change Default MySQL Data Directory
How to Fix firewall-cmd Command Not Found
How to Protect Hard & Soft Links in Linux

3 thoughts on “How to Mount Remote Filesystem or Directory in Linux

  1. I am trying to share this mount with apache and getting access denied, what are the options to use it with apache directory though it is given

    Order allow,deny
    Allow from all

    • For apache to be able to access a directory its owner needs to be www-data. Please ensure the owner of your target directory is www-data and not root or another Linux user. If it is not www-data please chown command to change the owner of target directory.

Leave a Reply

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