Software & AppsOperating SystemLinux

How To Auto Mount SSH Ubuntu Directory with SSHFS

Ubuntu 20

In this article, we will provide a detailed guide on how to auto mount an SSH Ubuntu directory using SSHFS. SSHFS (SSH File System) is a file system client that allows you to mount remote directories on your local machine over an SSH connection.

What is SSHFS?

SSHFS is a handy tool that uses SFTP (SSH File Transfer Protocol) to mount remote systems to your local machine. It provides a secure way to share files and interact with remote file systems as if they were local.

Prerequisites

Before we start, ensure that you have SSHFS installed on your Ubuntu system. If not, you can install it using the following command:

sudo apt-get install sshfs

Now, let’s dive into the different methods you can use to auto mount an SSH directory using SSHFS.

Method 1: Using a Script

The simplest way to auto mount using SSHFS is by creating a script. Here’s how you can do it:

  1. Create a script file: Create a script file named mountssh.sh and open it in a text editor. The content of the file should be as follows:
#!/bin/bash
sshfs user@192.xx.xx.xx.xx:/dir/dir /home/username/mount/xxx

In this command, user is your username on the remote system, 192.xx.xx.xx.xx is the IP address of the remote system, /dir/dir is the directory on the remote system you want to mount, and /home/username/mount/xxx is the local directory where you want to mount the remote directory.

  1. Make the script executable: Run the following command to make the script executable:
chmod +x mountssh.sh

Now, you can double-click the script file to execute it and mount the drive.

Method 2: Using fstab

The fstab (file systems table) file is a system configuration file on Ubuntu systems. It contains information about the system’s disk partitions and other data sources.

  1. Edit the fstab file: Open the /etc/fstab file with sudo privileges:
sudo nano /etc/fstab
  1. Add the SSHFS mount: Add the following line to the file:
user@host:/remote/folder /mount/point fuse.sshfs noauto,x-systemd.automount,_netdev,IdentityFile=/home/name/.ssh/id_rsa,allow_other,reconnect 0 0

In this line, user is your username on the remote system, host is the hostname or IP address of the remote system, /remote/folder is the directory on the remote system you want to mount, and /mount/point is the local directory where you want to mount the remote directory. The IdentityFile option specifies the path to your private key file.

  1. Mount the drive: Save the file and run the following command to mount the drive:
sudo mount -a

This method will automatically mount the drive when needed.

Method 3: Using autofs

autofs is a program that automatically mounts directories on an as-needed basis. It’s a great solution for mounting remote file systems.

  1. Install autofs: Run the following command to install autofs:
sudo apt-get install autofs
  1. Configure autofs: Open the /etc/auto.master file in a text editor with sudo privileges and add the following line:
/mnt/location /etc/auto.sshfs --timeout=30 --ghost

Next, create a new file named /etc/auto.sshfs and add the following line:

sshname:/mountpath -fstype=fuse,rw,allow_other,IdentityFile=/root/.ssh/id_rsa,default_permissions,uid=YOURIDHERE,gid=YOURIDHERE :sshfs\#user@user.example.org:/

Replace the placeholders with the appropriate values.

  1. Restart the autofs service: Save the files and restart the autofs service:
sudo service autofs restart

This method will automatically mount the SSH directory when accessed and handle disconnections and reconnections.

Method 4: Using a Custom Launcher

You can create a custom launcher on your desktop to execute the SSHFS command and mount the drive.

  1. Create a launcher: Create a .desktop file on your desktop and open it in a text editor. Add the following content:
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_US]=nautilus
Name[en_US]=Connect to xy
Exec=sshfs user@192.xx.xx.xx.xx:/dir/dir /home/username/mount/xxx
Comment[en_US]=Connect to xy via ssh
Name=Connect to xy
Comment=Connect to xy via ssh
Icon=nautilus

Customize the Exec line with your specific SSHFS command.

  1. Make the file executable: Run the following command to make the file executable:
chmod +x connect.desktop

Now, you can double-click the launcher to execute the SSHFS command and mount the drive.

In conclusion, SSHFS is a powerful tool that provides a secure and efficient way to interact with remote file systems. The methods described above provide different ways to auto mount using SSHFS, allowing you to choose the one that best suits your needs.

What is the purpose of SSHFS?

SSHFS allows you to mount remote directories on your local machine over an SSH connection, providing a secure way to share files and interact with remote file systems as if they were local.

How do I install SSHFS on Ubuntu?

You can install SSHFS on Ubuntu by running the command sudo apt-get install sshfs in the terminal.

How do I create a script to auto mount an SSH directory using SSHFS?

To create a script, you can create a file with a .sh extension, such as mountssh.sh, and include the SSHFS command inside the script. Don’t forget to make the script executable using the chmod +x command.

How do I edit the fstab file to auto mount an SSH directory using SSHFS?

You can edit the /etc/fstab file using a text editor with sudo privileges. Add a line to the file that specifies the SSHFS mount, including the remote system details and local mount point. Save the file and use the sudo mount -a command to mount the drive.

What is autofs and how do I use it to auto mount an SSH directory using SSHFS?

Autofs is a program that automatically mounts directories on an as-needed basis. To use it, you need to install autofs using the command sudo apt-get install autofs. Then, configure autofs by editing the /etc/auto.master and /etc/auto.sshfs files. Finally, restart the autofs service with sudo service autofs restart.

How do I create a custom launcher to auto mount an SSH directory using SSHFS?

To create a custom launcher, create a .desktop file and specify the SSHFS command in the Exec line. Make the file executable using chmod +x command. Double-clicking the launcher will execute the SSHFS command and mount the drive.

Leave a Comment

Your email address will not be published. Required fields are marked *