Linux screen is a tool used to multiplex single ssh terminal easily. Screen has a strong competitor named tmux where we will look next posts. Screen has these abilities to make system administrators life easy.
- Multiple screens in a single ssh connection
- Running commands after ssh disconnect
- Resuming screen sessions after ssh disconnect
- Easily dividing and multiplexing single window
Installing screen is easy like below.
$ sudo apt install screen
$ sudo yum install screen
To get more detailed help about screen command use man screen command like below
$ man screen
Starting Linux Screen
Screen can be started with simple screen command like below. This will create new shell same as default shell.
After issuing screen command we will see a notice about shell we will start like below. This page provides summary information about screen shell where we look more details later this post.
Press Space to start shell
This is a screen shell which is the same as bash shell
Check If Screen Whether Running
While working a lot of servers we may need to check if the screen is running. to get status of current shell bash environment variables provides infomraion about terminal with TERM variable. We can see current terminal from here.
We can see from result that current shell is screen and color support is 256 color with xterm .
Screen provides a lot of features. to activate these fatures some keyboard short cuts used. We call these shortcuts as screen commands. To send command to the screen there is a command named control command. Control command simple specifies next command is about screen. Default control command is Ctrl+a
Create New Window
One of the most used feature of screen is creating new windows. As we stated before screen can creates more than one windows with single terminal which is very useful for system administrator. Shortcut for creating new window is
First press control command which is by default Ctrl+a and after press c .This will create a new window with shell like below.
Switch Next Window
In a typical screen usage there will be more than one windows so how can we switch between these windows. In order to switch next window use
Switch Previous Window
Another navigation shortcut for screen is switching previous window. Switching between windows do not end process or close current window.
Detaching From Screen
Other very popular usage of screen is detaching from screen. Think about these scenario. We are copying some big files but have go out by closing ssh session. In this situation we can detach current screen session and then reattach.
List Existing Sessions
To reattach screen sessions we may need to list current existing sessions. Screen provides -list option to list these detached session with the date time information and session name.
$ screen -list
List Screen Process
We can look for existing screen sessions in a server. Screen runs as a process like a bash session.
$ ps aux | grep screen
As we see processes id 12336 is our current screen terminal
We will reattach screen session after connection our server with ssh.
$ screen -r
As we see if there is only one screen session to reattach it will automatically reattached but there are more than one screen session so we should specify which one we want to reattach
$ screen -r 12587.pts-12.ubu1
Save Terminal Commands
While working in remote systems we will issue a lot of commands. Remembering these commands can be hard some times especially if we try a lot of things to solve a problem. Screen provides logging commands of running sessions. After a session ends we can look commands issued at this session. To enable command history use following command.
After issuing shortcuts a logfile named screenlog.0 is created.
Read History Log
History log files by default named screenlog.id . They are simple text files and can be read with cat like below.
$ cat screenlog.0
Disable Saving Terminals
After enabling terminal logging we disable it with the same command for enabling like below.
While working multliple windows we can run some command and switch to the next windows. How can we notified if previous windows command is completed? Screen has bility to monitor windows and provides notifications if the commands have finished.
Simply issuing same command t enable monitor will disable monitorig for the current window.
Lock Screen Session
Screen session have the ability to lock which is very similar like desktop managers screen lock. To unlock screen current users system password should be entered.
Stopping or Exit Screen
The best practice screen after usage is closing the screen session with the shortcut below. This will close all windows of current session.
This will ask question “Really kill this window [y/n]” press y to kill session