How can I launch a screen session with a command over ssh on a remote server from my desktop?

Try using the -t option to ssh

ssh -t [email protected] 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.

You can use:

ssh root@host screen -m -d "tail -f /var/log/messages"

That starts a detached screen with a command running on it.

   -m   causes screen  to  ignore  the  $STY  environment  variable.  With
        "screen  -m"  creation  of  a  new session is enforced, regardless
        whether screen is called from within  another  screen  session  or
        not.  This  flag has a special meaning in connection with the `-d'
        option:

   -d -m   Start screen in "detached" mode. This creates a new session but
           doesn't  attach  to  it.  This  is  useful  for  system startup
           scripts.

Late answer, but this is what I do, I make an alias (let's call it t) that does this:

ssh $MYSERVER -a -x -t screen -xRR -A -e^Zz -U -O

This tells ssh to disable agent and X11 forwarding, and tells screen to attach to a running session, start a new one if needed, use ^Z as the breakout command, use UTF-8 and be smart about the terminal.

All this means that I can open a terminal, type t and it will open my screen session on $MYSERVER. I can then open another terminal, do the same thing and I get another window to the same session.

It's really nice to have multiple terminal windows to the same screen session so you get to look at two screens tabs at the same time.


By putting the following in the ~/.bashrc file on my server, it starts a screen session the first time I log on to the server, or if one is already running, re-connects me to that session.

I find this very handy:

if [ -n "$SSH_CONNECTION" ] && [ -z "$SCREEN_EXIST" ]; then
    export SCREEN_EXIST=1
    screen -DRi
fi