Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I'd like to be able to modify ssh_config aliases based on my current IP address.

I have a laptop which I carry around from network to network, and a camera which I do the same with. I have setup the dhcp on these networks to always give the camera the same IP address, and I calculate what it should be in my bashrc and put the camera IP into an environment variable.

Is there any way to do something like the following inside ~/.ssh/config:

Host camera
    HostName $CAMERA_IP
    User camuser

The above doesn't work as ssh complains:

ssh: Could not resolve hostname $CAMERA_IP: Name or service not known
share|improve this question
you could just add an alias in your hosts file ... instead of the ssh_config – Paul Bastide Jan 31 '14 at 13:15
But I want to do it based on my IP. I don't think the hosts file is aware of my user's environment either is it? – Sandy Patterson Jan 31 '14 at 13:21
you can just alias it. add an entry like IP camera – Paul Bastide Jan 31 '14 at 13:21
Yeah, but if I get the ssh alias's to work I get scp and rsync aliases all too. – Sandy Patterson Jan 31 '14 at 13:24
it'll all work if you just reference camera – Paul Bastide Jan 31 '14 at 13:25
up vote 2 down vote accepted

Since you are doing calculation in your bashrc, you can also generate the ssh_config you need from a template using sed.

The template (.ssh/ssh_config_t):

Host camera
    Hostname IP_PH
    user camuser

The script in bashrc

sed -e "s/IP_PH/$CAMERA_IP/" ~/.ssh/ssh_config_t > ~/.ssh/ssh_config
share|improve this answer
+1 41 seconds faster than me :) – zelanix Jan 31 '14 at 14:56
That's a good idea. I can even edit the ~/.ssh/config directly with sed since I know what the IP addresses that I set are. – Sandy Patterson Jan 31 '14 at 15:21

I think how I would solve this would be to use a template config file as follows.


Host camera
    HostName $CAMERA_IP
    User camuser

And then a simple update script.



# Do whatever to set $CAMERA_IP

# Update config file
sed "s/\$CAMERA_IP/$CAMERA_IP/" ~/.ssh/config_template > ~/.ssh/config

And then just call ~/.ssh/ from your bashrc file. Just remember to not make any manual changes to ssh_config.

share|improve this answer

Your Answer


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.