Strovent SystemsSign in
Reference

Mosh and SSH agent forwarding: why ssh -A does not carry over

Mosh cannot forward your SSH agent. SSH only starts mosh-server and then closes, so ssh -A or ForwardAgent leaves no agent, or a dead socket, in the Mosh shell.

Mosh does not forward your SSH agent. There is no option for it in Mosh 1.4.0, the current release, and adding -A or ForwardAgent yes to the SSH part does not help.

Why

Mosh uses SSH only to log in. According to the mosh manual, it runs mosh-server on the host, then closes the SSH connection and carries the session over UDP. Agent forwarding is a feature of an SSH connection: the agent's requests travel back to your device inside it. Once that connection has closed, nothing carries them. The manual also says Mosh does not support X forwarding or port forwarding, for the same reason.

What you see in the Mosh shell

Run echo $SSH_AUTH_SOCK and ssh-add -l on the host.

  • SSH_AUTH_SOCK is empty, and ssh-add -l says "Could not open a connection to your authentication agent." The SSH login that started mosh-server did not forward an agent. This is the normal result.
  • SSH_AUTH_SOCK is set, but ssh-add -l says "Error connecting to agent: No such file or directory". The login did forward an agent, for example because ForwardAgent yes is in your ~/.ssh/config or you ran mosh --ssh="ssh -A". mosh-server and your shell kept the variable, but sshd removed the socket when that SSH connection ended a moment later.

A shell inside tmux or screen can show the same dead socket from an older SSH login. See SSH agent forwarding not working: Could not open a connection to your authentication agent.

What to do instead

  • Hop from your device, not from the server. If you only need the agent to log in to a second host from the first, connect to the second host directly with a jump host. The client authenticates to both hosts itself, so no agent is forwarded. See Jump hosts (ProxyJump).
  • Use SSH for that task. For a git push or a hop that needs your key, open a normal SSH session with forwarding on, do the task, and close it. Keep Mosh for the long interactive session. See Mosh or SSH?.
  • Use a key that lives on the host. For repeat jobs such as pulling from one repository, a separate key stored on the host, limited to that job (for example a deploy key), avoids forwarding altogether.
  • Borrow the socket from a parallel SSH login. While an ssh -A session to the same host stays open, its socket exists. In that session run ln -sf "$SSH_AUTH_SOCK" ~/.ssh/agent.sock, then in the Mosh shell export SSH_AUTH_SOCK=~/.ssh/agent.sock. It stops working as soon as the SSH session drops, which on a mobile network is exactly what Mosh was meant to survive.

Before you forward an agent at all

Anyone who can open the forwarded socket on the host, such as its root, can use your keys while it exists, though they cannot copy them. See The agent and agent forwarding.

Sources

Spotted a mistake? Email [email protected].