Trying to setup dual repo SSH config file - github

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

Related

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

shh passphrase doesn't let me upload to github pages

I'm trying to upload to github pages. The passphrase that I use for the repository where the code is doesn't work when I run npm run deploy.
Could it be that I have to add github.io to the config file?
In /Users/sara/.ssh/config
#user1 account
Host github.com-sara-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
IdentitiesOnly yes
#user2 account
Host github.com-SaraLance
HostName github.com
User git
IdentityFile ~/.ssh/SaraLance
IdentitiesOnly yes
How can I do this?
Could it be that I have to add github.io to the config file?
No, you are not pushing directly to github.io. You are pushing to a regular GitHub repository, for which you have set a publication branch, making it eligible to be a project Github page.
As long as your passphrase is loaded by the SSH agent, npm run should not be blocked.
You need to make sure you are using the right SSH URL:
github.com-SaraLance:<user>/<repo>
# or
github.com-sara-work:<user>/<repo>
If npm is using a regular git#github.com:<user>/<repo> URL, none of your keys would be considered anyway.

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

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

How can I push git with multiple accounts on one machine

I want to use two different github accounts to separate my school and my personal things. So I found the methods here,(https://youtu.be/fnSRBRiQIU8)
I successfully added two ssh keys on each account and this is my ~/.ssh/config file
# Default account
Host github.com
User git
IdentityFile ~/.ssh/id_rsa
# Second account
Host github.com-SECONDARY
User git
IdentityFile ~/.ssh/id_rsa_secondary
I tried to push it but did not have luck.
In the youtube video and its written instruction describe,
1. git remote add origin git#github.com:SECONDARY/testing.git
2. git push -u origin master
I thought it is old way, so I did new way like this
3. git remote add origin https://github.com/SECONDARYusername/testing.git
4. git push -u origin master
Then I got this error message
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Aren't line 1 and 3 equivalent? Is there other method that I can use two accounts on one machine?
Thanks!
The basic technique is to configure SSH with two new virtual (ie. fake) host names. They both point at github.com, but one uses one key and the other use the other. Your ssh config has a problem, it doesn't specify what the real host is.
# Second account
Host github.com-SECONDARY
User git
IdentityFile ~/.ssh/id_rsa_secondary
That says "when you try to connect to github.com-SECONDARY, use the ssh key in ~/.ssh/id_rsa_secondary". But github.com-SECONDARY isn't real. You need to tell ssh that by adding a HostName line.
# Second account
Host github.com-SECONDARY
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_secondary
That's the first problem.
The second is you're not using that hostname in your remotes.
git remote add origin git#github.com:SECONDARY/testing.git
^^^^^^^^^^
That's the hostname part. It should be github.com-SECONDARY like so.
git remote add origin git#github.com-SECONDARY:SECONDARY/testing.git
Then ssh will know to use your special config for the github.com-SECONDARY virtual host.
There's a better info on this in the Q&A for "Multiple GitHub Accounts & SSH Config".

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).