i cannot run a command before connect with ssh to remote server using ~/.ssh/config file.

I have tried with options "ProxyCommand" and "LocalCommand" but it doesn't work.

  • ProxyCommand: seems it run the command, but after that doesn't ask me the password of ssh connection
  • LocalCommand: run the command only if the connection successful
  • Have you considered aliases or functions?
    – Jeff Schaller
    Jan 4, 2019 at 11:57
  • 2
    Theorectically it could be done with ProxyCommand and passing a scriptname that contains what you want to be executed, but you need to ensure that the script ends with setting up a connection to the remote host, fulfilling the "proxy" part. E.g. /usr/bin/nc %h %p
    – wurtel
    Jan 4, 2019 at 12:34
  • What command in particular are you trying to run?
    – Damian T.
    Jan 4, 2019 at 14:30
  • @JeffSchaller Thanks for your answare. Yes, before i was using aliases, but config file of ssh is more portable and smart than aliases.
    – Killouis
    Jan 4, 2019 at 15:01
  • @wurtel Thanks for your answare. Can i put my command and on success execute "nc" without log? E.g. /bin/echo hello > /tmp/test && /usr/bin/nc %h %p > /dev/null 2>&1
    – Killouis
    Jan 4, 2019 at 15:04

1 Answer 1


See these answers: https://unix.stackexchange.com/a/44343 (using ProxyCommand) and https://askubuntu.com/a/1268036 (using Match host ... exec)

  • I solved with the Match host! Thank you @silas-s-brown :)
    – Killouis
    Dec 26, 2020 at 18:19

Your Answer

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

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