SSH over servers
Image: Funtap/Shutterstock

SSHFS (SSH File System) makes it possible to mount remote filesystems via an SSH connection. SSHFS uses SFTP (SSH File Transfer Protocol) to mount a remote directory on a local machine such that the connection between the client and server is encrypted. Because of that, SSHFS can be used as a more secure solution for traditional FTP.

SEE: 5 programming languages network architects should learn (free PDF) (TechRepublic)

I want to walk you through the installation and usage of SSHFS on Rocky Linux.

What you’ll need

To make this work, you’ll need a running instance of Rocky Linux, a client machine that can use SSH, and a user with sudo privileges.

Let’s make some magic.

How to install SSHFS

The first thing we must do is install SSHFS. Log into your Rocky Linux server and first upgrade it with the command:

sudo dnf upgrade -y

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

sudo dnf install fuse-sshfs -y

Next, on the client machine, install SSHFS. If you’re u

How to create mountable directories

First, let’s create a directory on the server that will then be mounted on the client machine. To create the directory on the server, issue the command:

sudo mkdir /srv/data

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

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

If more than one user needs access to this directory, you’d need to create a new group, add the users to the group, and then give the group access to the directory. Let’s say you want to create a group named editorial and give them access to that 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 (one that will be used to mount the remote directory into) with the command:

mkdir ~/data_mount

How to mount the remote directory to the local machine

It’s time to mount the remote directory to the local machine. On the client issue 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’ll be prompted for the user’s password. Upon successful authentication, you’ll get your prompt back and the mount is ready to be used.

How to make the mount permanent

This is a bit tricky because you first must set up SSH key authentication. To do that, 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 domain of the remote server. Once the key is copied, test the connection with:

ssh USER@SERVER

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

You can now create an fstab entry for the SSHFS connection. Open that file (on the client) for editing with:

sudo nano /etc/fstab

At the bottom of that 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 ID of the user, and USER_GID is the group ID of the user. You can locate the IDs by issuing the command:

id

Save and close the file. Test the mount with:

mount -a

You should receive no errors.

The caveat to this is that the remote directory won’t be automatically mounted at boot. This is because it requires a network connection to be loaded first. However, once you log into the machine, you can simply issue the command:

mount -a

It’s a bit of a hassle, but I’ve yet to come up with a solid solution to get this to work without using passwordless ssh key authentication (which we don’t want to use because of security issues).

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

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Subscribe to the Cybersecurity Insider Newsletter

Strengthen your organization's IT security defenses by keeping abreast of the latest cybersecurity news, solutions, and best practices. Delivered every Monday, Tuesday and Thursday

Subscribe to the Cybersecurity Insider Newsletter

Strengthen your organization's IT security defenses by keeping abreast of the latest cybersecurity news, solutions, and best practices. Delivered every Monday, Tuesday and Thursday