How to Automatically Mount a Remote Directory to Your Local Directory

If you’re a regular Linux desktop user like me, you’ve likely encountered a scenario where you wanted to automatically mount a remote server directory to a local directory of your choosing. In my case, Obsidian serves as my personal note-taking app, allowing me to compile and organize my knowledge, and I store these notes on my home network server.

Since the app’s notebooks reside on the server, I found myself having to mount it to my local desktop directory every time I logged into my local machine. This process was cumbersome, and I wanted to automate it to save time.

Here’s what I did, and you might find this method useful for your own systems if you’re looking to automatically mount a remote directory to your local directory.

First, generate an SSH key pair (if you haven’t already). By default, this will create a pair of keys: a private key (usually ~/.ssh/id_rsa) and a public key (usually ~/.ssh/id_rsa.pub).

ssh-keygen -t rsa -b 4096

Next, copy your public SSH key to the remote server. This step is crucial for setting up key-based authentication.

ssh-copy-id user@remote-server-address

Open your shell’s profile script in a text editor (my favorite one is vim). This might be .bash_profile, .bashrc, .zshrc, etc., depending on your shell and setup. For Bash, you can use:

vim ~/.bashrc

At the end of the file, add the SSHFS command. To avoid having to manually enter the password, it’s recommended to set up SSH key-based authentication. Here’s the command you can add:

sshfs user@192.168.0.xx:/path/to/your/remote/dir /path/to/your/local/dir

After adding the command, save the file and exit the editor.

To apply the changes without logging out and back in, you can source your profile script:

source ~/.bashrc

Congratulations! From this point on, you no longer have to manually mount your remote directory to your local directory.

Leave a Reply