51

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.

5 Answers 5

50

You cold solve this in your .ssh/config file, for the host where you want to execute a command, add

  RequestTTY yes
  RemoteCommand <some command>

where <some command> is your command. This also works with screen or tmux.

2
  • 1
    supported only from OpenSSH 7.6
    – idanp
    Apr 8, 2021 at 7:08
  • This works great with RemoteCommand bash --rcfile /etc/skel/.bashrc -i to use bash with a default .bashrc (on Debian). Apr 7, 2022 at 17:50
20

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. ( http://linux.die.net/man/8/sshd )

3
  • 1
    This is awesome, didn't know you could do this. +1 Sep 24, 2013 at 19:48
  • 3
    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, 2015 at 11:32
  • @JohnHunt it will only affect keys where you put the command. It will not break anything else. It's wise to use dedicated keys for rsync anyway (and heavily restrict them). Jan 8 at 3:51
10

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

4
  • 3
    Is /.ssh/rc located on the client or server machine? Looks like from the docs the server, right?
    – heavyd
    Mar 25, 2010 at 18:45
  • 1
    Yes, that would be on the server machine.
    – coneslayer
    Mar 25, 2010 at 18:52
  • 1
    Unfortunately the rc file appears to fail to run things like screen, getting an error "Must be connected to a terminal" May 1, 2017 at 22:26
  • @davidparks21: You probably need to run ssh with the -t option.
    – sligocki
    May 5, 2020 at 14:48
9

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 ssl.sh
#!/bin/sh
ssh $1 some_command
kevin@box:~$
1

I use the following in .ssh/config:

  RemoteCommand cd ~/mydir;bash
  RequestTTY force

You must log in to answer this question.

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