1

I'm trying to override the User for certain hosts. I came across this gist and though I could improve on it by removing the step of creating a new Host alias for each domain-account needed.

~/.ssh/config

Host github.com bitbucket.org bitbucket.com
    User git

Host *
  Protocol 2
  UseKeychain yes
  AddKeysToAgent yes
  IdentitiesOnly yes
  IdentityFile ~/.ssh/%r@%h_id_rsa

Unfortunately unlike the Hostname, Port and other properties, when setting User git it is not changing the value.

$ ssh -Tv tarranjones@github.com

GitHub is still trying to authenticate the user tarranjones instead of git

debug1: Authenticating to github.com:22 as 'tarranjones'

Is there a way around this?

  • @steeldriver, I should have known this. This didn't fix the problem but it was useful thanks. – TarranJones Feb 14 '17 at 13:19
2

You can't override a user specified on the command line. Only give a default for when none is specified (if there's no default given in your ~/.ssh/config, your login as returned by whoami will be used). So it will work as desired if you use: ssh -Tv github.com

Note you can actually set Host to any value, not just one that corresponds to an actual hostname, like so:

Host gh
    Hostname gh
    User git

Then you could use ssh -Tv gh.

  • For my required result the user needs to be set so the correct ssh key is used. – TarranJones Feb 14 '17 at 13:19
0

Host * may as well be deleted:

IdentityFile %d/.ssh/%r@%h_id_rsa
Protocol 2
UseKeychain yes
AddKeysToAgent yes
IdentitiesOnly yes

Host github.com bitbucket.org bitbucket.com
  User git

Don't specify the username on the command line as that will override the user name in the configuration.

0

The %r and %h in the IdentityFile (~/.ssh/%r@%h_id_rsa) will always be the same as the remote user and remote host used to authenticate.

In the example i've changed Hostname

Host github.com bitbucket.org bitbucket.com
  IdentityFile ~/.ssh/%r@%h_id_rsa
  Hostname newhostname.com

Test

$ ssh -Tv tarranjones@github.com

Result

debug1: Authenticating to github.com:22 as 'tarranjones'
debug1: Offering RSA public key: ~/.ssh/tarranjones@newhostname.com_id_rsa

In this example i've changed User

Host github.com bitbucket.org bitbucket.com
  IdentityFile ~/.ssh/%r@%h_id_rsa
  User git

Test

$ ssh -Tv tarranjones@github.com

Now the remote host name wont change but if it did change this would just mean that the corresponding IdentityFile name would then be wrong.

debug1: Authenticating to github.com:22 as 'git'
debug1: Offering RSA public key: ~/.ssh/git@github.com_id_rsa

Using percent_expand for the remote host name %h is not going to work for naming the identity file. This will always need to be hard coded if you want it to differ from the remote host name used to authenticate.

The best i could come up with is this.

#Set Git User Domains
Host *-github.com *-bitbucket.org *-bitbucket.com
  User git

#IdentityFile
Host tarranjones-*
  IdentityFile ~/.ssh/tarranjones@%h_id_rsa

Host otherusername-*
  IdentityFile ~/.ssh/otherusername@%h_id_rsa

#Hostnames
Host *-github.com
  Hostname github.com

Host *-bitbucket.com *-bitbucket.org
  Hostname bitbucket.org

Host *
  Protocol 2
  UseKeychain yes
  AddKeysToAgent yes
  IdentitiesOnly yes

Usage

$ ssh -Tv tarranjones-github.com

Result

debug1: Authenticating to github.com:22 as 'git'
debug1: Offering RSA public key: ~/.ssh/tarranjones@github.com_id_rsa

Please see Updated Gist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.