78

I use the zsh and the oh-my-zsh. When I use the github and want to use the key to upload.

I always find the

#git push
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Because I have not add the key

#ssh-add -l
Could not open a connection to your authentication agent.

so I have to start the ssh-agent and add the key when I want to push or pull

#ssh-agent zsh
#ssh-add ~/.ssh/id_rsa

How can I add these command in the script,so I don't need type the commands?

wcc526
  • 3,915
  • 2
  • 31
  • 29

1 Answers1

148

open .zshrc in a text editor:

vim ~/.zshrc

Add ssh-agent to the plugins list and save:

plugins=(git ssh-agent)

You may want to immediately reload your .zshrc settings:

source ~/.zshrc
Ilan Frumer
  • 32,059
  • 8
  • 70
  • 84
  • 4
    It can run ssh-agent now,but how can it "ssh-add -l" automatically? – wcc526 Feb 23 '14 at 11:09
  • 15
    You can use this: `zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github` (found here: https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/ssh-agent/ssh-agent.plugin.zsh) – Olivier 'Ölbaum' Scherler Jul 24 '14 at 09:13
  • 8
    The readme is a little more clear. https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/ssh-agent – Cobman Mar 25 '20 at 16:49
  • The problem is that the passphrase isn't stored once you've logged out. Apparently on a Mac, you can add `ssh-add -K` to your `.zshrc` file to use the keychain but this doesn't work on Linux. – Jake Rayson Nov 13 '22 at 19:05
  • 1
    Unfortunately this doesn't work with powerlevel10k. – Anutrix Mar 13 '23 at 16:02
  • For me it works with Powerlevel10K, however, I get an error message the first time I start a new shell since the plugin creates output. This only happens once though. – m00am Mar 30 '23 at 14:16
  • There are some powerlevel10k specifics here: https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/ssh-agent#powerline-10k-specific-settings – Natetronn May 15 '23 at 03:42