In order to run ssh-agent
I have to use:
eval $(ssh-agent)
Why is it necessary to eval
the output of ssh-agent
? Why can't I just run it?
In order to run ssh-agent
I have to use:
eval $(ssh-agent)
Why is it necessary to eval
the output of ssh-agent
? Why can't I just run it?
ssh-agent
outputs the environment variables you need to have to connect to it:
shadur@proteus:~$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-492P67qzMeGA/agent.7948; export SSH_AUTH_SOCK;
SSH_AGENT_PID=7949; export SSH_AGENT_PID;
echo Agent pid 7949;
shadur@proteus:~$
By calling eval
you immediately load those variables into your environment.
As to why ssh-agent
can't do that itself... Note the word choice. Not "won't", "can't". In Unix, a process can only modify its own environment variables, and pass them on to children. It can not modify its parent process' environment because the system won't allow it. This is pretty basic security design.
You could get around the eval
by using ssh-agent utility
where utility
is your login shell, your window manager or whatever other thing needs to have the SSH environment variables set. This is also mentioned in the manual.
eval
by using ssh-agent utility
where utility
is your login shell, your window manager or whatever other thing needs to have the SSH environment variables set. This is also mentioned in the manual. No external utility can ever set variables in the calling environment.
– Kusalananda
Mar 15 '17 at 21:36
man
page, for a start... – jasonwryan Mar 15 '17 at 21:17ssh-agent
that is "designed this way", it's unix/linux, becausessh-agent
runs in a child process of the shell. Child processes can't modify parent processes. But a function can: because it runs in the current process. So you could write a function:do_set_ssh_agent() { eval
ssh-agent; }
and that could be run simply as:$ do_set_ssh_agent
. But "programs" aren't (typically) installed as "functions" in linux/unix; instead, programs are installed as files, which, as mentioned, run in a child process. (Sourcing scripts is an exception, but ssh-agent is binary.) – michael Mar 16 '17 at 15:55$()
instead). And BTW, quotes are important --do_set_ssh_agent() { eval "$(ssh-agent)"; }
is a bit more correct, inasmuch as it means that you don't have the output ofssh-agent
being string-split on contents of IFS into individual words, with those words then being formed back into a single string to be processed byeval
. – Charles Duffy Mar 16 '17 at 16:14IFS=';'
, or otherwise an IFS value containing a non-whitespace character present inssh-agent
's output. – Charles Duffy Mar 16 '17 at 16:17