I'm able to run a script automatically every time I log into my machine via SSH by placing a command in the /etc/ssh/sshrc file. That works perfectly, however it's only triggered when I log in via terminal or SFTP file browser like Transmit.

It does not run my script when I connect to a MySQL or MongoDB instance over SSH (where the database host is localhost through an SSH tunnel, see below).

How can I ensure this script gets run on every SSH connection to the server?

enter image description here

  • So you mean every time you get a new connection established over an already existing ssh tunnel? – Jo-Erlend Schinstad Mar 30 '15 at 6:28
  • What is the screenshot from? – don.joey Mar 30 '15 at 8:51
  • No these are new tunnels, not existing ones. Screenshot is from Sequel Pro, MySQL browser. sequelpro.com – winduptoy Mar 30 '15 at 12:32

There are different ways:

  • use a ~/.ssh/rc script;
  • use the ForceCommand keyword in sshd.conf telling sshd to execute a command on every login (but this will also make sshd ignore any command supplied by the client and ~/.ssh/rc if present).
  • monitor /var/log/auth using a tool like logwatch.

I assume using a rc script is the easiest solution.

  • Options 1 and 2 did not work. My script is run when I log in via terminal, but not MongoDB/MySQL ssh tunnel. – winduptoy Mar 31 '15 at 18:05

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.