How to access SSH file systems on remote servers from linux

botond published 2019/10/29, k - 20:36 time

Content

 

Introductory

If you often work on remote file systems, it is a good idea to mount them on your own file system so that you do not have to log on to another machine or connect to any server every time, but use that storage as if it were your local directory. This can come in handy, for example, if you need to develop a website, teamwork on a shared hosting, or even want to share a directory between two home Linux computers, etc.

We have already talked about how to access SSH (SFTP) -based file systems on remote Linux servers from a Windows computer, and in this tutorial we will do the same on a Linux machine SSHFS access and mount as a local directory.

 

 

basic Conditions

We use two Linux machines for this compilation, in this case I'm running two Debian machines. One will be the server we need to have at least one SSH access and the other is the client where we need to have root privileges. The latter should obviously not be a problem when it comes to our home machine. The server can be any remote machine, or even another computer on our home network. The point is to be able to access the machine from the outside via SSH. If you haven't already been able to do this, install it on the server machine openssh-server package:

apt-get install openssh-server

So far, everything is available: the two hosts are provided, including the network connection, and the client can access the server via SSH.

Then install it on the client machine to use SSHFS sshfs package (as root):

apt-get install sshfs

Create a mount directory

Create a mount directory on the client machine to which you will mount the remote SSHFS share. For example:

mkdir /helyi/csatolasi/konyvtar

Create it according to your needs, where we can access remote content.

 

Mount a remote file system

There are two ways to mount a remote SSHFS file system. In a temporary or permanent way.

Temporarily use the file system with the sshfs command

Az sshfs linux command to temporarily mount SSHFS filesystems that will disappear after the client machine is restarted:

sudo sshfs -o allow_other <felhasználó>@<szerver>:</abszolút/elérési/út> </helyi/csatolási/pont>

By default, only sshfs mount can be used as root, with the following parameters:

  • -o switch to add more options to the command. Here it is allow_other option allows other users to access the link.
  • This is followed by the remote host username, hostname (or IP addresses), followed immediately by a colon, followed by the absolute path of the subdirectory you want to access on the remote machine.
  • And finally, the local mount directory that we created earlier - where the contents of the remote directory are displayed.

In this case, make sure that the "username @ hostname: / path" section must be in one and not include a space, because this is one of the parameters of sshfs.

After issuing this command, we will be prompted for the destination user password.

If you want to mount the SSHFS file system as a regular user, open the /etc/fuse.conf file:

nano /etc/fuse.conf

And remove the comment from the "user_allow_other" line. In this case, keep in mind that SSHFS can still be unmounted only by root!

Use a public key to mount the file system without a password

If you do not want to type your password every time you mount a SSHFS file system, SSH login without password using public key and you will not be prompted for your password.

 

 

Termination

SSHFS mount as root only umount command we can terminate:

umount /helyi/csatolási/könyvtár

Permanently mount a file system using fstab

If you do not want to run the sshfs command as root to mount the remote file system after all machines have been restarted, / etc / fstab file in the appropriate setting.

Important!
It is important to note here that this method is only for Set up public key access will work after! Otherwise, the remote SSHFS file system will not be mounted during the boot process.

Let's see fstab setting with my specific example, then I describe the meaning of the parameters in detail. So my own fstab line:

botond@asztali:/mnt/drive-d /mnt/asztali_d      fuse.sshfs      defaults,_netdev,user,idmap=user,identityfile=/home/botond/.ssh/id_rsa,allow_other,default_permissions,uid=botond,gid=botond 0 0

In this example, my desktop computer / Mnt / drive-d I attach my library to my local laptop / Mnt / asztali_d directory where the other parameters are:

  • fuse.sshfs: File system type
  • Mount options, listed separately (comma separated list, no spaces!):
    • defaults: Provides default mount options plus the rest.
    • _netdev: This option indicates that it is not a block device but a network device. This will mount it only after the network part has been built.
    • user: Allows any user to mount the file system.
    • idmap = user: Sshfs automatically converts local IDs to remote users. This will make the user IDs of the mounted file system compatible with the local file system.
    • identityfile: This is where you need to enter your regular user private key to authenticate to the remote machine (or, if the local root public key is installed on the remote machine, you can omit it).
    • allow_other: It also gives access to "sharing" to other users, so not just the mount user.
    • default_permissions: Allows the kernel to verify the actual permissions on the remote file system.
    • uid = xxx and gid = xxx: Here you can configure which local user should share SSHFS on.
  • 0 (first 0): Dumping, that is, whether to back up this file system. In most cases, it is set to 0
  • 0 (Second 0): The fsck tool checks the file systems in that order. Values ​​of 0 are not checked. In the case of a remote file system, of course, you don't need to check the integrity of the file system, it's the job of the remote machine that maintains the file system.

So these are the fstab options that automatically reboot the remote SSHFS file system after the client restarts.

Of course, there are other settings, such as here you can find out, so everyone can spice up the fstab's settings to their liking to work properly. For me, the above works perfectly, so I use this.

 

Conclusion

Using the SSHFS file system is a useful way to conveniently access your stuff on each machine, or even use a live server backup directory structure to conveniently back up your home computer data.