Reputation: 4097
I have two following commands which I manually run when I connect on my VPS:
eval ssh-agent $SHELL
ssh-add /home/duvdevan/.ssh/id_rsa
I tried adding them into a ssh.sh
and run it within that directory using:
./ssh.sh
But nothing happends.
I'm not that bash
-savvy so any help is appreciated.
Upvotes: 4
Views: 3358
Reputation: 124648
Instead of running, you need to source the script:
. ./ssh.sh
Otherwise, the environment variables set by the eval
command will not be visible in your current shell, and so it cannot know where to find the running ssh agent.
To give a bit more background, here's how this works:
ssh-agent
command starts an ssh agent, and prints to stdout the environment variables you need to set to connect to the agent. The output is formatted as commands to execute. For a test, you can just run this command and see what it printseval
command executes the commands printed by ssh-agent
. As mentioned earlier, these are commands to set environment variables. After these are executed, the ssh commands you will run in this shell will know where to find the agentssh-add
command is able to find the agent, thanks to the environment variables set earlier./ssh.sh
, the variables are set inside the process of that script, and longer available after the script is finishedssh.sh
script using .
, the commands inside will be executed in the current shell, and therefore the environment variables are still set, and so your ssh related command can find the agentUpvotes: 7