How to Tell If You're in a Screen (Remote Desktop)
In this article
You can tell whether you are in a screen session by running echo $STY in your terminal: if it prints a session name, you are inside a screen; if it prints nothing, you are not. Alternatively, press Ctrl+A then ? to see the screen help overlay, which only appears when screen is active. This guide covers those checks plus how to tell if you are in a nested screen, a screen within tmux, or a screen session that has been detached and reattached.
What You'll Need
- Access to a terminal or command line on the machine where screen is installed (Linux, macOS, or WSL on Windows).
- Basic familiarity with typing commands and reading their output.
- No special permissions are required for the checks below; screen runs under your own user account.
Step-by-Step
- Check the STY environment variable. In your terminal, type
echo $STYand press Enter. If you are inside a screen session, this prints something like12345.pts-0.hostname. If it prints a blank line, you are not in screen. This is the fastest and most reliable check because screen sets STY automatically when it starts a session. - Use the screen escape key. Press
Ctrl+Afollowed by?. If screen is active, the screen help screen appears with a list of key bindings. PressSpaceorEnterto dismiss it. If nothing happens, you are not in screen (or your escape key has been remapped; see Troubleshooting). - List your screen sessions from outside. Open a separate terminal (or detach first with
Ctrl+AthenD) and runscreen -ls. This lists all screen sessions for your user, showing whether they are attached or detached. If you see a session marked(Attached), you are likely inside it in another terminal. - Check for nesting with the screen title. When you are in a screen session, the terminal title bar often changes to include the session name (depending on your terminal emulator). You can also run
screen -lsfrom inside a screen; it will list the current session and any others. If you see multiple sessions and one is attached, you may be in a nested screen. - Distinguish screen from tmux. If you are unsure whether you are in screen or tmux, check the TMUX environment variable:
echo $TMUX. If it prints a path, you are in tmux. If both STY and TMUX are set, you are in a nested setup (screen inside tmux or vice versa). - Confirm with the process list. Run
ps -p $PPID -o comm=to see the parent process name. If it showsscreen, you are likely inside a screen session. This is a secondary check; STY is more direct.
Common Mistakes
- Assuming a blank STY means no screen. If you started screen with a custom environment or via a wrapper script that unsets STY, the variable may be empty even inside screen. Use the Ctrl+A ? check as a backup.
- Pressing Ctrl+A in a text editor. In many editors, Ctrl+A moves the cursor to the beginning of the line. If you press it expecting screen help, you may just move the cursor. Always try the escape key in a plain shell prompt.
- Confusing screen with tmux. Both use similar key bindings but different escape keys (Ctrl+A for screen, Ctrl+B for tmux by default). Checking STY vs TMUX avoids confusion.
- Forgetting that screen -ls only shows your own sessions. If you are logged in as a different user, you will not see sessions belonging to others. This is normal and not an error.
Tips
- You can rename your screen session for clarity: press
Ctrl+AthenA, and type a new name. This name appears inscreen -ls. - If you frequently forget whether you are in screen, add a visual indicator to your shell prompt, such as showing
$STYwhen set. Many shell themes support this. - To detach from a screen session without killing it, press
Ctrl+AthenD. You can reattach later withscreen -r. - Use
screen -S nameto start a named session, making it easier to identify later.
Troubleshooting
- Ctrl+A ? does nothing. Your escape key may have been changed in
~/.screenrc. Check that file for a line likeescape ^Bb(which would make the escape key Ctrl+B). Try that combination instead. - STY is set but screen commands fail. You may be in a subshell or environment where screen is not actually running. Run
ps -p $PPID -o comm=to confirm the parent process. If it is not screen, you are not in a screen session despite STY being set (rare, but possible if the variable was exported manually). - screen -ls shows no sessions but you think you are in one. You might be in a different user account or a container without access to the screen socket directory. Check
ls /run/screenorls /var/run/screenfor socket files. - Nested screen confusion. If you accidentally start screen inside screen, you will see multiple layers. Press
Ctrl+AthenNto switch to the next screen, or detach one layer at a time withCtrl+AthenD.
Frequently Asked Questions
How can I tell if I'm in a screen session without running a command?
Press Ctrl+A then ?. If the screen help overlay appears, you are in screen. If nothing happens, you are not (or your escape key is remapped).
What does the STY environment variable contain?
It contains the session name, typically in the format pid.tty.host, for example 12345.pts-0.myserver. An empty or unset STY means you are not in screen.
Can I be in both screen and tmux at the same time?
Yes, you can run tmux inside screen or screen inside tmux. Check both $STY and $TMUX to see which layers are active.
Why does screen -ls show my session as attached when I'm not in it?
Another terminal or process may be attached to that session. You can detach it with screen -d followed by the session ID, or attach and detach manually.
Is there a way to make my shell prompt show when I'm in screen?
Yes, you can customize your shell prompt to include $STY when it is set. For example, in Bash you can add ${STY:+[screen]} to your PS1 variable.