SSHFS mounting without password with SSH keys

First we need to generate public and private keys on our host.
When prompted for passphrase just hit enter to go without it

nevillegroup@shell:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nevillegroup/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/nevillegroup/.ssh/id_rsa.
Your public key has been saved in /home/nevillegroup/.ssh/id_rsa.pub.

We need to add our public key into authorized_keys file and upload it to the remote host into the .ssh directory.

nevillegroup@shell:~$ cat .ssh/id_rsa.pub > authorized_keys

Now we can create a bash script for mounting the remote directory

!/bin/bash

USERID="YOUR_UID"
USER="example"
HOST="example.com"
MOUNT_DIR="/home/example/examplecom_sshfs"

# create dir if it doesn't exists
if [ ! -d "$MOUNT_DIR" ]; then
umount $MOUNT_DIR &> /dev/null
mkdir $MOUNT_DIR
fi

# to check your uid use command `id`
sshfs -C -o uid=$USERID,ssh_command='ssh -i ~/.ssh/id_rsa',workaround=rename $USER@$HOST:. $MOUNT_DIR

Add executable flag to the file and you ready to mount with the script

nevillegroup@shell:~$ chmod +x bin/sshfs_examplecom
nevillegroup@shell:~$ ./bin/sshfs_examplecom
nevillegroup@shell:~$ ls /home/example/examplecom_sshfs
dev/ git/ logs/ sub/ web/
Advertisements

Author: Michal Zuber

Full stack developer, biker and rollerblader. Owner and developer at https://nevilleweb.sk/ Co-founded http://neville.sk/ Blog at https://michalzuber.wordpress.com/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s