How to mount remote filesystem using SSH
You can access remote filesystem as a local filesystem by mounting the remote filesystem using sshfs. This is similar to other remote filesystem implementations such as SAMBA/CIFS or NFS.
sshfs uses SFTP protocol and requires ssh access to the remote server to work. The SSH user on the remote server must also have permission on the folder you want to mount.
The remote filesystem of SSH-enabled host can be mounted as a local filesystem by installing and configuring sshfs from the terminal. If you want just to copy or perform a simple file transfer, you can just use scp or sftp without mounting the remote filesystem.
Related: How to copy file remotely via SSH
Steps to mount remote filesystem using sshfs:
-
Launch terminal.
-
Install sshfs on the local host.
[email protected]:~$ sudo apt update && sudo apt install --assume-yes sshfs #Ubuntu and Debian variance [email protected]:~$ sudo dnf --enablerepo=PowerTools --assumeyes install fuse-sshfs #CentOS and Red Hat variance
-
Test SSH access to the remote host.
[email protected]:~$ ssh [email protected] The authenticity of host '192.168.111.20 (192.168.111.20)' can't be established. ECDSA key fingerprint is SHA256:w3b7BfZzQrrY75Fx1XYH0t2RVkiXb7r6+gcC5umTR7o. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.111.20' (ECDSA) to the list of known hosts. [email protected]'s password: Welcome to Ubuntu 20.10 (GNU/Linux 5.8.0-25-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage 0 updates can be installed immediately. 0 of these updates are security updates. The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. [email protected]:~$ exit logout Connection to 192.168.111.20 closed.
Make sure the user that you're connecting to have full access to the directory that you want to mount.
-
Create new directory locally to mount the remote filesystem if it doesn't already exist.
[email protected]:~$ mkdir -p /home/localuser/remote
-
Manually mount remote filesystem using sshfs.
[email protected]:~$ sshfs [email protected]:/home/remoteuser/shared /home/localuser/remote [email protected]'s password:
-
Check if mount successful.
[email protected]:~$ df -h Filesystem Size Used Avail Use% Mounted on tmpfs 391M 1.8M 389M 1% /run /dev/sda3 20G 6.4G 12G 35% / tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup /dev/sda2 512M 7.8M 505M 2% /boot/efi tmpfs 391M 104K 391M 1% /run/user/1000 [email protected]:/home/remoteuser/shared 20G 6.4G 12G 35% /home/localuser/remote
-
Access mount point to test.
[email protected]:~$ touch /home/localuser/remote/file.txt
-
Unmount remote sshfs filesystem.
[email protected]:~$ umount /home/localuser/remote
-
Check if unmount successful.
[email protected]:~$ df -h Filesystem Size Used Avail Use% Mounted on tmpfs 391M 1.8M 389M 1% /run /dev/sda3 20G 6.4G 12G 35% / tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup /dev/sda2 512M 7.8M 505M 2% /boot/efi tmpfs 391M 104K 391M 1% /run/user/1000
-
Open /etc/fstab using your preferred text editor to configure automatic mounting using sshfs during system startup.
[email protected]:~$ sudo vi /etc/fstab
This method require passwordless SSH configured for the user.
Related: How to SSH without password
-
Add sshfs entry to /etc/fstab.
sshfs#[email protected]:/home/remoteuser/shared /home/localuser/remote fuse user,_netdev,reconnect,uid=1000,gid=1000,idmap=user 0 0
Use id command to get the uid and gid of the user.
[email protected]:~$ id uid=1000(user) gid=1000(user) groups=1000(user),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),121(lpadmin),132(lxd),133(sambashare)
-
Mount remote filesystem via /etc/fstab.
[email protected]:~$ mount /home/localuser/remote
-
Check if mount successful.
[email protected]:~$ df -h Filesystem Size Used Avail Use% Mounted on tmpfs 391M 1.9M 389M 1% /run /dev/sda3 20G 6.4G 12G 35% / tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup /dev/sda2 512M 7.8M 505M 2% /boot/efi tmpfs 391M 108K 391M 1% /run/user/1000 [email protected]:/home/remoteuser/shared 20G 6.4G 12G 35% /home/localuser/remote