Ssh – How to launch a screen session with a command over ssh on a remote server from the desktop

gnu-screensshterminaltty

I want to be able to launch screen sessions on remote servers from a single ssh command on my desktop. However, screen seems to need a terminal, which is not available when running a command through ssh.

So the obvious

ssh root@my.machine screen "tail -f /var/log/messages"

(as an example) does not work, and gives

Must be connected to a terminal.

I want ssh to launch the command under a screen so I can log in later and attach as I would to a screen session I would have launched manually.

Best Answer

Try using the -t option to ssh

ssh -t root@my.machine screen "tail -f /var/log/messages"

From man ssh

-t      Force pseudo-tty allocation.  This can be used to execute arbi-
        trary screen-based programs on a remote machine, which can be
        very useful, e.g., when implementing menu services.  Multiple -t
        options force tty allocation, even if ssh has no local tty.