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, then D
  • 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, then S
  • Vertical split: Ctrl + A, then |
  • Switch between panes: Ctrl + A, then Tab
  • Close a pane: Ctrl + A, then X

Creating and Navigating Between Windows

  • Create a new window: Ctrl + A, then C
  • Switch between windows: Ctrl + A, then N (next) or P (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:

  1. Start screen with:
    screen -S shared
    
  2. Enable multiuser mode:
    Ctrl + A, then :multiuser on
    
  3. Grant permission to a user:
    Ctrl + A, then :acladd username
    
  4. 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

  1. How do I exit a screen session? Type exit or press Ctrl + D to close the session.
  2. How do I detach from a screen session without closing it? Press Ctrl + A, then D.
  3. How do I list active screen sessions? Use screen -ls.
  4. How do I reattach to a detached screen session? Use screen -r.
  5. How do I name a screen session? Start it with screen -S session_name.
  6. How do I kill a screen session? screen -X -S session_name quit
  7. How do I switch between multiple windows in screen? Press Ctrl + A, then N for next or P for previous.
  8. How do I create a new window in an existing session? Press Ctrl + A, then C.
  9. How do I split the screen horizontally? Ctrl + A, then S.
  10. How do I split the screen vertically? Ctrl + A, then |.
  11. How do I close a split pane? Ctrl + A, then X.
  12. How do I log screen output to a file? Ctrl + A, then H.
  13. How do I scroll back in screen? Ctrl + A, then Esc, use arrow keys.
  14. How do I search within screen history? In scrollback mode (Ctrl + A, Esc), press / and type the search term.
  15. How do I lock my screen session? Ctrl + A, then X.
  16. How do I clear a screen window? Use the command clear.
  17. How do I rename a screen window? Ctrl + A, then A, type the new name.
  18. How do I copy text within screen? Ctrl + A, then Esc, navigate, then Space to select, Enter to copy.
  19. How do I paste text in screen? Ctrl + A, then ].
  20. 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.


 




📢 Book a 1:1 session with Shyam Mohan K and get:
✅ 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

Popular posts from this blog

DevOps Learning Roadmap Beginner to Advanced

What is the Difference Between K3s and K3d

Lightweight Kubernetes Options for local development on an Ubuntu machine