Auto Unload Key From SSH - command-line

Why did I get the following error (on the same machine where the github connection had been properly set up before)? Meaning, it worked the previous day (e.g. I could push, pull, etc. with no problem) but didn't the next day.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
When I did ssh-add:
$ ssh-add -l
The agent has no identities.
Is it possible that a key, which was previously loaded into SSH, is automatically unloaded?
The following fixed the issue (for reference):
ssh-add ~/.ssh/github_rsa
Enter passphrase for /Users/admin/.ssh/github_rsa:
Identity added: /Users/admin/.ssh/github_rsa (/Users/admin/.ssh/github_rsa)

The latest Github for (Mac|Windows) include a credential helper (as explained in "How to use git with gnome-keyring integration").
That means, if you need to enter a password (because your private ssh key is passphrase protected), that will be stored by git for the duration of the session.
But at the next session, you will need to enter the passphrase again (once).

On the mac, identities that you add don't usually go away unless you explicitly remove them (ssh-add -d), the machine is rebooted, or you log out and then log back in again.

Related

Gitkraken cannot use local SSH agent

I have recently changed my GitHub username and when I try to push with Authentication > Use local SSH agent selected, GitKraken push fails with following error:
Push Failed
Configured SSH key is invalid.
Please confirm that it is properly associated with your git provider.
Unselecting the option reveals proper private/public key pair located at
~/.ssh/id_rsa
~/.ssh/id_rsa.pub
and allows the push to be done.
I can also push from inside Eclipse, and via command line without error. The URL of the repository point to my new username.
So far I have checked several things:
ssh -T github.com
gives permission denied
Fingerprint of id_rsa.pub matches to a key existing in Github account.
The following command suggests that ssh tool tries rsa private key, but is rejected:
$ ssh -vT github.com
debug1: Offering RSA public key: /home/<user>/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/<user>/.ssh/id_dsa
debug1: Trying private key: /home/<user>/.ssh/id_ecdsa
debug1: Trying private key: /home/<user>/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
What is causing ssh -vT to fail in this case, and why doesn't it prevent git push/pull but fails when GitKraken tries pushing?
For me this was caused by an upgrade to OSX Sierra. I had to execute
ssh-add -k ~/.ssh/id_rsa
(you might have to replace that with the path of the key you are using). The command adds the ssh key to the git agent. For more information see the GitHub Help Page about adding SSH Keys.
You might have to add the command to some sort of setup file which is run at boot, because it seems like the ssh agent doesn't remember the key (at least for me).
For people who will come here in the future, my case was that pull/push working on vsc but gitkraken was giving me the message : no auth sock variable.
I went into file > preferences > SSH > unmarked use local SSH agent then it automatically found my id_rsa, ida_rsa.pub (you can browse and locate them manually if needed)
and now everything working fine.
OS: Arch,
gitkraken version: 7.7.2
What helped me resolve the issue was to go to GitKraken preferences and connect both Bitbucket and Github and authorize them.
After that tick the box in "general" that says use local ssh agent. (might be
If you have already set-up your local SSH agent, and your Git in terminal works, but Gitkraken does not, this might help:
First, start gitkraken from your terminal, instead of a desktop shortcut:
Open your favorite terminal
Run gitkraken
See if "Pull" works, or any other authenticated operation
It it works, you need to edit your Gitkraken desktop shortcut:
In my scenario, this is where my desktop shortcut is located, so: sudo vim /usr/share/applications/GitKraken.desktop
Change Exec=gitkraken %f to Exec=env SSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/ssh-agent.socket" gitkraken %f

How could I setup a permanent connection to github from server with SSH key?

I followed the guide from https://help.github.com/articles/generating-ssh-keys/ to generate a ssh key used to connect to github from my server.
The problem is if I close the shell and login the server again, I have to repeat below steps to reconnect to github, otherwise the accessing will be denied:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
How can I setup a permanent connection with github?
Since you are logging on to the command-line (presumably using SSH) all applications that you run, including ssh-agent, stop when you log out.
In my opinion your best bet is to have your CentOS machine start ssh-agent for you each time you log in, then manually use ssh-add to add your key. This can be done by modifying your shell's login script. If you are using bash, this file is called ~/.bash_profile. If you are using another shell, refer to its documentation and choose an appropriate file.
Edit one of these files and add eval "$(ssh-agent -s)" at the end.
Now, each time you log in ssh-agent should automatically start, and you can optionally run ssh-add (since you are using a file that ssh-add looks for by default you don't need to pass the file to the command) to add your key, then do Git stuff.
It sounds like you want to automate the adding of the key as well. This isn't something that I would choose to do since I like adding keys to be explicit, but because you don't have a passphrase on your SSH key you should be able to add ssh-add to the same file we modified above (after we start the agent, of course) and have your key added automatically.
Thanks #Chris
I did the following and it worked:-
Step1:- .bash_profile
Step2:- enter image description here
Step3:-source .bash_profile
Step4:- enter image description here

Why do I need to run `ssh-add` in my Powershell profile?

In my Microsoft.PowerShell_profile.ps1 document, I've had to add ssh-add ~/.ssh/github_rsa following the poshgit examples in order for it to connect to my GitHub repos.
# Load posh-git example profile
. 'C:\tools\poshgit\dahlbyk-posh-git-8aecd99\profile.example.ps1'
ssh-add ~/.ssh/github_rsa
If I don't have that in my profile, I Github gives me permissions errors when I try to connect.
If I do it manually, it will work for the entire duration of my desktop session, but as soon as I reboot my computer, I need to re-run the command.
Why doesn't poshgit and ssh-add remember the rsa that I've added? It seems wrong to have to re-add it every time.
It's because your rsa key is not the default name ( id_rsa ) so you either need to use ssh-add (which adds it to a running service that remembers the key decrypted with your passphrase) or just add an entry into your ~\.shh\config
~\.ssh\config (create or edit):
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_rsa
Or, if github is the only thing you use ssh keys for, just rename the key to id_rsa and then git (well ssh.exe) will find it for you automatically AND poshgit will ssh-add it for you (to handle passphrases).

Permission denied (publickey). fatal: The remote end hung up unexpectedly for git pull

I am using Ubuntu 12.04
Kannan#Ubuntu:~/a/git pull
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
How to resolve this issue. Please do let me know
I think this is due to ssh problem
Make sure whether your key is in the .ssh folder, if yes, then do the following one
Open ~/.bashrc & add
eval `ssh-agent -s`
ssh-add ~/.ssh/your_respective_key
and do bash on terminal and do git pull.I hope it will work
You need to make sure the public key you've registered with Github is the one for the user doing the Git pull.
The Github guide for setting up keys is well laid out, and they have a linux version:
https://help.github.com/articles/generating-ssh-keys#platform-linux
Make sure it's definitely the correct user's public key, and you have not included any whitespace at the end of the key - the key finishes at the end of your email address.
ssh-rsa 8f97gs89dfng746c74nyc7n478cb6ts746tb8746tbv9846cyb78
sjfghsfg43534639fb09bg0g098243hfjc0478tjwygjw85jywcyhwj9y598
dfhghuh3546ntnfgskjvgiu34b6t3== you#example.com
That is the end of your key ----^

Permission denied (public key) during fetch from GitHub with Jenkins user on Ubuntu

Here is my setup:
Jenkins is running on my linux machine as 'jenkins' user.
I have generated a ssh key-pair as described in Linux - Setup Git, for the 'jenkins' user.
When I sudo su jenkins and try ssh -vT git#github.com, I am always asked my passphrase, but I am always eventually authenicated. (the verbose option shows which key is used, among others).
I could clone my repo from GitHub using jenkins:
Thusly:
jenkins#alpm:~/jobs/test git/workspace$ git pull
Enter passphrase for key '/var/lib/jenkins/.ssh/id*_rsa':
Already up-to-date.
Up to this point I have followed the instructions to the letter. The problem is that the Jenkins job fails with the following error:
status code 128:
stdout:
stderr: Permission denied (publickey).
fatal: The remote end hung up unexpectedly
This is same error as I get when I typo the passphrase (but of course, Jenkins does not ask me for the passphrase). The following pages:
GitHub - SSH Issues
Using SSH Agent Forwarding
indicate to me that ssh-agent could help remember the passphrase, which it does when I am using my own user, but not the jenkins id. Note that while running as my normal user yields:
echo "$SSH_AUTH_SOCK"
/tmp/keyring-nQlwf9/ssh
While running the same command as my 'jenkins' yields nothing (not even permission denied)
My understanding of the problem is that the passphrase is not remembered.
Do you have any idea?
Shall I start a ssh-agent or key ring manager for the jenkins user? How?
Or is ssh forwarding suitable when forwarding to the same machine?
Any brighter idea?
ps: I never sudo gitted, I always used jenkins or my user account (as mentioned in this SO post - Ubuntu/GitHub SSH Key Issue)
Since nobody wrote the answer from the comments for several months, I will quickly do so.
There are 2 possible problems/solutions:
id_rsa created with wrong user
Create id_rsa as the jenkins user (see hudson cannot fetch from git repository)
Leave passphrase empty
To summarise what must be done on the Jenkins server:
# 1. Create the folder containing the SSH keys if necessary
if [ ! -e ~jenkins/.ssh ]; then mkdir ~jenkins/.ssh; fi
cd ~jenkins/.ssh/
# 2. Create the SSH pair of keys
# The comment will help to identify the SSH key on target systems
ssh-keygen -C "jenkins" -f ~jenkins/.ssh/id_rsa -P ""
# 3. Assign the proper access rights
chown -R jenkins ~jenkins/.ssh/
chmod 700 ~jenkins/.ssh
chmod 600 ~jenkins/.ssh/*
Remember:
Please keep the default "id_rsa" name when generating the keys, as other such as "id_rsa_jenkins" won't work, even if correctly set up.
Do not use a passphrase for your key
Check that the public key (id_rsa.pub) has been uploaded on the git server (GitHub, Bitbucket, etc). Once done, test your SSH key by running: ssh -vvv git#github.com (change address according to your git server)
I got around this problem by simply leaving the passphrase empty when creating the keys.
I would add that if you created the keys by hand, they might still be owned by you and not readable by jenkins, try:
sudo chown jenkins -R /var/lib/jenkins/.ssh/*
To check are the following:
if the right public key (id_rsa.pub) is uploaded to the git-server.
jenkins user will access to github -> to CHECK if the right private key (id_rsa) is copied to /var/lib/jenkins/.ssh/
if the known_hosts file is created inside ~/.ssh folder. Try ssh -vvv git#github.com to see debug logs. If thing goes well, github.com will be added to known_hosts.
if the permission of id_rsa is set to 755 (chmod 755 id_rsa)
After all checks -> try ssh -vvv git#github.com
Dont try to do config in jenkins until ssh works!
If you are running jenkins as a service in windows, you need to verify the user running the service. If you created the keys using the user "MACHINENAME\user", change the service so the user running it can match
For Mac users, the issue can be solved by removing the existing keys and creating new Private and Public Keys by following these steps:
1.Remove all Public and Private keys located at /Users/Username/.ssh
2.Remove all the credentials saved under the Credentials tab in Jenkins.
3.Remove the existing Public SSH keys defined in the Github Repository Settings.
4.Create new SSH keys (private and public: id_rsa and id_rsa.pub) by following the steps from https://confluence.atlassian.com/bitbucketserver/creating-ssh-keys-776639788.html#CreatingSSHkeys-CreatinganSSHkeyonLinux&MacOSX
5.Set the newly created public SSH key (id_rsa.pub) in Github or an equivalent Repository Settings.
6.In Jenkins,create new credentials by adding the private SSH key(id_rsa) for your Github username.
7.The Error should be removed now.
keys need to generated from jenkins user.
sudo su jenkins
ssh-keygen
once the key is generated, it should be added as ssh key in bitbucket or github.