How to Use SSHFS to Mount Remote Directories on Rocky Linux

Learn how to mount a remote directory on a Rocky Linux server to a local directory using SSHFS.

Image: Funtap/Shutterstock

SSHFS (SSH File System) allows remote file systems to be mounted over an SSH connection. SSHFS uses SFTP (SSH File Transfer Protocol) to mount a remote directory on a local machine so that the connection between client and server is encrypted. For this reason, SSHFS can be used as a more secure solution for traditional FTP.

TO SEE: 5 Programming Languages ​​Network Architects Should Learn (Free PDF) (TechRepublic)

I want to walk you through installing and using SSHFS on Rocky Linux.

What you will need

For this to work, you will need a running instance of Rocky Linux, a client machine that can use SSH, and a user with sudo privileges.

Let’s do some magic.

How to install SSHFS

The first thing we need to do is install SSHFS. Connect to your Rocky Linux server and upgrade it first with the command:

sudo dnf upgrade -y

Once the upgrade is complete, reboot (but only if the kernel was upgraded) then install SSHFS with the command:

sudo dnf install fuse-sshfs -y

Next, on the client machine, install SSHFS. If you are you

How to create mountable directories

Let’s start by creating a directory on the server which will then be mounted on the client machine. To create the directory on the server, run the command:

sudo mkdir /srv/data

Next, modify the permissions of the new directory so that the necessary user can access it with the command:

sudo chown -R $USER.$USER /srv/data

If multiple users need access to this directory, you must create a new group, add the users to the group, and then grant the group access to the directory. Suppose you want to create a group named editorial and give them access to this new directory. First, create the group with:

sudo groupadd editorial

sudo usermod -aG editorial $USER

sudo chgrp -R editorial /srv/data

On the local machine, create a new directory (the one that will be used to mount the remote directory) with the command:

mkdir ~/data_mount

How to mount remote directory on local machine

It’s time to mount the remote directory on the local machine. On the client, run the command:

sshfs USER@SERVER:/srv/data ~/data_mount

Where USER is the user on the remote machine and SERVER is the IP address or domain of the remote server and you will be prompted for the user’s password. After successful authentication, you will receive your prompt and the mount is ready to use.

How to make the mount permanent

It’s a bit tricky because you have to configure SSH key authentication first. To do this, on the client machine create the SSH key with:

ssh-keygen -t rsa

Once the key is generated, copy it to the remote server with:

ssh-copy-id USER@SERVER

Where USER is the username and SERVER is either the IP address or the domain of the remote server. Once the key has been copied, test the connection with:

ssh USER@SERVER

Where USER is the username and SERVER is either the IP address or the domain of the remote server. You should be prompted for the SSH key authentication password. Exit the connection and test it again. This time you should not be prompted because the key has been stored in your keychain.

You can now create an fstab entry for the SSHFS connection. Open this file (on the client) to edit it with:

sudo nano /etc/fstab

At the bottom of this file, add the following line:

USER@SERVER:/srv/data /home/USER/data_mount   fuse.sshfs  delay_connect,_netdev,user,idmap=user,transform_symlinks,identityfile=/home/jack/.ssh/id_rsa,default_permissions,uid=USER_ID,gid=USER_GID   0 0

Where USER is the username, SERVER is either the IP address or domain of the remote server, USER_ID is the user ID, and USER_GID is the user’s group ID. You can locate the IDs by running the command:

id

Save and close the file. Test the mount with:

mount -a

You should not receive any errors.

The caveat to this is that the remote directory will not be automatically mounted on boot. Indeed, it requires a network connection to be loaded first. However, once connected to the machine, you can simply issue the command:

mount -a

It’s a bit complicated, but I haven’t yet found a solid solution to make it work without using passwordless ssh key authentication (which we don’t want to use due to security concerns).

Anyway, that’s all there is to mounting a remote directory with SSHFS on Rocky Linux.

Subscribe to TechRepublic How to make technology work on YouTube for all the latest tech tips for professionals from Jack Wallen.

Comments are closed.