Thursday, 15 November 2012

How to directly login to the screen session running on your remote server using ssh

You know that I am a big fan of gnu screen utility. Also we have seen many short cuts for screen here. We used to access our screen session after log in to the remote server through ssh and then 'screen -x screenname' command. But it is actually time consuming. Yes we can directly login to the screen session using the awesome ssh. Let's find out how.

You may think that we can type the command,

ssh [email protected] "screen -x screenname"

just like

ssh [email protected] "df -h"

But it will give an error like this.

"Must be connected to a terminal."

and you will be disconnected from remote server. So what we can do here. Screen needs a terminal to run this. We can add the command line argument -t to the ssh command which will create a terminal and run the screen command.

ssh [email protected] -t "screen -x screenname"

This will directly log you into the screen session in your remote server. Then you can run your script or what ever command you want to run in the screen. How you can you disconnect from the session then.

You can run the Ctrl+a+d to detach the screen session just like in a regular screen session. But this will log you out from the remote terminal. But don't worry, your screen session will be there in the remote server in detached state.

1 comment:

  1. Thank you very much for this article.
    However I think you mixed up commands and the right line is:

    ssh [email protected] -t "screen -x screenname"

    Thanks Again