Used for command line terminal switching
Screen is a free software developed by the GNU Project for command line terminal switching. Users can use this software to connect to multiple local or remote command line sessions at the same time and switch between them freely. GNU Screen can be thought of as the command line interface version of a window manager. It provides a unified interface and corresponding functions for managing multiple sessions.
Session Resume
As long as the Screen itself is not terminated, sessions running within it can be resumed. This is especially useful for users logging in remotely - even if the network connection is interrupted, the user will not lose control of the command line session they have opened. Just log in to the host again and execute screen -r to resume the session. Also when leaving temporarily, you can also execute the detach command to suspend the Screen (switch to the background) while ensuring that the programs inside are running normally. This is very similar to VNC under the graphical interface.
Multi-window
In the Screen environment, all sessions run independently and have their own numbers, input, output and window buffers. Users can switch between different windows through shortcut keys, and can freely redirect the input and output of each window. Screen implements basic text operations, such as copy and paste, etc.; it also provides scroll bar-like functions to view the history of window status. Windows can also be partitioned and named, and background window activity can be monitored. Session Sharing Screen allows one or more users to log in to a session multiple times from different terminals and share all features of the session (for example, they can see exactly the same output). It also provides a mechanism for window access permissions and can password-protect windows.
GNU's Screen official site: http://www.gnu.org/software/screen/
# screen -AmRvx -[ls -wipe][-d <job name>][-h <number of lines>][-r <job name>][-s ][-S <job name>]
-A Adjusts all windows to the size of the current terminal.
-d <job name> Take the specified screen job offline.
-h <number of lines> Specifies the number of buffer lines in the window.
-m Forcibly create a new screen job even if the screen job is currently in operation.
-r <job name> Resume offline screen jobs.
-R Try to resume offline jobs first. If no offline job is found, a new screen job is created.
-s specifies the shell to be executed when creating a new window.
-S <job name> Specifies the name of the screen job.
-v Display version information.
-x Restore previously offline screen jobs.
-ls or --list Display all current screen jobs.
-wipe Check all current screen jobs and delete screen jobs that are no longer available.
screen -S yourname -> Create a new session called yourname
screen -ls -> List all current sessions
screen -r yourname -> Return to the session yourname
screen -d yourname -> Remote detach a session
screen -d -r yourname -> End the current session and return to the session yourname
Under each screen session, all commands start with ctrl+a(C-a).
C-a ? -> Show all keybinding information
C-a c -> Create a new window running the shell and switch to that window
C-a n -> Next, switch to the next window
C-a p -> Previous, switch to the previous window
C-a 0..9 -> switch to window 0..9
Ctrl+a [Space] -> Switch sequentially from window 0 to window 9
C-a C-a -> Switch between the two most recently used windows
C-a x -> Lock the current window and need to use user password to unlock it
C-a d -> detach, temporarily leave the current session, throw the current screen session (which may contain multiple windows) to the background for execution, and return to the state before entering the screen. At this time, in the screen session, each window The processes running inside (whether in the foreground/background) continue to execute, even if the logout occurs, it will not be affected.
C-a z -> Put the current session in the background for execution, and use the shell's fg command to go back.
C-a w -> show list of all windows
C-a t -> time, displays the current time and system load
C-a k -> kill window, forcefully close the current window
C-a -> Enter copy mode. In copy mode, you can rollback, search, and copy just like using [vi.
C-b Backward, PageUp
C-f Forward,PageDown
H (capital) High, move the cursor to the upper left corner
L Low, move the cursor to the lower left corner
0 moves to the beginning of the line
$ end of line
w forward one word, move forward in units of words
b backward one word, move backward in units of words
The first press of Space marks the starting point of the area, and the second press marks the end point.
Esc ends copy mode
C-a ] -> paste, paste the content just selected in copy mode
Install screen
Popular Linux distributions (such as Red Hat Enterprise Linux) usually come with the screen utility. If not, you can download it from the official GNU screen website.
[root@TS-DEV ~]# yum install screen
[root@TS-DEV ~]# rpm -qa|grep screen
screen-4.0.3-4.el5
[root@TS-DEV ~]#
Create a new window
After the installation is complete, just type the command screen to start it. However, the screen session started in this way does not have a name. In practice, it is recommended to give each screen session a name to facilitate identification:
[root@TS-DEV ~]# screen -S david
After screen starts, it will create the first window, which is window No. 0, and open a system default shell in it, usually bash. So after you type the command screen, you will immediately return to the command prompt, as if nothing happened. In fact, you have entered the world of Screen. Of course, you can also add your favorite parameters after the screen command to directly open the program you specify, for example:
[root@TS-DEV ~]# screen vi david.txt
screen creates a single-window session that executes vi david.txt. Exiting vi will exit the window/session.
View window and window name
After opening multiple windows, you can use the shortcut key C-a w to list all current windows. If you are using a text terminal, this list will be listed in the lower left corner of the screen. If you are using a terminal emulator under the X environment, this list will be listed in the title bar. The window list generally looks like this:
0$ bash 1-$ bash 2*$ bash
In this example, I opened three windows. The * sign indicates that I am currently in window 2, and the - sign indicates that I was in window 1 when I last switched windows.
Screen will name the window by default as a combination of the number and the name of the program running in the window. In the above example, the windows are all default names. After practicing the above method of viewing windows, you may want to have different names for each window to make it easier to distinguish them. You can use the shortcut key C-a A to rename the current window. After pressing the shortcut key, Screen will allow you to enter a new name for the current window and press Enter to confirm.
Session Detachment and Recovery
You can temporarily disconnect (detach) the screen session without interrupting the running of the program in the screen window, and reconnect (attach) the session at a later time to regain control of the programs running in each window. For example, we open a screen window to edit the /tmp/david.txt file:
[root@TS-DEV ~]# screen vi /tmp/david.txt
After that, if we want to temporarily quit and do something else, such as going for a walk, then type C-a d in the screen window, and Screen will give a detached prompt:
Temporarily interrupt session
I came back half an hour later and found this screen session:
[root@TS-DEV ~]# screen -ls
Reconnect session:
[root@TS-DEV ~]# screen -r 12865
Everything is there.
Of course, if you do not detach a Screen session on another machine, there is no way to restore the session. At this time, you can use the following command to force the session to be separated from the terminal where it is located and transferred to a new terminal:
Clear dead session
If one of the sessions dies for some reason (for example, the session is killed manually), screen -list will show that the session is in a dead state. Use the screen -wipe command to clear the session:
Close or kill a Screen session
Under normal circumstances, when you exit the last program in a window (usually bash), the window is closed. Another way to close a window is to use the ctrl
+a
keys, then press the k
key, and finally press the y
key when prompted whether you want to kill the session. This shortcut will kill the current session. window, and will also kill the process running in this window.
If the last window in a Screen session is closed, the entire Screen session will exit and the screen process will be terminated.
In addition to exiting/killing all windows in the current Screen session in sequence, you can also use the shortcut key C-a: and then enter the quit command to exit the Screen session. Note that exiting in this way will kill all windows and exit any programs running in them. In fact, C-a: This shortcut key allows users to directly enter many commands, including split screen input, etc. This is also a way to implement the Screen function, but I personally think that the shortcut key is more convenient.
In addition, here is another command to quickly kill a Screen session:
[root@TS-DEV ~]# screen -ls #List existing sessions
[root@TS-DEV ~]# screen -XS "session id or name" quit
Example:
[root@TS-DEV ~]# screen -ls
There are screens on:
11235.test (01/25/2021 03:35:31 PM) (Detached)
1 Sockets in /run/screen/S-root.
[root@TS-DEV ~]# screen -XS 11235 quit
#or
[root@TS-DEV ~]# screen -XS test quit
Session Sharing
There is also a more interesting session recovery, which can realize session sharing. Suppose you and a friend are logged into a machine in different locations as the same user, and then you create a screen session, and your friend can command on his terminal:
[root@TS-DEV ~]# screen -x
This command will attach your friend's terminal to your Screen session, and your terminal will not be detached. In this way, you can share the same session with your friends. If you are currently in the same window, it is equivalent to sitting in front of the same monitor. Your operations will be demonstrated to your friends synchronously, and your friends' operations will also be demonstrated synchronously. for you. Of course, if you switch to different windows of this session, you can still perform different operations separately.
Session Lock and Unlock
Screen allows locking the session using the shortcut C-a s. After locking, the screen will no longer respond to any further input. But please note that although no response is visible on the screen, your input will be received by the process in the Screen. The shortcut key C-a q can unlock a session.
也可以使用C-a x lock the session. The difference is that after locking, the session will be protected by the password of the user who owns the Screen. You need to enter the password to continue accessing the session.
Send command to screen session
Outside of a Screen session, you can operate a Screen session through the screen command, which also adds convenience to using Screen as a script program. The application of Screen in scripts is beyond the scope of entry. Here is just an example to experience the operation of Screen outside the session:
[root@TS-DEV ~]# screen -S sandy -X screen ping www.baidu.com
This command creates a new window in a screen session called sandy and runs the ping command in it.
Screen Split
Now that the monitor is so big, it is obviously a cool thing to split a screen into different areas to display different Screen windows. You can use the shortcut key C-a S to split the display horizontally. Screen 4.00.03 and later also supports vertical splitting. The shortcut key is C-a |. After splitting the screen, you can use C-a
You can use the C-a The window in the closed block will not be closed, and it can also be found through window switching.
C/P Mode and Operation
Another very powerful feature of screen is the ability to copy and paste between different windows. Use the shortcut key C-a
In general, you can move the cursor to the specified position, press space to set a beginning mark, then move the cursor to the end position, press space to set the second mark, and store the part between the two marks in copy/paste buffer and exit copy/paste mode. In normal mode, you can use the shortcut key C-a ] to paste the content stored in the buffer into the current window.
More screen features
Like most UNIX programs, GNU Screen provides rich and powerful customization functions. You can specify more in Screen's default two-level configuration files /etc/screenrc and $HOME/.screenrc, such as setting screen options, customizing key bindings, setting the screen session self-starting window, enabling multi-user mode, and customizing User access rights control, etc. If you wish, you can also specify the screen configuration file yourself.
Taking the multi-user function as an example, screen runs in single-user mode by default. You need to specify multiuser on in the configuration file to turn on multi-user mode. Through the acl* (acladd, acldel, aclchg...) command, you can flexibly Configure other users to access your screen session. For more configuration file content, please refer to screen's man page.