I have installed keychain.
sudo apt-get install keychain
If you're running bash you need to add a few commands to your .bash_profile If you do not have a .bash_profile create one in your home folder. Add these lines:
### START-Keychain ###
# Let re-use ssh-agent and/or gpg-agent between logins
/usr/bin/keychain $HOME/.ssh/id_dsa
source $HOME/.keychain/$HOSTNAME-sh
### End-Keychain ###
At the start of a work day I will login. When I open a terminal, I will be prompted once for my passphrase. For all other new terminals and connections I will not be asked for my passphrase again.
- In this example I implied that we work as root. If you don't, apply
sudo su
or sudo
when needed.
- Your system may use different init system than Systemd, but Cron is pretty universal.
You can simply use /etc/fstab
to pre-define your mount options and whatnot.
Example:
USERNAME@HOSTNAME_OR_IP:/REMOTE/DIRECTORY /LOCAL/MOUNTPOINT fuse.sshfs defaults,password_stdin,_netdev 0 0
Keep in mind default mount options are far from perfect.
For example: reconnect is important. see: https://github.com/libfuse/sshfs/issues/101
An example with these options (taken from the Github issue):
sshfs#user@storage.cz:/content/ /mnt/srv fuse password_stdin,defaults,user,allow_other,reconnect,delay_connect,ConnectTimeout=5,ServerAliveInterval=5,IdentityFile=/root/.ssh/id_rsa_storage 0 0
Once that's done, you need a simple script with this sole content, such as:
#!/bin/bash
echo "passwordgoeshere" | mount /mnt/srv
Let's save it under your root user, so an example: /root/mount_sshfs.sh
Now you need to make it executable: chown +x /root/mount_sshfs.sh
Now all you need is just cron
or systemd
to execute this on mount.
With cron, a simple entry like this works:
@reboot /root/mount_sshfs.sh
With Systemd:
1) You have to create the script. See just above.
2) You have to create a new Systemd service script.
An example would be: /etc/systemd/system/mount_network.sh
3) The contents of the file:
[Unit]
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/root/mount_sshfs.sh
[Install]
WantedBy=default.target
Notes:
- I did not test the Systemd method as I try not to rely on it as much as possible. It's personal dislike/dislike/hate. :)
- With Cron, you may need to add a "sleep" to the script, so it doesn't try to run the script "too early", ie.: before internet/network comes up.
Source: https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux
Best Answer
Are you starting
sshfs
from your shell, or from a boot-time script?If you are starting it from your shell, and provided you have a running ssh-agent, it should just work:
If you are starting it from a location that does not already have SSH_AUTH_SOCK in its environment, and assuming the ssh-agent is already active and has the key, you can simply set SSH_AUTH_SOCK to the correct value, which you can either get by running
ps uxeww | grep SSH_AUTH_SOCK
and massaging the output, or simply looking at whichever/tmp/ssh-*/*
socket you can access and using that! I assume you can write a simple shell script to do that!