Mastering Linux Screen: A Comprehensive Guide for Beginners and Advanced Users
Linux screen
is a powerful terminal multiplexer that allows users to create, manage, and detach multiple terminal sessions within a single window. This is especially useful for managing remote SSH sessions, running long processes, and maintaining workflow efficiency.
Introduction to Linux Screen
Why Use screen
?
- Keeps sessions active even after disconnection
- Allows multiple windows within a single terminal
- Supports session detaching and reattaching
- Useful for remote administration and long-running tasks
Installing screen
Most Linux distributions come with screen
pre-installed. If not, install it using:
# Debian/Ubuntu
sudo apt install screen
# RHEL/CentOS
sudo yum install screen
# Arch Linux
sudo pacman -S screen
Basic Commands for Beginners
Starting a New Screen Session
To start a new screen session, simply run:
screen
You will be placed in a new screen session where you can run commands as usual.
Detaching and Reattaching Sessions
- Detach a session: Press
Ctrl + A
, thenD
- List active screen sessions:
screen -ls
- Reattach a detached session:
screen -r
Naming Sessions
To make it easier to manage multiple sessions, name them:
screen -S mysession
To reattach a named session:
screen -r mysession
Advanced Usage of Screen
Splitting Windows
You can split the screen horizontally or vertically:
- Horizontal split:
Ctrl + A
, thenS
- Vertical split:
Ctrl + A
, then|
- Switch between panes:
Ctrl + A
, thenTab
- Close a pane:
Ctrl + A
, thenX
Creating and Navigating Between Windows
- Create a new window:
Ctrl + A
, thenC
- Switch between windows:
Ctrl + A
, thenN
(next) orP
(previous) - List all windows:
Ctrl + A
, then"
Logging and Monitoring Output
Screen allows logging of terminal output:
Ctrl + A, then H
This creates a screenlog.0
file in the current directory.
Sharing a Screen Session
To allow another user to connect to your screen session:
- Start screen with:
screen -S shared
- Enable multiuser mode:
Ctrl + A, then :multiuser on
- Grant permission to a user:
Ctrl + A, then :acladd username
- The other user can connect with:
screen -x username/shared
Customizing Screen
Modify ~/.screenrc
to add custom settings. Example:
defscrollback 5000
startup_message off
hardstatus alwayslastline "%{=b}%H | Screen session: %S | %d/%m %c"
Troubleshooting Common Issues
Fixing "Cannot Reattach"
If screen -r
fails, find the session ID:
screen -ls
Then attach with:
screen -r session_id
If the session is still attached, force detach it:
screen -d session_id
screen -r session_id
Top 20 Linux Screen FAQs
- How do I exit a screen session?
Type
exit
or pressCtrl + D
to close the session. - How do I detach from a screen session without closing it?
Press
Ctrl + A
, thenD
. - How do I list active screen sessions?
Use
screen -ls
. - How do I reattach to a detached screen session?
Use
screen -r
. - How do I name a screen session?
Start it with
screen -S session_name
. - How do I kill a screen session?
screen -X -S session_name quit
- How do I switch between multiple windows in screen?
Press
Ctrl + A
, thenN
for next orP
for previous. - How do I create a new window in an existing session?
Press
Ctrl + A
, thenC
. - How do I split the screen horizontally?
Ctrl + A
, thenS
. - How do I split the screen vertically?
Ctrl + A
, then|
. - How do I close a split pane?
Ctrl + A
, thenX
. - How do I log screen output to a file?
Ctrl + A
, thenH
. - How do I scroll back in screen?
Ctrl + A
, thenEsc
, use arrow keys. - How do I search within screen history?
In scrollback mode (
Ctrl + A
,Esc
), press/
and type the search term. - How do I lock my screen session?
Ctrl + A
, thenX
. - How do I clear a screen window?
Use the command
clear
. - How do I rename a screen window?
Ctrl + A
, thenA
, type the new name. - How do I copy text within screen?
Ctrl + A
, thenEsc
, navigate, thenSpace
to select,Enter
to copy. - How do I paste text in screen?
Ctrl + A
, then]
. - How do I share a screen session with another user? Enable multiuser mode and use access control commands as shown above.
Conclusion
The Linux screen
command is an essential tool for both beginners and advanced users. It enhances productivity by allowing multiple terminal sessions to run simultaneously, even after disconnecting. Mastering screen
will make remote server management and long-running tasks much more efficient.
✅ A personalized DevOps roadmap tailored to your experience
✅ Hands-on guidance on real-world DevOps tools
✅ Tips on landing a DevOps job and interview preparation
Comments
Post a Comment