I have been reading about how to automatically start a screen when I ssh to a remote server. I would like a simple one-liner to add to my .bash_profile to start screen when I ssh in, and NOT exit my remote connection when I detach the screen.

I have been looking into exec screen and pretty much every combination of -d, -r, -D and -R and cannot figure out what flags I need.

Ideally I would type ssh whatever and be logged into the server in a new screen or it can reattach an old screen, I would kind of like to know how to do both to see which I like better. Then I can either type Ctrl ad or Ctrl d and have it bring me back to the normal ssh login for that server (where you would see the motd). From there I can screen -r back into whatever screen I want, or hit Ctrl d again to log out.

6 Answers 6


A simple screen -R should do the trick.

To verify this, I added screen -R to my .bash_profile on a remote server, logged in, detached from screen, and was dropped back to a normal shell prompt on the remote server. Verified with .bashrc as well. Subsequent logins yielded the expected result (re-attach to screen session).

  • Thanks! I was reading around and I was under the impression that I would need exec screen -R. Glad this worked Apr 9, 2013 at 3:48
  • Is it a bad idea to put logout or exit right after that line s.t. an ssh session is immediately terminated once the screen is closed by ctrl+D?
    – Gerrit-K
    Jan 4, 2017 at 12:49
  • This did not work for me. I started a few dozen screens since the line is executed every time a session starts. How did you do it so that this does not happen?
    – Thomas
    Jun 26, 2018 at 9:04

screen -RR will reattach to the first available session or create one if necessary.

  • that was really useful as using screen -R was causing me trouble with multiple sessions.
    – David V.
    Sep 28, 2016 at 9:09
  • This is awesome, thanks! It connects to the first "Detached" session found (if there are any), and ignores attached sessions. So it allows you to login and have multiple windows open, but still make sure that each session has the protection of running in a screen. Our OPS people thank you! Dec 2, 2016 at 19:36

I had issues with 40 cascading screen sessions being created with some of the solutions when starting a new window or screen session. I was able to eliminate the cascading screen and create a new session if one didn't exist with this:

if [ -z "$STY" ]; then screen -R; fi

It tests whether you're in a screen session and runs screen -R if you aren't. Without the test you get the "Attaching from inside of screen?" warning from screen each time you create a new screen window.

  • +1 - like this better than the screen -ls test that Tony mentioned
    – cwd
    Mar 22, 2016 at 20:53
  • This doesn't work if you do su - followed by su - ORIGINALUSER. May 4, 2020 at 22:02

If I understand your need, you should try :

if $(screen -ls | grep -q pts); then screen -x; else screen -R; fi

It will create only one screen session, otherwise it will reconnect to the existing one in a multi-windows fashion ( even if you have multiple ssh session, you will end up to the same screen session )

  • I like this idea for its recognition that we have multi-window SSH clients (or sometimes go and log in directly on the machine). It's quite flexible if you use something like Bitvise and have multiple terminal windows open - you can turn individual screen windows into separate physical windows.
    – Piku
    Oct 16, 2016 at 20:57
  • 4
    All this can be replaced with screen -xR, which seems to be parsed as "attach if there's something to attach to, else make new screen". Jan 3, 2019 at 14:44
  • screen -xR added to .bashrc works perfectly on Ubutu 20.04 LTS !
    – alphaGeek
    Jun 1, 2021 at 10:29

If you want to connect automatically to a screen session when you connect via SSH, add the code below to your .bash_profile or your .bashrc on your remote machine, it will:

  • Start a new screen session every time you connect through SSH.
  • Re-use already existing detached screen session, always starting with the latest one. If no detached sessions available it starts a new one.
  • When you open multiple SSH connections to your machine you will get a different screen session since we are only re-using detached ones.
  • Avoid loops in case you are adding the script to your .bashrc

Here is the script:

#  Attaches to the first Detached Screen. Otherwise starts a new Screen.

# Only run if we are not already inside a running screen and only if in an SSH session.
if [[ -z "${STY}" && ! -z "${SSH_CLIENT}" ]]; then
  detached_screens=($(screen -ls | grep pts | grep -v Attached))

  for screen in "${detached_screens[@]}"; do
    if [[ "${screen}" == *".pts"* ]]; then
      IFS='.pts' read -ra split <<< "${screen}"
      for id in "${split[@]}"; do

  screen -R $first_id

PS: If you would like to enable this for local terminal, remove && ! -z "${SSH_CLIENT} on the first line.


Here a little extension,

the script for the bashrc check if there is more than one detached screen session. If there is more, then you must choose the session manualy. Whene there is a detached session, you come back to this session. If you exit the session then the ssh session will close up too.

 #Start Screen
 if [ -z "$STY" ]; then

 value=$( screen -ls |grep '\<pts.*Detached\>' |wc -l )

 if [ $value -gt 1 ]; then

  screen -ls


  screen -R

  echo 5 Sek. bis die SSH Session beendet wird.

  echo Strg + C - um ohne screen weiter zu arbeiten.

  sleep 6




You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .