DEV Community

Cover image for Error connecting to agent when in screen
suntong
suntong

Posted on

Error connecting to agent when in screen

Situation

I got an "Error connecting to agent" error when attaching to an old screen session the other day, but didn't find a good article on how to fix it, when searching with both phase of "Error connecting to agent" and "screen". So I decided to write about it so the next person knows how to fix such problem in the future.

Solution

The solution is quite simple, actually, and here is exactly how to do it.

  • Before attaching to the old screen session, do echo "$SSH_AUTH_SOCK".
  • After attaching to the old screen session, do echo "$SSH_AUTH_SOCK" again. You should see that the values are different in the two cases.
  • Fix SSH_AUTH_SOCK with the correct value, which is the one before attaching to screen.

That should fix the Error connecting to agent problem.

Once again,

# after logged into remote host, 
$ echo "$SSH_AUTH_SOCK"
/tmp/ssh-2HsGhzu77S/agent.125711

# attaching to the old `screen` session
screen -r old_session

# get into screen
$ echo "$SSH_AUTH_SOCK"
/tmp/ssh-vosa42Ualz/agent.22985

$ ssh-add -l
Error connecting to agent: No such file or directory

# The SSH_AUTH_SOCK is no longer there. Point to the new one
SSH_AUTH_SOCK=/tmp/ssh-2HsGhzu77S/agent.125711
Enter fullscreen mode Exit fullscreen mode

Now, verify with another ssh-add -l, which shall now output what it was before attaching to the old screen session.

All set!

Credit

The cover image is obtained from TechRepublic.

Discussion (0)