DEV Community

Cover image for Error connecting to agent when in screen

Posted on

Error connecting to agent when in screen


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.


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"

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

# get into screen
$ echo "$SSH_AUTH_SOCK"

$ 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
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!


The cover image is obtained from TechRepublic.

Discussion (0)