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

I'd like to be able to setup a command to run on ssh login to a server, without needing to type it. Basically I'm looking for the ssh config file equivalent of:

ssh host command

so that all I need to type is:

ssh host

and the command gets run.

share|improve this question

It is also possible to insert a command in your authorized keys file. (~/.ssh/authorized_keys). This allows you to execute a custom command for each key in the file. I use this to forward shell connections through my firewall. The result is that I can ssh to one host and it automatically connects the session to a host inside the network. The authorized_keys entry looks like this:

command="ssh -Tq <hostname> \"$SSH_ORIGINAL_COMMAND\"",no-port-forwarding,no-agent-forwarding,no-X11-forwarding,no-pty ssh-rsa AAAA... the rest of the key ...

More specifically the redirect is for my gitolite instance. This allows simple outside access without directly exposing the gitolite host to any external access. Check the man page for more info. ( )

share|improve this answer
This is awesome, didn't know you could do this. +1 – Brian Wigginton Sep 24 '13 at 19:48
If you put the command in your authorized_keys file it'll break things such as rsync and scp - you'll get something like this: open terminal failed: not a terminal. Also, be aware that if the command fails you'll not be able to login. I've discovered this the hard way. – John Hunt Sep 18 '15 at 11:32

If you are running OpenSSH, it looks like ~/.ssh/rc is executed upon login.

share|improve this answer
Is /.ssh/rc located on the client or server machine? Looks like from the docs the server, right? – heavyd Mar 25 '10 at 18:45
Yes, that would be on the server machine. – coneslayer Mar 25 '10 at 18:52

You could set up a bash alias.

In your .bashrc file, put:

alias ssl='ssh some_host run_command'

Then you wouldn't even have to type the hostname.

Or, if you wanted to do this with multiple hosts(and multiple aliases wouldn't work), then use a small script:

kevin@box:~$ cat
ssh $1 some_command
share|improve this answer

You must log in to answer this question.

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