Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My understanding of the way ~/.ssh/config works is that each 'Host ' line takes effect for any host matching after that point in the config file.

I have a number of personal servers and work servers that I need to connect to. I'm trying to do something like the following:

# General Settings
ControlMaster auto
ControlPath   ~/.ssh/controlmaster/%r@%h:%p
ForwardAgent  yes
ForwardX11    yes
GSSAPIAuthentication no
PubkeyAuthentication yes

# Personal Servers
Host *
User harleypig
IdentityFile ~/.ssh/personal_id_rsa

Host host1
Hostname host1.com

Host host2
Hostname host2.com

# Work Servers
Host *
User alan.young
IdentityFile ~/.ssh/work_id_rsa

Host work1
Hostname work1.companyserver.com

Host work2
Hostname work2.companyserver.com

Host *
User devuser

Host dev1
Hostname dev1.companyserver.com

Host dev2
Hostname dev2.companyserver.com

The docs seem to indicate that host1 and host2 should use 'personal_id_rsa' and the user harleypig. work1, work2, dev1 and dev2 should use 'work_id_rsa' and the first two should be the user 'alan.young' and dev1 and dev2 should be the user 'devuser'

However, this is not happening. Whatever 'Host *' I put first is what all of the following hosts try to connect with. Am I misunderstanding or missing something?

share|improve this question
    
Thank you, between both of these answers I was able to get my connections working. –  harleypig Jul 13 '11 at 22:06
add comment

2 Answers 2

up vote 3 down vote accepted

From the ssh_config manual:

Since the first obtained value for each parameter is used, more host-specific declarations should be given near the beginning of the file, and general defaults at the end.

So in your example, all hosts will use User harleypig and IdentityFile ~/.ssh/personal_id_rsa.

Think of Host directives with wildcards as fallbacks: use the following settings only if they haven't been set yet. You need to write something like this:

Host host1
Hostname host1.com
Host host2
Hostname host2.com
Host host*
User harleypig
IdentityFile ~/.ssh/personal_id_rsa

You can put multiple patterns on a Host line if a given set of host aliases can't be matched with wildcards, e.g. Host host* more* outlier.

share|improve this answer
add comment

You are definitely doing it wrong.

  • You should always put Host * as the last entry.
  • You can not have multiple Host * entries

If your work machines have a name format which you can generalize to target just the work machines, for e.g: machine1.work.com, host.work.com, fileserver.work.com then you can target your work machines as:

Host *.work.com
User alan.young
IdentityFile ~/.ssh/work_id_rsa

Same applies for your personal machines.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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