Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

Before I can to connect to a particular remote machine I have to run a certain local command. So instead of ssh me@remote.machine I have to do

ssh me@remote.machine

I would like to automate this so that I only have to do ssh remote.machine.

I know that I can achieve this at the shell level by creating my own ssh script that calls /usr/bin/ssh, but can I do it using the ProxyCommand option of ssh_config?

As far as I understand it, I need something like

Host remote.machine
    ProxyCommand local_command; ssh me@remote.machine

in my ~/.ssh/config file, but not exactly this of course because it's circular!

share|improve this question
up vote 7 down vote accepted

If using ProxyCommand, you must use something like /usr/bin/nc to connect the server.

For invoking your command before connect, you need to use sh -c "command list" to merge the two commands as one.

Host remote.machine
    ProxyCommand sh -c "local_command; /usr/bin/nc %h %p"


If your local_command is too complicated, you can use a script:

cat my_connect.sh

/usr/bin/nc "$1" "$2"

The ssh config becomes:

Host remote.machine
        ProxyCommand /path_to_my_connect.sh %h %p

At the last, you can add your own proxy to the /usr/bin/nc

share|improve this answer
This works perfectly, thanks a lot. – Dave Wilding Jul 31 '12 at 6:03

Use the example from the page you linked:

ProxyCommand /usr/bin/nc -X connect -x %h %p

Invokes nc (netcat) to connect to a proxy (proxy running on and passes %h and %p to the ssh command (%h = hostname, %p = port)

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.