You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unix domain sockets return spurious data from getsockopt(SO_PEERCRED) if the peer has already departed, which breaks ssh-agent forwarding for Ubuntu 18.04 #3183
Since upgrading from Ubuntu 16.04 to 18.04, I've noticed that my ssh-agent setup no longer works reliably. For the purposes of debugging, I ran ssh-agent as:
/usr/bin/ssh-agent -s -d -a /tmp/.sshagent.sock
After doing this, I can add keys to the agent, and make exactly one SSH connection. Later, trying to connect to the agent fails with Error connecting to agent: Connection refused. Before the SSH connection (when everything still works), getsockopt appears to behave as expected, returning the pid/uid/gid of the process connecting to the socket:
Some more diagnosis: this bug is caused by initiating an SSH connection that uses agent connection forwarding (ssh -O ForwardAgent=yes). When that connects, it does a test connection to the auth sock:
On the SSH agent side, this one connect/close pair causes the bogus data returned from getsockopt SO_PEERCRED (probably because the peer is already gone by the time of the getsockopt call):
… and after that time the ssh-agent remains alive but is unusable because it closed its end of the socket. This is the only thing that differs between Ubuntu 16.04 and 18.04 -- in earlier versions, the agent would log the error message and drop that specific connection but keep its listening socket open; now (probably for security paranoia) it becomes unusable after one failed connection.
So there's a simple workaround for now: don't use agent forwarding.
changed the title [-]Unix domain sockets sometimes return spurious data from getsockopt(SO_PEERCRED), breaks ssh-agent for Ubuntu 18.04[/-][+]Unix domain sockets return spurious data from getsockopt(SO_PEERCRED) if the peer has already departed, which breaks ssh-agent forwarding for Ubuntu 18.04[/+]on May 11, 2018
Nikita Ushakov (@DarthSpock), I already have the metadata flag on my drvfs mounts, but I don't think it's relevant here anyway because the ssh-agent socket is on a different lxfs mountpoint.
Nikita Ushakov (@DarthSpock) the issue is the new socket close call in the error-handling path of the ssh-agent binary. Why would that be any different from a clean install? I'm pretty sure this is just another minor compatibility bug in the implementation of getsockopt.
Nikita Ushakov (@DarthSpock) I'm doing this on a clean Windows 10 install with a clean Ubuntu 18.04 install. I've since removed the 18.04 install and installed 16.04 where I don't have this issue.
ssh-agent is still broken in the latest ubuntu 18 from the windows store.
$ echo $SSH_AUTH_SOCK
the environment variable is not being set when you close a Ubuntu terminal and reopen it.
I tried the script ADotOut posted but the environment variable is still not being set.
This is annoying for a lot of reasons, especially because you cannot use ssh-keys to log into servers anymore.
Reacted by Christoph Schleßelmann, Manuel Figueroa, Sebastian R, Max Drasbeck, Maxim and UFYLORD
If you're still having this issue, double-check that ssh-agent is actually running and listening to the socket specified by $SSH_AUTH_SOCK. In my .bashrc I have:
if [ ! -S ~/.ssh/ssh_auth_sock ]; then
eval `ssh-agent`
ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock
fi
export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
[ -z "`ssh-add -l | grep 'somekey'`" ] && ssh-add ~/.ssh/somekey
I started getting "Error connecting to agent: Connection refused" after an automatic system reboot, so I checked, and sure enough, the socket in /tmp had somehow persisted across a reboot. Obviously the listening ssh-agent instance didn't, hence the error. I don't yet know if this is a reproducible issue, but if you have similar logic in your .bashrc, it might be the cause of your problem.
I can approve that the script is working fine to reuse one ssh-agent.
But you are calling ssh-add even if the ssh-agent exists.
I would only call it only once:
if! pgrep ssh-agent > /dev/null;then
rm -f /tmp/ssh-auth-sock
eval"$(ssh-agent -s -a /tmp/ssh-auth-sock)"
ssh-add
elseexport SSH_AUTH_SOCK=/tmp/ssh-auth-sock
fi
There is still the issue that you have to enter the password after a restart. Any idea how to solve that?
There's by the way some "official" script in the docs:
Linux:
To start the SSH Agent in the background, run:
eval "$(ssh-agent -s)"
To start the SSH Agent automatically on login, add these lines to your ~/.bash_profile:
if [ -z "$SSH_AUTH_SOCK" ]
then
# Check for a currently running instance of the agent
RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
if [ "$RUNNING_AGENT" = "0" ]
then
# Launch a new instance of the agent
ssh-agent -s &> .ssh/ssh-agent
fi
eval `cat .ssh/ssh-agent`
fi
if ! pgrep ssh-agent > /dev/null; then
rm -f /tmp/ssh-auth-sock
eval "$(ssh-agent -s -a /tmp/ssh-auth-sock)"
fi
export SSH_AUTH_SOCK=/tmp/ssh-auth-sock
if ! ssh-add -l; then
ssh-add
fi
Actually non of the above scripts are working in WSL2 Ubuntu 18.04.
The only good solution for me so far was when I simply added eval "$(ssh-agent -s)" to my .bash_profile.
this snippet I currently use in my .bashrc.local (as I often copy .bashrc and other dot files to servers, splitting into separate .bashrc.local ensures desktop specific settings doesn't hit remote servers)
#setting ssh agent
ssh-add -l &>/dev/null
if [ "$?" == 2 ]; then
test -r ~/.ssh-agent && \
eval "$(<~/.ssh-agent)" >/dev/null
fi
ssh-add -l &>/dev/null
if [ "$?" == 2 ]; then
(umask 066; ssh-agent > ~/.ssh-agent)
eval "$(<~/.ssh-agent)" >/dev/null
fi
ssh-add -l &>/dev/null
if [ "$?" == 1 ]; then
ssh-add .ssh/mykey.id_rsa.key
fi
.bashrc part to include local file:
if [ -f ~/.bashrc.local ]; then
. ~/.bashrc.local
fi
Actually non of the above scripts are working in WSL2 Ubuntu 18.04. The only good solution for me so far was when I simply added eval "$(ssh-agent -s)" to my .bash_profile.
Anyone in 2022 and beyond running wsl2, can confirm this worked for me!
Actually non of the above scripts are working in WSL2 Ubuntu 18.04. The only good solution for me so far was when I simply added eval "$(ssh-agent -s)" to my .bash_profile.
Activity
WSLUser commentedon May 11, 2018
How did you upgrade?
do-release-upgradeor installing the new 18.04 app?0xabu commentedon May 11, 2018
Nikita Ushakov (@DarthSpock),
do-release-upgrade -d0xabu commentedon May 11, 2018
Some more diagnosis: this bug is caused by initiating an SSH connection that uses agent connection forwarding (ssh -O ForwardAgent=yes). When that connects, it does a test connection to the auth sock:
On the SSH agent side, this one connect/close pair causes the bogus data returned from getsockopt SO_PEERCRED (probably because the peer is already gone by the time of the getsockopt call):
… and after that time the ssh-agent remains alive but is unusable because it closed its end of the socket. This is the only thing that differs between Ubuntu 16.04 and 18.04 -- in earlier versions, the agent would log the error message and drop that specific connection but keep its listening socket open; now (probably for security paranoia) it becomes unusable after one failed connection.
So there's a simple workaround for now: don't use agent forwarding.
[-]Unix domain sockets sometimes return spurious data from getsockopt(SO_PEERCRED), breaks ssh-agent for Ubuntu 18.04[/-][+]Unix domain sockets return spurious data from getsockopt(SO_PEERCRED) if the peer has already departed, which breaks ssh-agent forwarding for Ubuntu 18.04[/+]mdkeehan commentedon May 12, 2018
I have noticed my ssh-agent exhibiting similar behaviour after upgrading to 18.04 using
do-release-upgrade -drarenerd commentedon May 15, 2018
I can confirm having the same issue, using Ubuntu 18.04 from the Microsoft Store on a new Windows 10 installation.
rarenerd commentedon May 15, 2018
My config is identical to the example in your link. This does not resolve the issue for me.
0xabu commentedon May 15, 2018
Nikita Ushakov (@DarthSpock), I already have the
metadataflag on my drvfs mounts, but I don't think it's relevant here anyway because the ssh-agent socket is on a different lxfs mountpoint.0xabu commentedon May 15, 2018
Nikita Ushakov (@DarthSpock) the issue is the new socket close call in the error-handling path of the ssh-agent binary. Why would that be any different from a clean install? I'm pretty sure this is just another minor compatibility bug in the implementation of getsockopt.
rarenerd commentedon May 15, 2018
Nikita Ushakov (@DarthSpock) I'm doing this on a clean Windows 10 install with a clean Ubuntu 18.04 install. I've since removed the 18.04 install and installed 16.04 where I don't have this issue.
0xabu commentedon May 15, 2018
It's a bug in WSL.
rarenerd commentedon Jun 5, 2018
Is there anything I can do to help progress this ticket? Anything needed to reproduce the issue?
WSLUser commentedon Jun 5, 2018
Kristian Vlaardingerbroek (@rarenerd) By following CONTRIBUTING.md
d-rez commentedon Jun 26, 2018
I can confirm a clean installation of Ubuntu 18.04 on 1803 build of Windows 10 results in this problem (WSL was installed after updating to 1803)
Brian-Perkins commentedon Jun 28, 2018
There were changes in Insider Build 17704 that may address this.
24 remaining items
petersalomonsen commentedon Aug 9, 2019
Thanks ADotOut . This was really helpful.
HarikrishnanBalagopal commentedon Sep 18, 2019
ssh-agent is still broken in the latest ubuntu 18 from the windows store.
the environment variable is not being set when you close a Ubuntu terminal and reopen it.
I tried the script ADotOut posted but the environment variable is still not being set.
This is annoying for a lot of reasons, especially because you cannot use ssh-keys to log into servers anymore.
tmillican commentedon Oct 7, 2019
Hari (@HarikrishnanBalagopal) :
If you're still having this issue, double-check that
ssh-agentis actually running and listening to the socket specified by$SSH_AUTH_SOCK. In my.bashrcI have:I started getting "Error connecting to agent: Connection refused" after an automatic system reboot, so I checked, and sure enough, the socket in /tmp had somehow persisted across a reboot. Obviously the listening
ssh-agentinstance didn't, hence the error. I don't yet know if this is a reproducible issue, but if you have similar logic in your.bashrc, it might be the cause of your problem.shaunthegeek commentedon Oct 8, 2019
add these code to
~/.bashrcN0xFF commentedon Dec 24, 2019
You create a new process each time you open bash. We can reuse one
ssh-agentinstance:Morgy93 commentedon Jan 15, 2020
This worked fine until I restarted the pc.
Now I'm only left with:
Error connecting to agent: Connection refusedAny idea?
N0xFF commentedon Jan 15, 2020
Thomas Hauschild (@Morgy93) I updated script above and replace socket file lookup with a process.
lmachens commentedon Feb 7, 2020
I can approve that the script is working fine to reuse one ssh-agent.
But you are calling
ssh-addeven if the ssh-agent exists.I would only call it only once:
There is still the issue that you have to enter the password after a restart. Any idea how to solve that?
Morgy93 commentedon Feb 7, 2020
There's by the way some "official" script in the docs:
Source: https://code.visualstudio.com/docs/remote/troubleshooting#_setting-up-the-ssh-agent
pearcec commentedon Mar 26, 2020
Try this:
if ! pgrep ssh-agent > /dev/null; then
rm -f /tmp/ssh-auth-sock
eval "$(ssh-agent -s -a /tmp/ssh-auth-sock)"
fi
export SSH_AUTH_SOCK=/tmp/ssh-auth-sock
if ! ssh-add -l; then
ssh-add
fi
istvan-ujjmeszaros commentedon Oct 6, 2020
Actually non of the above scripts are working in WSL2 Ubuntu 18.04.
The only good solution for me so far was when I simply added
eval "$(ssh-agent -s)"to my.bash_profile.CoolCold commentedon Oct 7, 2020
this snippet I currently use in my .bashrc.local (as I often copy .bashrc and other dot files to servers, splitting into separate .bashrc.local ensures desktop specific settings doesn't hit remote servers)
.bashrc part to include local file:
.ssh/config to learn new keys once used:
all that works fine for me on WSL2 with Ubuntu 20.04
lucas-langa commentedon Jul 27, 2022
Anyone in 2022 and beyond running wsl2, can confirm this worked for me!
Adriano-Dos-Santos commentedon Oct 22, 2022
This worked for me on 2022 using wsl2
JJDSNT commentedon Jan 29, 2025
under WSL:
add to your ~/.bashrc or ~/.zshrc
change github_key for the name of the ssh key you want to use