I have multiple screen sessions running on one of my server (say session_read, session_prod..)

Currently I specify the screen session after I have sshed into my server.

Is there a way to specify the screen session while I am trying to ssh in?

Something like

ssh user@servername screen_session_name

You need to allocate a TTY, or SSH will complain:

$ ssh host screen
Must be connected to a terminal.

Assuming the session is already disconnected:

$ ssh -t user@host screen -r session

otherwise a suitable combination of -d, -r, -D, -R, -x per the man page.

| improve this answer | |

I use a custom hardstatus line in ~/.screenrc:

hardstatus alwayslastline "%{wk}%-w%{Gk}[%n %t]%{wk}%+w%=%{Ck}%d %M %Y %c:%s"

combined with a little shell script:

#!/usr/bin/env bash
show_help() {
  printf "Usage: con hostname\n"
  exit 0

[[ "$#" -eq 1 ]] || show_help


screen -t "${uppercase}" -T screen-256color ssh "${lowercase}"

exit 0

Put it somewhere in your PATH, and you only need to call it:

$ my_script hostname

and a new screen session will be allocated with the title set to the hostname you are connecting to.

You can provide further customizations via ~/.ssh/config settings.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.