Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Use screen and tmux for Persistent Sessions in Linux

Published

Linux and Commands

If you frequently work on Linux servers or remote systems, you’ve probably run into a common problem: you start a long-running process (like compiling code, running backups, or managing a server), but your SSH connection drops or you accidentally close your terminal window. Suddenly, your task is interrupted or lost, and you have to start over.

This is where session managers like screen and tmux become essential tools. They let you start a session, run programs inside it, and then safely disconnect—knowing your work keeps running in the background. Later, you can reconnect and pick up right where you left off.

In this guide, you’ll learn how to use both screen and tmux to create, manage, and reconnect to persistent sessions on Linux. You’ll see the differences between them, practical examples, and tips from real-world scenarios.

Why Use Persistent Sessions?

  • Resilient to disconnects: Your processes keep going even if you lose network connection or close your terminal.
  • Multi-tasking: Run multiple programs in tabs or panes, switch between them, and split your workspace.
  • Remote collaboration: Share your session for pair programming or troubleshooting.
  • Convenience: Start something, go do something else, and come back to it later—no lost progress.

Getting Started: Installing screen and tmux

On most Linux distributions, these tools are either pre-installed or available via the package manager.

To install:

  • screen: sudo apt install screen # Debian/Ubuntu sudo dnf install screen # Fedora/RHEL
  • tmux: sudo apt install tmux sudo dnf install tmux

Using screen

1. Start a New screen Session

screen

You’ll be dropped into a new shell, inside a screen session.
To name your session (for easier reconnection):

screen -S mysession

2. Running Commands

Run any command or script as you normally would. For example:

python myscript.py

3. Detach and Leave Your Session Running

To safely detach (leave the session running and return to the normal shell), press:

Ctrl + A, then D

(Press and hold Control, then press A, then release both and press D.)
You’ll see a message like “Detached from session.”

4. List Existing Sessions

screen -ls

Shows all current screen sessions.

5. Reattach to a Session

screen -r

If you have multiple sessions, specify the name or ID:

screen -r mysession

6. Kill a Session

While inside the session, type exit, or kill from outside:

screen -S mysession -X quit

7. Other Useful screen Shortcuts

  • Create a new window: Ctrl + A, then C
  • Switch windows: Ctrl + A, then N (next) or P (previous)
  • List windows: Ctrl + A, then " (double quote)
  • Rename window: Ctrl + A, then A

Tip: If you forget the shortcuts, type Ctrl + A, then ? for help.

Using tmux

tmux offers a more modern and flexible experience than screen, with better window and pane management.

1. Start a New tmux Session

tmux

Or start with a custom name:

tmux new -s mysession

2. Run Your Commands

Work as usual—your shell and any programs you run are protected inside tmux.

3. Detach from tmux

Press:

Ctrl + B, then D

(Press and hold Control, then press B, release both, then press D.)
You’ll return to your normal shell with the session running in the background.

4. List tmux Sessions

tmux ls

5. Reattach to a tmux Session

tmux attach-session -t mysession

Or use:

tmux a -t mysession

6. Kill a tmux Session

From outside:

tmux kill-session -t mysession

Or, inside tmux, just type exit to close a window, and the session will end when all windows are closed.

7. Windows and Panes in tmux

  • New window: Ctrl + B, then C
  • Switch windows: Ctrl + B, then N (next) or P (previous)
  • Split horizontally: Ctrl + B, then %
  • Split vertically: Ctrl + B, then "
  • Switch panes: Ctrl + B, then arrow keys
  • List windows: Ctrl + B, then W

Tip: Get help anytime with Ctrl + B, then ?

Practical Scenarios

Scenario 1: Running a Long Upgrade

You’re updating your system, but your SSH connection isn’t always stable. Start a tmux session first:

tmux new -s upgrade sudo apt update && sudo apt upgrade

If you disconnect, just SSH back in and reattach.

Scenario 2: Monitoring Multiple Logs

Split your tmux window into panes and tail several logs side-by-side:

tmux
Ctrl + B, then %
tail -f /var/log/syslog

Scenario 3: Working Remotely Over Flaky Networks

Start critical processes (like backups or data processing) inside screen or tmux. If your laptop goes to sleep or your network drops, simply reconnect later and pick up where you left off.

Choosing Between screen and tmux

  • screen: Simpler, installed everywhere, great for basic tasks.
  • tmux: More features, easier window/pane management, more customizable.

If you’re starting fresh, tmux is often preferred for its modern features and active development. For systems where only the basics are available, screen is always a safe bet.

Tips for Productive Use

  • Name your sessions: This makes it easy to reconnect, especially if you run several at once.
  • Customize your config: Both screen and tmux support configuration files in your home directory for custom keybindings or startup windows.
  • Share sessions: Both tools can be used by multiple users for collaborative troubleshooting or teaching.
  • Don’t forget to detach: If you’re leaving your desk or connection, remember to detach rather than just closing your terminal.

Conclusion

With screen and tmux, you gain the power to keep your work running and accessible, no matter what happens to your connection or terminal window. You can confidently manage long jobs, multitask efficiently, and recover from accidental disconnects—all from the command line.

Soon, persistent sessions will become second nature, and you’ll wonder how you ever managed without them.

If you’d like more advanced tips (like scripting tmux layouts, sharing sessions, or customizing your workflow), just let me know!

Related guides

Account services are unavailable until the production backend is configured.