I need to re-run eval(ssh-agent) and ssh-add on every boot
Asked Answered
M

4

16

I'm wondering if I could get some help. I recently reinstalled my OS and I'm running into a strange issue that I've never run into before. I'm following the Github steps to add ssh agent

Basically, every time I log in I need to run

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_personal

when I reboot my computer.

When I try to run git clone X I'm getting

Cloning into 'X'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

but after I run the above 2 commands, I can clone just fine

Note: issue is also with Gitlab. Sorry, I should have been explicit

Maquis answered 7/10, 2021 at 21:22 Comment(3)
See all answers for https://mcmap.net/q/12877/-start-ssh-agent-on-login-closed/7976758 Found in stackoverflow.com/search?q=%5Bssh-agent%5D+every+loginVicissitude
Personally I use gpg-agent in ssh-agent mode. It stores keys forever in a file and allows to configure how often it re-asks passphrase; mine asks every 12 hours. I start gpg-agent before XWin so it's available for all terminals and programs inside my X session.Vicissitude
MacOS starts an ssh-agent via launchd at the time you log on, before firing up any windows, so that the windows have the ssh environment variables pre-set. You can get Linux software that will do the same thing, but I have never used it. I use something much more like the method @Vicissitude describes.Moon
H
23

It sounds like you're running on Linux, and your login is probably "bash".

If so, I would consider adding these two lines to the bottom of your ~/.bashrc login file:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_personal

You might also consider trying this:

Hannan answered 7/10, 2021 at 21:45 Comment(7)
Yeah, I'm using Linux but I'm running on zsh. I could probably place those two lines at the bottom of my .zshrc (and I will) but I'm also not sure why this is happening.... I've been using linux for quite a while now and I've never seen anything like thisMaquis
This doesn't really have that much to do with Linux, but everything to do with GitHub and the way its authentication interacts with a running ssh-agent.Hannan
Sorry, I should habe been more clear and said this, but I'm running into the same issue with gitlab :/Maquis
For bash, since you only want to do this at initial login, you might consider using .bash_profile. I'm not sure about the zsh setup. Another common trick is to first check whether there's an existing agent available, and if not, start one, but that has some potential races, so I dislike this one myself.Moon
@IanQ: 1) In case you didn't notice my update, please consider this: How to Set ssh-agent to Run Automatically. 2) phd made an interesting suggestion about using gpg-agent (part of GNUPrivacy Guard) as an alternative to ssh-agent. You can learn more here: gnupg.org, 3) As you probably already know, the zsh user init file is ~.zshrc. 'Hope that helps; please post back what you learn!Hannan
Yeap! Sorry for the silence - work got crazy. Will test out when I'm freeMaquis
If you're curious about what's the difference between .bashrc and .bash_profile. Go to cloudzy.com/knowledge-base/…Pleione
P
8

Try this, I found it somewhere online. Add this to your ~/.bashrc file

    if [ -z "$SSH_AUTH_SOCK" ] ; then
    eval `ssh-agent -s`
    ssh-add ~/.ssh/<NAME OF YOUR PRIVATE KEY>
    fi

This assumes your private key is located under .ssh. Otherwise, use the adequate path

Pleione answered 30/5, 2022 at 18:45 Comment(1)
best answer so far as this will not run new ssh-agent on every new terminal you openOde
M
1

Adding my own answer:

I think the issue was that when I made the first key, I changed the name from the default. I.e I changed from

~/.ssh/id_id_edX -> ~/.ssh/id_personal

After removing the key and regenerating things it all worked out. Why? IDK

Maquis answered 8/10, 2021 at 22:34 Comment(1)
Ah. That is because ssh needs to be given the file and path to that id_id_edX key, using the -i option (or use IdentityFile as a Host directive in ssh config). Basically SSH has a list of built-in SSH key "names" it will automatically look for and submit to hosts. You can also create a config that Also, your instinct here to create specific keys for specific use-cases or sites, is a good one (if you don't mind tracking which key goes to which role). It should never be too painful to have to re-generate a key if you suspected it was leaked/lost.Blythe
P
1

I saw that you mentioned using zsh for your shell. You can add ssh-agent to your plugins and it’ll handle it for you.

Perisarc answered 1/12, 2022 at 5:4 Comment(2)
Any guide please?Dominate
please elaborate moreLombroso

© 2022 - 2024 — McMap. All rights reserved.