37

When I update my ssh config file so I can switch my current github account, the changes wont work properly unless I restart iTerm. I'm working on a script to automate the github account switch and I'd like to have the script reload the config settings in the updated config file. How can I achieve this?

2
  • 4
    How, do you switch accounts? Do you use ssh-agent? The ssh_config is read for every single invocation of ssh.
    – Jakuje
    Nov 23, 2016 at 8:44
  • Can you give us more detail about the config file? Also, could you define different hosts for the different github accounts, and avoid the reloading problem?
    – JasKerr
    Nov 24, 2016 at 2:45

3 Answers 3

21

In my case, I finally discovered that the issue wasn't the config file (ssh -vvv -F /dev/null -i /some/path/some_other_key and even moving the old keys in ~/.ssh/ elsewhere, nonetheless still managed to magick the old key out of nowhere), but rather the ssh agent. I had to clear it with ssh-add -D.

man ssh_config clarifies that -i on ssh should take precedence over the ~/.ssh/config file; so if you're doing this and it's still not working, some undocumented higher priority power is butting in.

2
  • 8
    ssh-add -D did the trick
    – Paul
    Nov 28, 2019 at 4:41
  • 1
    that's it! - I've been looking for why my changes never reflected... ssh-add -D refreshed it.
    – JasonGenX
    Jun 17, 2022 at 22:45
6

You may want to look at the Atlassian documentation on using multiple identities. A case like the one I think you're describing - switching accounts - may be best handled with an SSH config file that accommodates multiple accounts simultaneously instead of scripting.

They provide the following example for the config file at ~/.ssh/config:

# Default GitHub user
Host github.com
 HostName github.com
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/personalid

# Work user account
Host bitbucket.org
 HostName bitbucket.org
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/workid
1
  • 1
    Note you could also use different Host nicknames for the same HostName, with different IdentityFiles for each: Host github-charlie, Host github-sam
    – jpaugh
    Feb 5, 2018 at 14:56
0

While I was looking for a way to 'refresh' the file I realised what I was actually looking for was a way to auto complete the command,

Refreshing was not necessary as @Jakuje above mentions

For those interested the auto complete script is:

complete -o default -o nospace -W "$(grep "^Host" $HOME/.ssh/config | cut -d" " -f2)" scp sftp ssh

Which I found here.

Add the above script to .bash_profile and then run source .bash_profile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .