Linux Screen

The short version that you need, the rest is nice to know but this is your bread and butter.

screen (starts a new session) this is once off until you destroy it or server/system restart.

CTRL+A (then release both) then press D will put the session into the back.

screen -r (will reconnect you to that session)

(i recommend you add an alias to your bashrc. alias sc=’screen -r’ (then simply type sc on login and you are set. If session is found it will put you right into it)


Obviously : first install screen on your server / system. Ubuntu – sudo apt install screen

Once install on your system, get used to doing the below or above directly after logging in to the system.


Things to remember CTRL-D will close/destroy/terminate the screen session, not put it into the back. You need to get used to not hitting this to leave a system quickly. CTRL+A then D is how to put the screen session into the back. CTRL+A then release and press D. this will become your new i am ending my server session go to.

Type : screen hit enter and you have a session going.

Every time you log in to your system/server simply type : screen -r

And it will auto connect you right back into where you where. Mid script, what ever, no more ip resets taking you back to unwritten files.

This screen -r will become your best friend very quickly once you put it on top of your logged in command list.

Every now an then type screen -list just to make sure you dont have a bunch of sessions you might have left open. typing screen and hitting enter will create a new session not connect you to one thats already running in the back. It is easy to get multiple session without realizing it.

Starting Linux Screen

To start a screen session, simply type screen in your console:

screen

This will open a screen session, create a new window, and start a shell in that window.

Now that you have opened a screen session, you can get a list of commands by typing:

Ctrl+a ? / Press CTRL(hold it)+A release CTRL and A, then press ? this is for all the CTRL+A options.

Starting Named Session

Named sessions are useful when you run multiple screen sessions. To create a named session, run the screen command with the following arguments:

screen -S session_name

It’s always a good idea to choose a descriptive session name.

Working with Linux Screen Windows

When you start a new screen session, it creates a single window with a shell in it.

You can have multiple windows inside a Screen session.

To create a new window with shell type Ctrl+a c, the first available number from the range 0...9 will be assigned to it.

Below are some most common commands for managing Linux Screen Windows:

  • Ctrl+a c Create a new window (with shell).
  • Ctrl+a " List all windows.
  • Ctrl+a 0 Switch to window 0 (by number).
  • Ctrl+a A Rename the current window.
  • Ctrl+a S Split current region horizontally into two regions.
  • Ctrl+a | Split current region vertically into two regions.
  • Ctrl+a tab Switch the input focus to the next region.
  • Ctrl+a Ctrl+a Toggle between the current and previous windows
  • Ctrl+a Q Close all regions but the current one.
  • Ctrl+a X Close the current region.

Detach from Linux Screen Session

You can detach from the screen session at any time by typing:

Ctrl+a dThe program running in the screen session will continue to run after you detach from the session.

Reattach to a Linux Screen

To resume your screen session use the following command:

screen -r

In case you have multiple screen sessions running on your machine, you will need to append the screen session ID after the r switch.

To find the session ID list the current running screen sessions with:

screen -ls
There are screens on:
    10835.pts-0.linuxize-desktop   (Detached)
    10366.pts-0.linuxize-desktop   (Detached)
2 Sockets in /run/screens/S-linuxize.

If you want to restore screen 10835.pts-0, then type the following command:

screen -r 10835

Customize Linux Screen

When screen is started, it reads its configuration parameters from /etc/screenrc and ~/.screenrc if the file is present. We can modify the default Screen settings according to our preferences using the .screenrc file.

Here is a sample ~/.screenrc configuration with customized status line and few additional options:

~/.screenrc

# Turn off the welcome message
startup_message off

# Disable visual bell
vbell off

# Set scrollback buffer to 10000
defscrollback 10000

# Customize the status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'

Basic Linux Screen Usage #

Below are the most basic steps for getting started with screen:

  1. On the command prompt, type screen.
  2. Run the desired program.
  3. Use the key sequence Ctrl-a + Ctrl-d to detach from the screen session.
  4. Reattach to the screen session by typing screen -r.