GICT knowledge base
How to Use screen and tmux for Persistent Sessions in Linux
Published
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/Ubuntusudo dnf install screen# Fedora/RHEL - tmux:
sudo apt install tmuxsudo 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) orP(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) orP(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
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
