search

Loading

Thursday, November 15, 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 user@server "screen -x screenname"

just like

ssh user@server "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 user@server -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.


2 comments :

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

    ssh user@server -t "screen -x screenname"

    Thanks Again

    ReplyDelete
    Replies
    1. I have corrected this in the post and updated it. Thanks for pointing this out

      Delete