Skip to content
Explore the learning library ☟
SanchitGurukulTechnology · Education · Innovation Take a quiz

Screen Session Sharing: Comprehensive Guide

Screen Session

screen session is a powerful terminal multiplexer that allows users to manage multiple terminal sessions from a single window or remote terminal session. It provides a persistent terminal session that can be reattached later, making it ideal for managing long-running processes on remote servers. Here’s an in-depth look at screen, its features, and how to use it effectively.

Key Features of screen

  1. Multiple Sessions: Run multiple shell sessions within a single terminal window.
  2. Session Persistence: Detach from a session, keep it running in the background, and reattach later.
  3. Session Sharing: Share a terminal session with other users for collaborative work.
  4. Scrollback Buffer: Access a scrollback buffer to view terminal output history.
  5. Window Management: Split the terminal into multiple regions and switch between them.
  6. Customizability: Highly configurable through the .screenrc file.

Advanced Features – Screen Session

To share a Screen Session with another user, follow these steps:

  1. Start a screen session:
Command
screen -S shared_session
Screen Session
  1. Enable multiuser mode: Press Ctrl-a followed by :, then type:
Command
multiuser on
image
image
  1. Add a user to the session: Press Ctrl-a followed by :, then type:
Command
acladd username
image
  1. Other user joins the session: The other user can join the session using:
Command
screen -x username/shared_session

Customizing Screen

Change the default escape sequence:

The default escape sequence in screen is Ctrl-a, which is used to issue commands within the screen session. If you want to change this to a different key sequence, you can use the escape command in your .screenrc configuration file or within a screen session.

Changing the Escape Sequence

  • Modify .screenrc File:
    • Open or create the .screenrc file in your home directory:
Command
nano ~/.screenrc
  1. Add the following line to change the escape sequence:
Command
escape ^Xx

In this example, ^X is Ctrl-x, and x is the literal character. So, the new escape sequence would be Ctrl-x followed by x. You can replace ^Xx with your desired sequence.

  • Change the Escape Sequence During a Session:
    • You can also change the escape sequence on the fly by entering the following command in your screen session:
      • Press Ctrl-a, then : to bring up the command prompt.
      • Type:
Command
escape ^Xx
  1. Replace ^Xx with your desired sequence.

Example:

If you want to change the escape sequence to Ctrl-b followed by b, you would set it up like this:

  • In .screenrc:
Command
escape ^Bb
  • In a running session:
Command
escape ^Bb

Important Notes:

  • Escape Character: The first character (^B in the example) is the control character you press, and the second character (b) is the literal character you type after the control key.
  • Avoid Conflicts: Ensure the new escape sequence doesn’t conflict with other commonly used shortcuts or commands.
Command
escape ^Aa

This changes the command prefix from Ctrl-a to Ctrl-a a.

  • Set a scrollback buffer size:

To set a scrollback buffer size in screen, you can configure it either by modifying your .screenrc file or by setting it within a running screen session.

1. Set Scrollback Buffer Size in .screenrc

Step 1: Open or create the .screenrc file in your home directory:

Command
nano ~/.screenrc

Step 2: Add the following line to set the scrollback buffer size:

Command
defscrollback 5000

Replace 5000 with the number of lines you want to be kept in the scrollback buffer.

2. Set Scrollback Buffer Size in a Running Screen Session

Step 1: While in a screen session, press Ctrl-a followed by :.

Step 2: Type the following command and press Enter:

Command
scrollback 5000

Again, replace 5000 with your desired number of lines.

Explanation:

defscrollback 5000: This sets the default scrollback buffer size for all new windows in screen to 5000 lines. If you want a different size, simply change the number.

scrollback 5000: This sets the scrollback buffer size for the current window in the screen session to 5000 lines.

Note:

Modify .screenrc: To apply the setting to all screen sessions by default.

Set during a session: To change the setting temporarily for the current session.

This will allow you to scroll back and view more lines of output in your screen session.

Command
defscrollback 10000

This increases the scrollback buffer to 10,000 lines.

  • Customize the status line:

The hardstatus line you’ve provided is a configuration for the screen utility. It customizes the status line that appears at the bottom of the screen session, showing useful information such as window titles, the hostname, and the current date and time.

Here’s a breakdown of what this configuration does:

Configuration Breakdown:

  • hardstatus alwayslastline:
    • This ensures that the status line is always displayed on the last line of the terminal window.
  • “%{= kG}%-w%{= kW}%n %t%{-}%+w %=%{= kw} %H %Y-%m-%d %c”:
    • %{= kG}: Sets the text color and background. kG means black text on a green background.
    • %-w: This part handles the left side of the window list.
    • %{= kW}: Sets the window title’s color. kW means black text on a white background.
    • %n: Displays the window number.
    • %t: Displays the window title.
    • %{-}: Resets the colors to the default for the next part.
    • %+w: This part handles the right side of the window list.
    • %=: Places the following content on the right side of the status line.
    • %{= kw}: Sets the text color to black on a white background.
    • %H: Displays the hostname.
    • %Y-%m-%d: Displays the current date in YYYY-MM-DD format.
    • %c: Displays the current time in HH:MM format.

Note:

This hardstatus line creates a status bar that shows:

  1. Window List: On the left, a list of windows with their numbers and titles.
  2. Hostname: On the right, the hostname of the machine.
  3. Date and Time: The current date and time on the far right.

Example Appearance:

  • Windows: [0 bash] [1 editor]
  • Hostname, Date, and Time: myhost 2024-08-31 12:34

This configuration provides a clear and informative status line, helping you keep track of active windows and system information while working in screen.

Command

This sets a custom status line showing the window list, host name, and date/time.

Practical Use Cases

Running Long-Running Processes

When running long-running processes on a remote server, screen allows you to start a process, detach from the session, and reattach later to check on the progress or interact with the process. This is particularly useful for tasks like compiling large codebases, running database migrations, or performing data analysis.

Example:

Here’s how to correctly use screen to manage a long-running process:

  1. Start a new screen session:
Command
screen -S long_task
  1. Run your long-running task:
Command
./run_long_task.sh
  1. Detach from the screen session:
    • Press Ctrl-a, then d.
  2. To reattach to the session later:
Command
screen -r long_task
Output / Result
Rearranged for clarity:screen -S long_task      # Start a new screen session named 'long_task'./run_long_task.sh       # Run your long-running processCtrl-a d                 # Detach from the sessionscreen -r long_task      # Reattach to the session when

This allows your process to continue running in the background, even if you disconnect from the terminal.

Managing Multiple Sessions

Developers and system administrators often need to run multiple terminal sessions simultaneously. With screen, you can create multiple windows within a single session and switch between them easily, improving productivity and organization.

Example:

Here’s a step-by-step breakdown of how to use screen to create and manage multiple windows for different tasks like editing code and running an application:

  1. Start a new screen session:
Command
screen -S dev_session
  1. Create a new window for editing code:
    • Press Ctrl-a, then c to create a new window.
    • In this new window, you can start editing your code:
Command
nano my_code.py
  1. Create another window for running the application:
    • Press Ctrl-a, then c again to create another new window.
    • In this new window, you can run your Python application:
Command
python3 my_code.py

Summary of Commands:

Output / Result
screen -S dev_session  # Start a new screen session named 'dev_session'Ctrl-a c               # Create a new window for editing codenano my_code.py        # Edit your codeCtrl-a c               # Switch to a new window for running the applicationpython3 my_code.py     # Run your Python application

You can switch between these windows by using Ctrl-a followed by the window number (0, 1, etc.), or by using Ctrl-a followed by n (next) or p (previous).

Session Sharing for Collaboration

Collaborative tasks, such as pair programming or troubleshooting with a colleague, are simplified with screen session sharing. Both users can view and interact with the same terminal session, facilitating real-time collaboration.

Here’s a step-by-step guide to setting up a shared screen session between two users, where User 1 starts the session and User 2 joins:

User 1: Start the Shared Session

  1. Start a new screen session:
Command
screen -S shared_dev
  1. Enable multiuser mode:
    • Press Ctrl-a, then : to enter the screen command mode.
    • Type the following command and press Enter:
Command
multiuser on
  1. Allow User 2 to join the session:
    • Press Ctrl-a, then : again.
    • Type the following command to add User 2 to the session:
Command
acladd user2

User 2: Join the Shared Session

  1. User 2 joins the shared session:
    • On User 2’s terminal, use the following command to attach to the session started by User 1:
Command
screen -x user1/shared_dev
  1. Replace user1 with the actual username of User 1.

Summary:

  • User 1 starts and configures the session with screen -S shared_dev, enables multiuser mode with Ctrl-a : multiuser on, and adds User 2 with Ctrl-a : acladd user2.
  • User 2 joins the session with screen -x user1/shared_dev.

This allows both users to view and interact with the same terminal session.

Summary – Screen Session

screen is an invaluable tool for Linux users, particularly for those managing remote servers or long-running processes. Its ability to handle multiple terminal sessions, detach and reattach to sessions, and share sessions with other users makes it a versatile and powerful utility. By mastering screen, users can enhance their productivity and efficiency in managing terminal-based tasks. Despite some complexity and a learning curve, the benefits of using screen for session management and process persistence make it a worthwhile tool in any Linux user’s toolkit.

screen in Linux

Useful Links

Screen Session Sharing: Comprehensive Guide

This article provided insights on the topic. For latest updates and detailed guides, stay connected with Sanchit Gurukul.

Your feedback matters

Was this post helpful?

0 reactions

SanchitGurukul — Technology • Education • Innovation

Discussion

0 approved comments

No approved comments yet. You can start the discussion below.

Leave a Comment

No login is required. Name and email are used for moderation/security. Your email is never displayed publicly. All comments require administrator approval.

This field is not used to determine whether a comment is safe.

Plain text only. File uploads, scripts, executable code, abuse and spam are not accepted.


Discover more from SanchitGurukul

Subscribe to get the latest posts sent to your email.

Keep exploring

Related guides

Namaste! I’m SG Saarthi 👋Need help learning or finding something?