Screen Linux Cheat Sheet

Advertisement

Screen Linux Cheat Sheet

The GNU Screen is a powerful terminal multiplexer that allows users to manage multiple terminal sessions from a single window. It is especially useful for those who work on remote servers or need to run processes that should continue even after disconnecting. This cheat sheet aims to provide a comprehensive overview of the essential commands and functionalities of Screen, making it a valuable resource for both beginners and experienced users.

Getting Started with Screen



Before diving into the commands, it’s important to understand how to install and start using Screen.

Installation



To install Screen on various Linux distributions, you can use the following commands:

- Debian/Ubuntu:
```bash
sudo apt-get install screen
```

- Fedora:
```bash
sudo dnf install screen
```

- CentOS/RHEL:
```bash
sudo yum install screen
```

- Arch Linux:
```bash
sudo pacman -S screen
```

Starting Screen



To start a new Screen session, simply type:
```bash
screen
```
If you want to name your session, use:
```bash
screen -S session_name
```

Basic Commands



Once inside a Screen session, you can use a series of commands to control and manage your sessions. Most commands are preceded by a control key, often the `` key combined with the letter `a` (denoted as `Ctrl+a`).

Creating and Managing Sessions



- Detach from a session:
```bash
Ctrl+a d
```
This command allows you to detach from your current session and return to the terminal.

- Reattach to a session:
```bash
screen -r session_name
```
If you have multiple sessions, you can list them using:
```bash
screen -ls
```

- Terminate a session:
```bash
exit
```
or simply type:
```bash
Ctrl+a k
```

Navigating Between Windows



- Create a new window:
```bash
Ctrl+a c
```

- Switch to the next window:
```bash
Ctrl+a n
```

- Switch to the previous window:
```bash
Ctrl+a p
```

- List all windows:
```bash
Ctrl+a "
```

- Close the current window:
```bash
Ctrl+a &
```

Advanced Features



Screen offers a variety of advanced features that enhance its usability, especially for power users.

Scrolling and Copying Text



- Enter copy mode:
```bash
Ctrl+a [
```

- Scroll up/down:
Use the arrow keys or `Page Up/Page Down`.

- Select text:
Move the cursor to the start of the text you want to copy, press `Space`, move to the end of the text, and then press `Space` again.

- Paste the copied text:
```bash
Ctrl+a ]
```

Session Management



- Rename the current window:
```bash
Ctrl+a A
```

- Send a command to all windows:
```bash
Ctrl+a :
```
Then type the command you want to send.

- Lock the screen:
```bash
Ctrl+a x
```

Customizing Screen



Screen can be customized through its configuration file, typically located at `~/.screenrc`. Here are some common customizations you can apply:

- Set default window title:
```bash
caption always "%{= kw}%-w%{= r} %n %t %{-} %-w%{= k} %="
```

- Enable mouse support:
```bash
mousesupport on
```

- Change scrollback buffer size:
```bash
defscrollback 10000
```

Useful Tips and Tricks



Here are some additional tips to enhance your experience with Screen:

- Log all output to a file:
You can log the output of a Screen session to a file by starting the session with:
```bash
screen -L
```

- Transfer files between windows:
To copy files between different Screen sessions, you can utilize the `screen -X` command:
```bash
screen -S session_name -X stuff "command_to_run"
```

- Use Screen with SSH:
When you SSH into a remote server, starting a Screen session can be beneficial. This way, if your connection drops, your processes will continue running.

- Detach and leave running processes:
Always remember to detach from your Screen session before closing your terminal. This ensures that processes continue running.

Troubleshooting Common Issues



While using Screen, you may encounter some common issues. Here are a few solutions:

- Cannot reattach to a session:
If you get the message that no screens are found, ensure that your session is still running with:
```bash
screen -ls
```

- Screen session not responding:
If Screen seems unresponsive, try sending a break signal:
```bash
Ctrl+a \
```

- Configuration not applying:
If changes in your `.screenrc` file are not taking effect, ensure you restart your Screen session or reload the configuration with:
```bash
Ctrl+a :source ~/.screenrc
```

Conclusion



The Screen Linux Cheat Sheet encapsulates the essential commands and functionalities of GNU Screen, an invaluable tool for anyone who frequently works in the terminal. With its ability to manage multiple sessions, detach and reattach sessions, and facilitate remote work, Screen significantly enhances productivity. By mastering the commands outlined in this cheat sheet, users can navigate and utilize Screen effectively, whether for personal projects or professional tasks. Embrace the power of Screen, and optimize your terminal experience today!

Frequently Asked Questions


What is the purpose of the 'screen' command in Linux?

The 'screen' command allows users to manage multiple terminal sessions from a single SSH session, enabling session persistence and multitasking.

How do you start a new screen session?

You can start a new screen session by typing 'screen' in the terminal.

What keyboard shortcut is used to detach from a screen session?

To detach from a screen session, press 'Ctrl + A' followed by 'D'.

How can you reattach to a detached screen session?

To reattach to a detached screen session, use the command 'screen -r'.

What command can you use to list all running screen sessions?

Use the command 'screen -ls' to list all running screen sessions.

How do you name a screen session when starting it?

You can name a screen session by using the command 'screen -S session_name' when starting it.

What is the command to kill a specific screen session?

You can kill a specific screen session using 'screen -X -S session_name quit'.

How do you create a new window within an existing screen session?

To create a new window within an existing screen session, press 'Ctrl + A' followed by 'C'.

What command is used to split the screen into multiple regions?

Use the command 'Ctrl + A' followed by 'S' to split the screen horizontally, and 'Ctrl + A' followed by '|' to split it vertically.