Using screen

What is screen

screen is a free software terminal multiplexer developed by the GNU Project.

It has many features, but here I’ll focus on one particularly useful for remote server management:

Session Recovery

When you connect to a remote server and start a terminal session with screen, as long as screen itself hasn’t terminated, all sessions running inside it can be recovered. This effectively solves issues like session hang/disconnection caused by network problems when remotely managing Linux servers.

Usage

After installation on the remote server, run the following command to create a session named sessionName:

Terminal window
screen -S sessionName

Common screen parameters:

Terminal window
screen -S sessionName -> Create a new session named sessionName
screen -ls -> List all current sessions
screen -r sessionName -> Reattach to the session named sessionName
screen -d sessionName -> Remote detach a session
screen -d -r sessionName -> End current session and reattach to sessionName

Session Status: Attached

When you detach from screen and later try to reconnect, sometimes the screen session status remains “Attached” and you simply can’t reconnect.

List the session ID with Attached status:

Terminal window
screen -ls
screen -D -r <session-id>

Note: -D -r forcefully kicks out the previous user, then logs in.

More

Official site: https://www.gnu.org/software/screen/