Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the office, I can connect to internal machines without a proxy, but need a proxy for external connections. Outside of the office, I can connect to other external machines without a proxy, but need to use one of 2 proxies to connect to internal machines.

If I can figure out a way to autodetect what network I'm on, can I instruct ssh to load the appropriate config file?

If not, is there a more elegant solution than writing a shell script to symlink the appropriate config file to ~/.ssh/config (my best idea so far)?

Edit: I think @pcm and JonnyRo understood my question and I'll try what they suggest, but just to be clear, I want

|--------\    Dest   | | |
| Source  \---------\|                 |                  |
| office | No Proxy        | Proxy            |
| outside            | Proxy           | No Proxy         |
share|improve this question

You can use the -F option to select from different config files. for the two network cases. You can then either create aliases that use the different config files, based on which net you are on, or in your login script setup a single alias, based on your IP address.

share|improve this answer
Thanks! Aliases with -F will definitely be my fallback if I can't get a script working based on IP. – Nate Parsons Apr 25 '12 at 6:29
This is awesome, I was just about to ask the same question. I already script some other options to set environment variables when I start a new terminal, I can easily create an alias or symlink the proper .ssh/config based on the environment. – newz2000 May 23 '13 at 18:12

Depending on how your proxy is configured, you can simply build an SSH config entry that works in either situation. For example on one network I regularly use I have to ssh to an intermediate host before I can make an outbound connection. So basically I setup a configuration that looks like this.

# proxy ssh 
Host *%sshproxy
    ProxyCommand ssh /bin/netcat -w 1 $(echo %h | cut -d%% -f1) 22



So when I don't need to use a proxy, I can simply run a command like ssh and get connected, but when I do need the proxy, I run the command ssh

I suspect you could probably setup some kind of alias or auto-complete setting that would automatically append the %proxy bit.

share|improve this answer

Write a shell script that checks your IP against a pattern, then symlinks the appropriate shell script.

If for some reason your IP range is the same for home and work, try switching based on /etc/resolv.conf, which contains the DHCP configured DNS servers.

share|improve this answer

The "Host" option in .ssh/config lets you change the configuration based on the destination. I use that to adjust port forwarding and such based on where I'm going.

share|improve this answer

I have looked into this question in the past and one possible solution is to have such entry in the config file:


ProxyCommand ssh ENTRY_POINT nc %h %p

This way, if you are outside, you can $ ssh machine.inside_domain. If you are inside and have dns resolving, you can $ ssh machine. That works well for me.

Maybe someone can improve upon this idea, maybe changing DNS configuration so it automatically resolves machine to machine.inside_domain if you are outside and use the SSH entry point.

share|improve this answer

You must log in to answer this question.

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