Using different ssh config options based on port

I have a situation where I have to use different login credentials on the same IP address based on the port the I use.

So and example on the command line would look like this:

ssh user1@192.168.1.2:22

and

ssh user2@192.168.1.2:2222

I have setup the following in my ~/.ssh/config file:

 Host "192.168.1.*" user user1 IdentityFile ~/.ssh/id_user1 PubkeyAuthentication yes

which works great for the user1 at port 22 scenario but is there a way to setup an alternate user and IdetityFile for user2 at port 2222?

2 Answers

I guess I should have kept searching....

SSH config host match port over on superuser.com had the answer I needed!

Basically instead of:

Host "192.168.1.*"

I'm now using:

Match host "192.168.1.*" exec "test %p = 22"

and

Match host "192.168.1.*" exec "test %p = 2222"
1

I didn't try it out, but I think the HostName option (see man ssh_config could provide you a solution:

 Host "host22" Hostname "192.168.1.2" Port 22 user user1 IdentityFile ~/.ssh/id_user1 PubkeyAuthentication yes Host "host2222" Hostname "192.168.1.2" Port 2222 user user2 IdentityFile ~/.ssh/id_user2 PubkeyAuthentication yes

With this you should be able to do:

ssh user1@host22
ssh user2@host2222

or even

ssh host22
ssh host2222

as the user name and the port are given in the config file.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like