Here we will show most frequent usage of screen command in Linux and that is…how to start a screen, how to give a name for a screen session, how to attach a terminal to “disconnected” screen session, how to list screen sessions and similar things.
Also, the screen is a good solution if you need to run multiple terminal sessions, for example – from one screen to edit a bash script – and from another one to run the same script (ok, it is a little bit stupid example but you know what I want to say)
How to start а screen with some generic name
How to start a screen session with some normal descriptive name, like a “Pinging Google”
screen -S "Pinging Google" # -S: "Session name"
How to list all active sessions of а screen
screen -ls # -ls: list sessions
By default, you will see only your own sessions with this command, so if user Draza started own screen session, even if you are root – you will not be able to see him. If you want to list all screen sessions – you can do that by listing a /var/run/screen folder as a root (non-root user will see only own sessions)
ls -l /var/run/screen/*
How to reattach a screen session
screen -r "Pinging Google" # -r: resumes a detached screen session
If you need to attach your session to already attached screen with r (resume a detached screen session) use a “-d” switch which will first detach session elsewhere
screen -r -d 1365 # -r: resumes a detached screen session # -d: detach session elsewhere
How to kill а screen session
If you want to kill screen session you can do it while you are logged in a screen with simple “exit” command, like when you exiting from normal terminal windows. If you want to kill screen session without login to session – you can do that with a command:
screen -X -S "Pinging Google" quit # or screen -X -S 1379 quit # -X: Send the specified command to a running screen session # -S: Session name # quit: kill all and terminate screen session
Also, when you are in screen session – you can exit from her with a simple “exit” command.
Most used hotkeys for me are:
CTRL+a+d which will detach screen session,
CTRL+a+n for next screen session, and
CTRL+a+c for a new screen session.
For more details about the screen, please use man page or go to screen homepage here.