I don’t know about you, but I struggle with this a lot - until now!! 🦾
It’s worth mentioning that this solution:
- works on Linux operating systems
- resolves a specific issue related to this error message - that being SSH keys are not present in Linux when accessing a git repository (unless you know that this is your issue, first run through this Github troubleshooting page and come back if the problem persists)
- works with Systemd
The Problem#
As alluded to above, the issue under the spotlight here is when you either don’t have an SSH agent running or it doesn’t have the relevant SSH keys loaded in order to authenticate with the server hosting your repository.
If you running a big Desktop Environment like KDE, Gnome, Cinnamon and the like, this is handled for you. But for those that use Window Managers like dwm
, xmonad
, hyprland
and the like, this may not be the case.
How the issue presents itself in the terminal#
1
2
3
4
5
| Connection closed by x.y.z
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
|
The Solution#
We will solve this issue by running an SSH agent user service that constantly adds and manages SSH keys while you’re logged in. This will fix access to repositories as well as for applications such as Obsidian (and its Obsidian Git plugin).
- Open a terminal and start an SSH agent with:
- Ensure that the agent is running by executing:
1
2
3
4
5
| echo $SSH_AUTH_SOCK
# Example expected output
#
# /tmp/ssh-megq74th/agent.12345
|
- Add your SSH keys to the agent
1
2
3
4
5
| ssh-add
# Example expected output
#
# Identity added: /home/user/.ssh/id_rsa (user@localhost)
|
- Verify that the SSH key has been added by listing out the loaded keys:
1
2
3
4
5
| ssh-add -l
# Example expected output
#
# 1234 SHA256:<KEY_ID> user@localhost (RSA)
|
Congratulations, your SSH agent is running and works. Now we need to get all of this working in the background, upon login.
- Create a user service config in
~/.config/systemd/user/ssh-agent.service
with the following content:
[Unit]
Description=SSH key agent
[Service]
Type=simple
Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK
[Install]
WantedBy=default.target
- Add the following environment variable export to you shell’s runtime config i.e.
.bash_profile
for Bash, .zshrc
for Zsh etc.
1
| export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"
|
- Add the following git config pair to
~/.ssh/config
Host *
AddKeysToAgent yes
- Enable the service to run at login
1
| systemctl --user enable ssh-agent
|
- Run the service now (so that you don’t need to log out)
1
| systemctl --user start ssh-agent
|
And all should be working now (hopefully) 😆