
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Screen Command Examples to Manage Linux Terminals
In the world of Linux system administration and development, working with multiple terminal sessions is a common necessity. The screen command is a powerful terminal multiplexer that allows you to create, access, and manage multiple terminal sessions within a single window. This comprehensive tutorial will explore the screen command in detail, covering its functionalities, options, and practical use cases, empowering you to manage your Linux terminals more efficiently.
Why Use screen?
screen offers several key advantages ?
- Detaching and Reattaching Sessions ? You can detach from a screen session and reattach to it later, even from a different location. This is incredibly useful for long-running processes or when working remotely.
- Multiple Windows within a Single Terminal ? You can create multiple "windows" (virtual terminals) within a single screen session, allowing you to run different commands or applications simultaneously.
- Session Persistence ? If your terminal connection is interrupted, your screen session and all running processes within it will continue to run in the background.
- Sharing Sessions ? You can share a screen session with other users, allowing for collaborative work or remote assistance.
Installation: Screen Command
screen is usually available in the repositories of most Linux distributions.
Debian/Ubuntu
sudo apt update sudo apt install screen
CentOS/RHEL
sudo dnf install screen
Basic Usage of Screen Command
Starting a new screen session ?
screen
This will open a new screen session. You'll be presented with a new shell prompt.
Detaching from a screen session ?
Press Ctrl+a followed by d (Ctrl+a d). This will detach you from the current screen session, leaving it running in the background.
Listing running screen sessions ?
screen -ls
This will list all currently running screen sessions, showing their session IDs.
Reattaching to a screen session ?
screen -r
If there's only one detached session, this will reattach to it. If there are multiple sessions, you'll need to specify the session ID ?
screen -r session_ID
You can get the session ID from the screen -ls command.
Working with Multiple Windows
Within a screen session, you can create multiple windows (virtual terminals).
Creating a new window ?
Press Ctrl+a followed by c (Ctrl+a c). This will create a new window.
Switching between windows ?
- Press Ctrl+a followed by n (Ctrl+a n) to switch to the next window.
- Press Ctrl+a followed by p (Ctrl+a p) to switch to the previous window.
- Press Ctrl+a followed by a number (e.g., Ctrl+a 0, Ctrl+a 1) to switch to a specific window by its number.
Renaming a window ?
Press Ctrl+a followed by A (Ctrl+a Shift+a). This will prompt you to enter a new name for the current window.
Listing windows ?
Press Ctrl+a followed by w (Ctrl+a w). This will display a list of all open windows with their numbers and names.
Killing a window ?
Press Ctrl+a followed by k (Ctrl+a k). This will kill the current window.
Other Useful screen Commands
Scrolling within a window ?
Press Ctrl+a followed by [ (Ctrl+a [). This will enter copy mode, allowing you to scroll using the arrow keys, Page Up, and Page Down. Press Esc to exit copy mode.
Copying and pasting text ?
In copy mode (Ctrl+a [), move the cursor to the beginning of the text you want to copy, press Space, move the cursor to the end of the text, press Space again. Then, press Ctrl+a followed by ] (Ctrl+a ]) to paste the copied text.
Logging a session ?
Press Ctrl+a followed by H (Ctrl+a Shift+h). This will start logging the current window's output to a file named screenlog.0 (or screenlog.1, screenlog.2, etc., for subsequent sessions).
Quitting screen entirely ?
Exit all windows within the screen session. Once all windows are closed, the screen session will terminate. Alternatively, you can use the exit command within a window.
Practical Use Cases
Let's now highlight some of the practical use-cases of the screen command ?
- Running Long Processes ? Start a long-running process in a screen session and detach from it. The process will continue to run even if your terminal connection is interrupted.
- Remote Server Management ? Connect to a remote server via SSH, start a screen session, and run your commands. If your SSH connection drops, you can simply reconnect and reattach to the screen session.
- Working on Multiple Tasks Simultaneously ? Create multiple windows within a screen session to work on different tasks without constantly switching between terminal windows.
- Sharing Terminal Sessions ? Share a screen session with a colleague for collaborative work or remote troubleshooting.
Conclusion
The screen command is an invaluable tool for managing Linux terminals. Its ability to create persistent sessions, manage multiple windows, and detach/reattach sessions significantly enhances productivity and streamlines workflows. By mastering the commands and options outlined in this guide, you can effectively leverage screen to manage your Linux terminals like a pro.