Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|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

uppercase="${1^^}"
lowercase="${1,,}"

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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