{"id":10161,"date":"2021-10-07T23:40:24","date_gmt":"2021-10-07T20:40:24","guid":{"rendered":"https:\/\/kifarunix.com\/?p=10161"},"modified":"2024-03-18T08:43:38","modified_gmt":"2024-03-18T05:43:38","slug":"mount-remote-filesystem-over-ssh-using-sshfs","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/mount-remote-filesystem-over-ssh-using-sshfs\/","title":{"rendered":"Mount Remote Filesystem Over SSH using SSHFS"},"content":{"rendered":"\n
Follow through this tutorial to learn how to mount remote filesystem over SSH using SSHFS. sshfs<\/a> is a filesystem client based on the SSH File Transfer Protocol.<\/p>\n\n\n\n According to man pages;<\/p>\n\n\n\n “SSHFS (Secure SHell FileSystem) is a file system for Linux (and other operating systems with a FUSE implementation, such as Mac OS X or FreeBSD) capable of operating on files on a remote computer using just a secure shell login on the remote computer. On the local computer where the SSHFS is mounted, the implementation makes use of the FUSE (Filesystem in Userspace) kernel module. The practical effect of this is that the end user can seamlessly interact with remote files being securely served over SSH just as if they were local files on his\/her computer. On the remote computer the SFTP subsystem of SSH is used<\/em>“.<\/p>\n\n\n\n In this setup, we will be using two servers for demonstrating how SSHFS can be used to mount remote filesytem via SSH protocol.<\/p>\n\n\n\n <\/p>\n<\/div><\/div>\n\n\n\n Install SSHFS on the client system.<\/p>\n\n\n\n If you are running Ubuntu\/Debian based systems, run the command below to install SSHFS package.<\/p>\n\n\n\n On CentOS\/RHEL systems;<\/p>\n\n\n\n For other distributions, refer to their package manager docs on how to install SSHFS package.<\/p>\n\n\n\n The syntax of the SSHFS command is;<\/p>\n\n\n\n On your client system, you can mount the remote file-system as a standard user or root user.<\/p>\n\n\n\n For example, to mount the remote system log directory, Note that, in this particular example, the Mounting root owned filesystems remotetly means that you also need to configure remote SSH server to allow remote root login.<\/p>\n\n\n\n This is a risk. However, you can restrict this to specific client.<\/p>\n\n\n\n On the server, you can add such lines to Checking the mount points on the client system;<\/p>\n\n\n\n Checking the contents of the remote filesystem\/directory;<\/p>\n\n\n\n You can read the logs in realtime as if they were on the local system;<\/p>\n\n\n\n You can use a standard remote user if what you are mounting is owned by that user.<\/p>\n\n\n\n For example, to mount the remote system Note, as a standard user on the client system, you can only mount remote filesystems to directories you have permissions to write to.<\/p>\n\n\n\n Btw, it is good to note that sshfs doesnt intepret ~ ( To ensure seamless mounting of remote filesystem even when the remote server reboots, you can use However, you first need to generate the passwordless SSH keys (empty passphrase) and copy them to a remote system.<\/p>\n\n\n\n Copy the key to the remote user you are using for mounting;<\/p>\n\n\n\n For example, to copy the above key to a remote server user;<\/p>\n\n\n\n You should now be able to login without being prompted for password.<\/p>\n\n\n\n Next, update the fstab file with the mount details.<\/p>\n\n\n\n For example, to automatically mount the remote To mount the remote Make the appropriate adjustments.<\/p>\n\n\n\n Reboot the system and verify if the remote directory was mounted.<\/p>\n\n\n\n To unmount the filesystem, run the command below;<\/p>\n\n\n\n For example;<\/p>\n\n\n\n And that concludes our guide on how to mount remote filesystem over SSH via SSHFS.<\/p>\n\n\n\n Read more on Install and Configure NFS Server on Rocky Linux 8<\/a><\/p>\n\n\n\n Allow\/Deny Specific Users to Login via SSH<\/a><\/p>\n\n\n\nMounting Remote Filesystem Over SSH using SSHFS<\/h2>\n\n\n\n
+---------------------------+ +----------------------------+\n| Log Manager | | Web Server |\n| logman.kifarunix-demo.com +----+----+ web.kifarunix-demo.com |\n| 192.168.58.21 | | 192.168.58.22 |\n| SSH Client\/SSHFS | | SSH Server |\n+---------------------------+ +----------------------------+<\/code><\/pre>\n\n\n\n
Install SSHFS on Linux Client System<\/h3>\n\n\n\n
apt install sshfs -y<\/code><\/pre>\n\n\n\n
\nReading package lists... Done\nBuilding dependency tree... Done\nReading state information... Done\nThe following additional packages will be installed:\n fuse3 libfuse3-3\nThe following NEW packages will be installed:\n fuse3 libfuse3-3 sshfs\n0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.\nNeed to get 166 kB of archives.\nAfter this operation, 528 kB of additional disk space will be used.\nGet:1 http:\/\/deb.debian.org\/debian bullseye\/main amd64 libfuse3-3 amd64 3.10.3-2 [85.3 kB]\nGet:2 http:\/\/deb.debian.org\/debian bullseye\/main amd64 fuse3 amd64 3.10.3-2 [34.7 kB]\nGet:3 http:\/\/deb.debian.org\/debian bullseye\/main amd64 sshfs amd64 3.7.1+repack-2 [46.0 kB]\nFetched 166 kB in 2s (97.8 kB\/s)\nSelecting previously unselected package libfuse3-3:amd64.\n(Reading database ... 30388 files and directories currently installed.)\nPreparing to unpack ...\/libfuse3-3_3.10.3-2_amd64.deb ...\nUnpacking libfuse3-3:amd64 (3.10.3-2) ...\nSelecting previously unselected package fuse3.\nPreparing to unpack ...\/fuse3_3.10.3-2_amd64.deb ...\nUnpacking fuse3 (3.10.3-2) ...\nSelecting previously unselected package sshfs.\nPreparing to unpack ...\/sshfs_3.7.1+repack-2_amd64.deb ...\nUnpacking sshfs (3.7.1+repack-2) ...\nSetting up libfuse3-3:amd64 (3.10.3-2) ...\nSetting up fuse3 (3.10.3-2) ...\nupdate-initramfs: deferring update (trigger activated)\nSetting up sshfs (3.7.1+repack-2) ...\nProcessing triggers for initramfs-tools (0.140) ...\nupdate-initramfs: Generating \/boot\/initrd.img-5.10.0-8-amd64\nProcessing triggers for libc-bin (2.31-13) ...\nProcessing triggers for man-db (2.9.4-2) ...\n<\/code><\/pre>\n\n\n\n
dnf install sshfs -y<\/code><\/pre>\n\n\n\n
\nCentOS-8 - AppStream 5.0 kB\/s | 4.3 kB 00:00 \nCentOS-8 - AppStream 3.8 MB\/s | 9.3 MB 00:02 \nCentOS-8 - Base 6.6 kB\/s | 3.9 kB 00:00 \nCentOS-8 - Base 3.9 MB\/s | 7.5 MB 00:01 \nCentOS-8 - Extras 2.6 kB\/s | 1.5 kB 00:00 \nCentOS-8 - Extras 18 kB\/s | 10 kB 00:00 \nCentOS-8 - PowerTools 8.5 kB\/s | 4.3 kB 00:00 \nCentOS-8 - PowerTools 1.7 MB\/s | 2.4 MB 00:01 \nDependencies resolved.\n============================================================================================================================================================================\n Package Architecture Version Repository Size\n============================================================================================================================================================================\nInstalling:\n fuse-sshfs x86_64 2.8-5.el8 PowerTools 56 k\nInstalling dependencies:\n fuse x86_64 2.9.7-12.el8 BaseOS 82 k\n fuse-common x86_64 3.2.1-12.el8 BaseOS 21 k\n\nTransaction Summary\n============================================================================================================================================================================\nInstall 3 Packages\n\nTotal download size: 160 k\nInstalled size: 320 k\nDownloading Packages:\n(1\/3): fuse-common-3.2.1-12.el8.x86_64.rpm 159 kB\/s | 21 kB 00:00 \n(2\/3): fuse-2.9.7-12.el8.x86_64.rpm 609 kB\/s | 82 kB 00:00 \n(3\/3): fuse-sshfs-2.8-5.el8.x86_64.rpm 401 kB\/s | 56 kB 00:00 \n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------\nTotal 163 kB\/s | 160 kB 00:00 \nRunning transaction check\nTransaction check succeeded.\nRunning transaction test\nTransaction test succeeded.\nRunning transaction\n Preparing : 1\/1 \n Installing : fuse-common-3.2.1-12.el8.x86_64 1\/3 \n Installing : fuse-2.9.7-12.el8.x86_64 2\/3 \n Installing : fuse-sshfs-2.8-5.el8.x86_64 3\/3 \n Running scriptlet: fuse-sshfs-2.8-5.el8.x86_64 3\/3 \n Verifying : fuse-2.9.7-12.el8.x86_64 1\/3 \n Verifying : fuse-common-3.2.1-12.el8.x86_64 2\/3 \n Verifying : fuse-sshfs-2.8-5.el8.x86_64 3\/3 \n\nInstalled:\n fuse-sshfs-2.8-5.el8.x86_64 fuse-2.9.7-12.el8.x86_64 fuse-common-3.2.1-12.el8.x86_64 \n\nComplete!\n<\/code><\/pre>\n\n\n\n
Using SSHFS to Mount Remote Filesystem Over SSH<\/h3>\n\n\n\n
sshfs [user@]host:[dir] mountpoint [options]<\/code><\/pre>\n\n\n\n
\/var\/log<\/code>, under the
\/media\/<\/code> directory on the client system, simply run the command below;<\/p>\n\n\n\n
\/var\/log<\/code>, is remotely owned by root user. As such, if you want to mount this directory locally, then you need to mount it as a remote root user.<\/p>\n\n\n\n
sshfs root@192.168.58.22:\/var\/log \/media\/<\/code><\/pre>\n\n\n\n
\/etc\/ssh\/sshd_config<\/strong><\/code> file.<\/p>\n\n\n\n
PermitRootLogin no\nMatch Address 192.168.58.1\n PermitRootLogin yes<\/code><\/pre>\n\n\n\n
df -hT -P \/media\/<\/code><\/pre>\n\n\n\n
Filesystem Type Size Used Avail Use% Mounted on\nroot@192.168.58.22:\/var\/log fuse.sshfs 15G 1.3G 13G 9% \/media<\/code><\/pre>\n\n\n\n
ls -1 \/media\/ | column -c 50<\/code><\/pre>\n\n\n\n
alternatives.log\tjournal\napt\t\t\tkern.log\nauth.log\t\tlastlog\nbtmp\t\t\tmessages\ndaemon.log\t\tprivate\ndebug\t\t\trunit\ndpkg.log\t\tsyslog\nfaillog\t\t\twtmp\ninstaller<\/code><\/pre>\n\n\n\n
tail -f \/media\/auth.log<\/code><\/pre>\n\n\n\n
\nOct 7 20:45:43 debian11 systemd-logind[396]: New session 4 of user kifarunix.\nOct 7 20:45:43 debian11 systemd: pam_unix(systemd-user:session): session opened for user kifarunix(uid=1000) by (uid=0)\nOct 7 20:45:49 debian11 su: (to root) kifarunix on pts\/0\nOct 7 20:45:49 debian11 su: pam_unix(su-l:session): session opened for user root(uid=0) by kifarunix(uid=1000)\nOct 7 21:03:01 debian11 sshd[416]: Received signal 15; terminating.\nOct 7 21:03:01 debian11 sshd[944]: Server listening on 0.0.0.0 port 22.\nOct 7 21:03:01 debian11 sshd[944]: Server listening on :: port 22.\nOct 7 21:04:57 debian11 sshd[957]: Accepted password for root from 192.168.58.1 port 45488 ssh2\nOct 7 21:04:57 debian11 sshd[957]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)\nOct 7 21:04:57 debian11 systemd-logind[396]: New session 6 of user root.\nOct 7 21:13:26 debian11 sshd[10524]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.58.1 user=kifarunix\nOct 7 21:13:28 debian11 sshd[10524]: Failed password for kifarunix from 192.168.58.1 port 45598 ssh2\n...\n<\/code><\/pre>\n\n\n\n
\/home\/kifarunix<\/code> directory, login as the owner of this directory. For example;<\/p>\n\n\n\n
sshfs kifarunix@192.168.58.22:\/home\/kifarunix ~\/remote\/<\/code><\/pre>\n\n\n\n
sshfs kifarunix@192.168.58.22:~\/ remote\/<\/code>) as users home directory on remote system.<\/strong><\/p>\n\n\n\n
Using SSHFS to Mount Remote Filesystem Over SSH Permanently<\/h3>\n\n\n\n
\/etc\/fstab<\/strong><\/code>.<\/p>\n\n\n\n
ssh-keygen<\/code><\/pre>\n\n\n\n
\nGenerating public\/private rsa key pair.\nEnter file in which to save the key (\/home\/kifarunix\/.ssh\/id_rsa): \nEnter passphrase (empty for no passphrase): \nEnter same passphrase again: \nYour identification has been saved in \/home\/kifarunix\/.ssh\/id_rsa.\nYour public key has been saved in \/home\/kifarunix\/.ssh\/id_rsa.pub.\nThe key fingerprint is:\nSHA256:Gvclrjgw2hFQJtWl5tteTT9gqB0IqSSM0\/aNVjOgmHM kifarunix@ldapslave.kifarunix-demo.com\nThe key's randomart image is:\n+---[RSA 3072]----+\n| .o=. .. |\n| *.+ ..o |\n|* E.. O |\n| = +.B + . . |\n| =.+ S + = |\n| .+ * = B o |\n| o +o o = . o |\n| . . .o o . |\n| ..o |\n+----[SHA256]-----+\n<\/code><\/pre>\n\n\n\n
ssh-copy-id -i \/path\/to\/key user@remote-host<\/code><\/pre>\n\n\n\n
ssh-copy-id -i \/home\/kifarunix\/.ssh\/id_rsa root@192.168.58.22<\/code><\/pre>\n\n\n\n
\/var\/log<\/code> directory on a client system, update
\/etc\/fstab<\/code><\/strong> by entering the line in the format;<\/p>\n\n\n\n
<filesystem\/remote directory> <mount point> <type> <options> <dump> <pass><\/code><\/pre>\n\n\n\n
\/var\/log<\/strong><\/code> directory, enter the line below to \/etc\/fstab.<\/p>\n\n\n\n
sshfs#root@192.168.58.22:\/var\/log \/mnt fuse reconnect,IdentityFile=\/home\/kifarunix\/.ssh\/id_rsa,_netdev,port=22 0 0<\/code><\/pre>\n\n\n\n
Unmounting the Filesystem\/directory<\/h3>\n\n\n\n
fusermount -u mountpoint<\/code><\/pre>\n\n\n\n
fusermount -u \/mnt<\/code><\/pre>\n\n\n\n
man sshfs<\/strong><\/code>.<\/p>\n\n\n\n
Other Tutorials<\/h3>\n\n\n\n