Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Run a remote command using ssh config file

Writer Matthew Barrera

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

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.

3

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. ( )

2

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

4

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:~$

I use the following in .ssh/config:

 RemoteCommand cd ~/mydir;bash RequestTTY force

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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