Github: I have to make a new SSH key after every use - github

I have a school and a personal Github, so I made an SSH key for my personal account and linked it up. It always works only once, then gives me
Push Failed
Git#github.com: Permission denied (publickey). Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
It works fine again after I delete the old SSH and make a new one, but this is obviously a huge hassle. I am on Windows 10, using Git Bash, and IDE is IntelliJ.
Any ideas? Thanks.

I solved this problem in the following way:
1.Create a file named config in the path $USER_HOME/.ssh/, then add following content.
# For school account
Host school_github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_school
# For personal account
Host personal_github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal
2.Run following command using git bash:
Change your school github repository configuration
git remote rm origin
git remote add origin git#school_github:your_github_username/your_repo_name.git
You can hava a try.

You don't need to create SSH keys for each git service just upload the same public key to both personal and school accounts and it will work.
SSH public key is tied to you local account you have generate SSH keys only if you are using different local(machine) account or different machine

Seems to me like when you try to authenticate, you are probably not using your keys.
If you run the command
ssh-add -l
You (should) see a list of all your keys. Each time you start a new session, make sure to start up the agent and add your keys.
eval `ssh-agent`
ssh-add ~/.ssh/id_rsa

Related

Trying to setup dual repo SSH config file

I've successfully had my MacBook working with my own Github repo for years. Recently, I tried to setup my computer to access both my own Github repo and the Github repo for my work account. I read many articles and threads and learned that this is supposed work with the SSH config file. I've tried many things but just can't get it to work.
In my .ssh directory I have these files:
config
id_ed25519
id_ed25519.pub
id.rsa
id_rsa.pub
The first private/public key is for my work account and the second is for my personal account.
Here's the config file I [naively] created:
Host https://github.com/employer
HostName github.com
IdentitiesOnly yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519
Host RobertWerner-MWTech.github.com
HostName github.com
IdentitiesOnly yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
I've obscured the name of my employer but that http://... address is what my DevOps colleague told me to add. For my personal account, the first part - RobertWerner-MWTech is my Github account name. I don't know if RobertWerner-MWTech.github.com is the correct host name.
Anyhow, it seems that the config file has no actual effect. Even with it in place, I can't access both repos at the same time. The best I can achieve now is to run ssh-agent whenever I want to use a specific repo, like this:
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
OR
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_rsa
While this does allow me to switch back & forth between the two repos, it would be fantastic if I could actually get the config file to work as it's supposed to.
Any guidance on how to resolve this would be much appreciated!
Robert

GitHub using the wrong SSH key

I have two GitHub accounts setup with ssh keys, one personal and one enterprise.
I have an ssh config file as such:
# * GitHub CKO SSH Key
Host github-enterprise
HostName github.com
AddKeysToAgent yes
UseKeychain yes
User git
IdentityFile ~/.ssh/id_ed25519_github
# * GitHub Personal SSH Key
Host github-personal
HostName github.com
AddKeysToAgent yes
UseKeychain yes
User git
IdentityFile ~/.ssh/gh_mervinhemaraju_ed25519
Both keys were created seperately and attached to the respective account.
The weird issue is that I was using this for like a month, and it was working. Today, when i logged in, i committed some work on my personal repo and when i tried to do a remote push (which was working for this repo previously), i got and user permission denied.
I then performed an ssh test on both ssh keys and the results was as such:
ssh -T ssh -T git#github-personal
Hi mervin-hemaraju-enterprise! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git#github-enterprise
Hi mervin-hemaraju-cko! You've successfully authenticated, but GitHub does not provide shell access.
The personal key test is wrong. It should've been Hi mervinhemaraju! You've successfully authenticated, but GitHub does not provide shell access. since mervinhemaraju is my personal account, but is instead referring to the enterprise one.
I am on MacOs. Can someone please help ?
Add IdentitiesOnly yes for the entries in the ssh config. This will prevent the SSH Agent from trying all the keys it knows of and only use the ones specified in the config file.
Specifies that ssh should only use the identity keys configured in the ssh_config files, even if ssh-agent offers more identities.
https://www.ssh.com/academy/ssh/config
I have had a similar issue, and what I did was create a local git config that explicitly specifies which SSH key to use.
My ~/.ssh/config file specifies to use my "work" ssh key, since that's most common on my work computer.
I have something like this in my ~/.ssh/config:
Host *
PreferredAuthentications publickey
IdentityFile ~/.ssh/my-work-ssh-key
ServerAliveInterval 60
In my "personal" projects, I configure Git to ignore my ~/.ssh/config file, and I set the identity file to my "personal" SSH key.
Something like this:
git config --local core.sshCommand "ssh -i ~/.ssh/my-personal-ssh-key -F /dev/null"
That puts the following in .git/config:
[core]
sshCommand = ssh -i ~/.ssh/my-personal-key -F /dev/null

Add SSH key for both github and bitbucket in single PC

Is it possible to use both the github and bitbucket repo in personal computer.
if not, Let me know any other possibilities.
Thanks in advance.
Complete Guide to Add SSH keys for both github and bitbucket in single PC
if you already have one ssh key then you must have 2 files public(id_rsa.pub) and Private key(id_rsa) in the .ssh folder You can Skip the Step 2
Step 1. Prepare your default identity it Required for Both Account Before Doing Step 2 and Step 3
Determine your Git clone URL.
$ git remote -v
origin git#bitbucket.org:teamsinspace/bitbucketspacestation.git (fetch)
origin git#bitbucket.org:teamsinspace/bitbucketspacestation.git (push)
Update the remote URL with your Bitbucket username by replacing git#bitbucket.org with <username>#bitbucket.org.
For this step and the ones that follow, enter your username in place of .
$ git remote set-url origin <username>#bitbucket.org:teamsinspace/bitbucketspacestation.git
Step 2 : create the 1st ssh key with default names (id_rsa)
$ssh-keygen
You will see the following text:
Generating public/private RSA key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):
you can input the password to secure your file or you can press enter for all the steps and then it will create one ssh key.
By default, the system adds keys for all identities to the /Users/<username>/.ssh directory
You can check the already existing ssh key by following command
$ ls ~/.ssh
id_rsa id_rsa.pub
Two files should be there one with the name of the key(id_rsa) Private key second with the same (ir_rsa.pub) public key.
you can open make it visible by ctrl + h in Home Directory
then it will be shown
Second, Create your known hosts file if you not have already
To create your known hosts files-
touch known_hosts
STEP 3 : Sethup the Multiple account ssh
3.1 : Create 2nd Key With default name
You might be using one SSH key pair for working on your company's internal projects, now but you might be need a diffrent key for accessing a client's servers so you can create the key
By run the same command ssh-keygen again it will create the 2nd ssh key files with the name of [ id_rsa2 and id_rsa2.pub ]
3.2 : Create the 2nd Key with Custom Name
ssh-keygen -f NAME_OF_YOUR_KEY
$ ssh-keygen -f work_key it will create then 2 files
[ work_key, work_key.pub ]
if want to run the one key at a time then you can use this command before connecting to that repo for pull and push .
ssh-add -K ~/.ssh/YOUR_KEY_FILE
ssh-add -K ~/.ssh/work_key
Step 4 : Setup both two Accounts at the same time
You want to Setup both two Accounts at the same time then you will have to use the following command
Third Create your config file
To create your config file
touch config or open directly into any text editor,
Here is Example for One Github and one Bitbucket account
Host : name of your ssh setting you can give custom name
HostName : github domain or bitbucket domain
IdentityFile : path of your ssh file
#Work account
Host pers
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
UserKnownHostsFile ~/.ssh/known_hosts
IdentitiesOnly yes
#Personal account
Host work
HostName bitbucket.org
User git
IdentityFile ~/.ssh/work_key
UserKnownHostsFile ~/.ssh/known_hosts
IdentitiesOnly yes
Step 5 : Add your public keys to the Bitbucket or github account
For Bitbucket
For Github [Go to Setting of your Account -> SSH and GPG key]
Step 6 : Add your public permanently
ssh HOST(What we written in config file )
like
ssh work
if you got this error make sure you did Step 4 Correctly.
git#bitbucket.org: Permission denied (publickey).
or can be use this way as well
Now you can use
git clone `git#pers/project.git`
git clone `git#work/project.git`
Step 7 : auotmaticly pick by domain name we are requiesting to
Managing SSH keys can become cumbersome as soon as you need to use a second key. Traditionally, you would use ssh-add to store your keys to ssh-agent, typing in the password for each key. The problem is that you would need to do this every time you restart your computer, which can quickly become tedious.
if you have only two account and one in the github and one in the bitbucket you can use the following approch to auotmaticly pick by domain name we are requiesting to
#Work account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
UserKnownHostsFile ~/.ssh/known_hosts
IdentitiesOnly yes
#Personal account
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/work_key
UserKnownHostsFile ~/.ssh/known_hosts
IdentitiesOnly yes
Here is a link to further guidance on it
https://www.freecodecamp.org/news/the-ultimate-guide-to-ssh-setting-up-ssh-keys/
https://support.atlassian.com/bitbucket-cloud/docs/set-up-additional-ssh-keys/
Yes it is. You tell git where the remote code lives from a per-repository configuration file. You can even push to GitHub and Bitbucket from the same repository if you want to.
See here for more details:
http://blog.lckymn.com/2013/03/11/git-push-to-pull-from-both-github-and-bitbucket/
One important piece will be connecting to each separately with SSH.
Your SSH keys should live in $HOME/.ssh and can contain any number of keys. The default name for an SSH key is id_rsa (or similar, depending on the protocol used to create it).
Try doing:
ls $HOME/.ssh
... to see what's in there.
I do what you are asking about myself and for me that brings up something like:
github-personal
github-personal.pub
bitbucket-work
bitbucket-work.pub
known_hosts
Where known_hosts is a file that contains a list of the servers I connect to and the public keys associated with them. The other files that end in .pub are my own public keys and the rest are my private keys.
You get your GitHub and Bitbucket keys into there by following their appropriate tutorials:
GitHub: https://help.github.com/articles/generating-ssh-keys/
Bitbucket: https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git
Yes, it is possible to use both the github and bitbucket repo in personal computer.
You can setup multiple SSH profiles.
First Generate your SSH keys To generate the first key-from your root folder
$ cd ~/.ssh
$ ssh-keygen -f work_key,
then enter a passphrase of your choice.
To generate the second key
$ ssh-keygen -f personal_key, then enter a passphrase of your choice.
Second, Create your known hosts file
To create your known hosts files-
touch known_hosts
Third Create your config file
To create your config file
touch config, then it would look something like this
#Work account
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_rsa
UserKnownHostsFile ~/.ssh/known_hosts
IdentitiesOnly yes
#Personal account
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_rsa2
UserKnownHostsFile ~/.ssh/known_hosts
IdentitiesOnly yes
Then add your SSH key to bitbucket. Go to bitbucket, settings, then SSH keys
pbcopy < id_rsa.pub (to copy the key) and paste it in bitbucket
Definitely YES. You can generate ssh key pair first, see How to generate, then go to .ssh directory, copy the content of id_rsa.pub, and paste into your github or bitbucket ssh setting area.
I make use of Sourcefree Desktop Application and My Gitbash prompt
to push my project to Bitbucket and Github repository simultaneously
below is what i did
right click to open bash shell of the folder you want to push
First you need to create key, i will advice make use of the same ssh key
create key
ssh-keygen -t rsa -C "your_email#example.com"
go to the folder directory
right click and click on git bash
then run the below command one after the other
git init
git add .
git commit -m "first"
open your sourcefree desktop app
click on + to open your local
repo file
create repo on bitbucket
then click repo icon
then click on repo icon to add
name: originbit
url: "repo link you created"
For your bitbucket you have to make use of both
public ssh key and private ssh key if you want to make use of
both public and private repository
To make use of ssh key earlier created
go to TOOLS icon of sourcefree App
Click on load tab
then resave it with .ppk extension
save the same key as private key and public key
in the same directory
dont input passhrase yet to avoid difficulties
just save it.
so that it can be loaded to pageant icon
to create new key
go to TOOLS icon of sourcefree
then click on create ssh key
change the bottom number to 2048
but if you are unable to change it
first create a new key then drag your mouse around the colum provided
in a zigzag way or any form once created.
change the 1048 to 2048 so that it can be accepted at bitbucket ssh
then recreate a new key
then save it as public key and private key
add key to the Pageant (check your desktop notification for this Pageant key icon)
add both private ssh key and ssh public key
Goto your bitbucket account seetings
and add the content of the two ssh keys
name them differently
go back to the bash shell
git status
git add -A
git pull originbit master
git commit -m "modified code"
git push originbit master
create repo on github
Open Sourcefree desktop app
copy the repo url
then click repo icon
then click on repo icon to add
then push on Sourcefree

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.

Github wrong username

I have setup with guthub with a previous github account on this computer called myaccount when i run this command
ssh -T git#github.com
Hi myaccount! You've successfully authenticated, but GitHub does not provide shell access.
Ok but i have now setup a new github account called samaccount but i cant run this command i get this error.
git push -u origin master
i get this error
ERROR: Permission to samueleast/Samuel-East-S3-Audio-Playlist-Player.git denied to myaccount.
I dont want it to use myaccount it should be using samaccount !!!
i have followed all the steps here http://help.github.com/mac-set-up-git/
setup brand new ssh key.
and also run this command many times
git config --global user.name "samaccount"
but it still uses myaccount its sooo annoying where am i going wrong???
GitHub recognizes you as myaccount because SSH is using a key that corresponds to a public key that has been added to the myaccount account on the GitHub website.
If you want to switch entirely to using the sameueleast account instead of myaccount, you can do the following:
Login to GitHub's website as myaccount
Go to "Account Settings"
Go to "SSH Public Keys"
Delete your public key from that list
Logout
Login to GitHub's website as samueleast
Go to "Account Settings" -> "SSH Public Keys"
Select "Add another public key" and paste in the contents of your public key. You public key will (probably) be called id_rsa.pub or id_dsa.pub and be in your .ssh directory.
On the other hand, if you want to be able to use either myaccount or samueleast when pushing to GitHub, you will need to create aliases for git#github.com in ~/.ssh/config, such as:
Host github-myaccount
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_dsa-myaccount.pub
Host github-samueleast
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_dsa-samueleast.pub
That assumes you've generated a second SSH keypair and given them names as above. Then you need to make sure that you are using the appropriate alias in the URLs of your git remotes. For example, if you want to change your origin remote so that pushing to origin means "pushing to origin as samueleast", you could do:
git remote set-url origin \
git#github-samueleast:samueleast/Samuel-East-S3-Audio-Playlist-Player.git