Linux – how to ssh into a remote host and execute a command immediately

linux

If i've already put public key to remote host. So there is no password input problem.

I want to login a remote machine and execute screen -r immediately. Is there a way to achieve this ?

For example:

ssh example.com ;  screen -r 

But this is wrong since screen -r won't send to remote host.

Best Solution

When you run a command on a remote host, by default ssh will not allocate a pseudoterminal. If you run an interactive program like screen on a remote host, you must have a pseudoterminal. The -t option makes this happen. Try:

ssh -t example.com "screen -r"