The screen command in Linux allows users to manage multiple terminal sessions within a single window, facilitating remote access and session persistence.
Using Screen
-
Start a new screen session:
screen -
Detach from the current screen session: Press
Ctrl + A, thenCtrl + D. -
List existing screen sessions:
screen -ls -
Reattach to a detached screen session:
screen -r [session_id] -
Create a named screen session:
screen -S [session_name] -
Reattach to a named screen session:
screen -r [session_name] -
Switch between multiple windows within a screen session: Press
Ctrl + A, thennfor next window orpfor previous window. -
Split the screen horizontally: Press
Ctrl + A, thenS. -
Split the screen vertically: Press
Ctrl + A, then|. -
Navigate between split screens: Press
Ctrl + A, thenTab. -
Open shell on new screen split: Press
Ctrl + A, thenc. -
Close the current window or screen: Press
Ctrl + A, thenk. -
Exit the screen session: Inside the screen session, type
exit. -
Run a command in a new screen session:
screen -dmS [session_name] [command] -
Send a command to a running screen session:
screen -S [session_name] -X stuff '[command]\n' -
Attach to a running screen session and run a command:
screen -S [session_name] -X stuff '[command]\n' -
Attach to a running screen session that already says Attached (for instance if your previous ssh connection failed):
screen -rd
Note: Replace [session_id], [session_name], and [command] with the actual session ID, session name, and command, respectively.