Install and Use GNU Screen on Ubuntu VPS
GNU Screen is a terminal multiplexer that lets you run multiple sessions inside a single SSH connection, keep processes alive after you disconnect, and reattach to them later. This guide walks you through installing Screen on an Ubuntu VPS and using its core features—detaching, reattaching, and managing multiple windows—so you can run long tasks without keeping your laptop open.
What You’ll Need
- An Ubuntu VPS (any recent version, e.g., 20.04, 22.04, or 24.04) with SSH access.
- A user account with sudo privileges (the default
ubuntuuser on most VPS images works). - Basic familiarity with the command line.
Step-by-Step
- Connect to your VPS via SSH. Open a terminal on your local machine and run:
ssh username@your_server_ip. Enter your password or use your SSH key. - Update your package list. Run
sudo apt updateto refresh the repository index. This ensures you get the latest version of Screen. - Install Screen. Run
sudo apt install screen -y. The-yflag automatically confirms the installation. Wait for the process to finish. - Verify the installation. Type
screen --version. You should see output likeScreen version 4.09.00(the exact number may vary). If not, re-run the install command. - Start a new Screen session. Simply type
screenand press Enter. You’ll see a welcome message; press Space or Enter to continue. You’re now inside a new shell that Screen manages. - Run a long command. For example, start a process that takes time:
ping google.comorsudo apt upgrade. This will run in the foreground of your Screen session. - Detach from the session. Press
Ctrl+AthenD(release both, then press D). You’ll return to your normal SSH prompt with a message like[detached from 12345.pts-0.ubuntu]. The process keeps running in the background. - Reattach to the session. Type
screen -rto reattach to the most recent detached session. If you have multiple sessions, you’ll see a list; usescreen -r session_idto attach to a specific one. - Create multiple windows. Inside a Screen session, press
Ctrl+AthenCto create a new window (like a new terminal). You can switch between windows withCtrl+AthenN(next) orCtrl+AthenP(previous). - List all sessions. From outside any session, run
screen -lsto see all active Screen sessions with their IDs and status. - Kill a session. To end a session completely, reattach to it and type
exitor pressCtrl+Din the shell. Alternatively, from outside, runscreen -X -S session_id quit. - Use named sessions (optional). Start a session with a name:
screen -S mytask. Then reattach withscreen -r mytask. This helps when you have multiple sessions.
Common Mistakes
- Forgetting the Ctrl+A prefix. Many shortcuts require pressing
Ctrl+Afirst. If you press justD, you’ll type a ‘d’ in the terminal, not detach. - Using
screen -rwhen no session exists. If you get an error like “No Sockets found”, you need to start a new session first withscreen. - Detaching instead of exiting. When you’re done with a session, use
exitto close it. If you only detach, the session stays in memory, consuming resources. - Running
screenas root unnecessarily. Use your normal user; you can sudo inside Screen if needed. Running everything as root can lead to permission issues later. - Not checking for existing sessions. Before starting a new one, run
screen -lsto see if you already have a session you meant to reattach to.
Tips
- Scrollback buffer. To scroll up in a Screen window, press
Ctrl+AthenEsc, use arrow keys, and pressEscagain to exit copy mode. - Split screen. You can split the terminal horizontally with
Ctrl+AthenS, switch to the new region withCtrl+AthenTab, and create a new window there withCtrl+AthenC. Remove splits withCtrl+AthenQ. - Lock the screen. Press
Ctrl+AthenXto lock the terminal with a password (uses your system password). - Use a config file. Create
~/.screenrcto set options like a status bar:hardstatus alwayslastlineandhardstatus string '%{= KW} %w'shows window titles at the bottom. - Reattach even if detached. If you lose your SSH connection, the session stays. Just reconnect and run
screen -rto pick up where you left off.
Troubleshooting
- Screen not found after install. If
screen --versionfails, the install may have failed. Runsudo apt install screenagain and watch for errors. Ensure you have network connectivity. - Cannot reattach: “There is no screen to be resumed.” This means no detached session exists. Check with
screen -ls. If you see a session marked “attached”, it means it’s still connected to another SSH session—either reconnect to that SSH session or force detach withscreen -d -r session_id. - Screen session dies when SSH disconnects. This usually means you didn’t detach properly. Always use
Ctrl+AthenDto detach before closing the terminal. If you lost connection abruptly, the session should still be running—check withscreen -ls. - Ctrl+A not working. Some terminals or SSH clients intercept Ctrl+A. Try pressing
Ctrl+Atwice quickly, or useCtrl+AthenAto send a literal Ctrl+A. Alternatively, you can remap the escape key in~/.screenrcwithescape ^Z^Z(then use Ctrl+Z instead). - Screen shows “Terminal type ‘xterm’ is unknown” or similar. This is a TERM environment issue. Try
export TERM=xtermbefore starting Screen, or install thencurses-termpackage:sudo apt install ncurses-term. - Multiple sessions and you don’t know which to attach to. Run
screen -lsto see the list. Each session has a unique ID (e.g.,12345.pts-0.ubuntu). Usescreen -r 12345to attach to that specific one.
Frequently Asked Questions
- What is the difference between Screen and tmux? Both are terminal multiplexers. Screen is older and simpler, while tmux offers more modern features like vertical splits and a better status bar. Screen is often pre-installed or easier to install on minimal systems.
- Can I run Screen without installing it? No, you need to install it. However, some minimal VPS images might already have it. Check with
which screenbefore installing. - How do I exit a Screen session without killing the process? Detach with
Ctrl+AthenD. The session and its processes continue running in the background. - Can I share a Screen session with another user? Yes, by setting the session to multiuser mode. Inside the session, type
Ctrl+Athen:and entermultiuser on, then add users withacladd username. The other user can then attach withscreen -x. - Does Screen work over a slow connection? Yes, but you might want to adjust the scrollback buffer or disable visual bells to reduce data usage. Use
Ctrl+Athen:and setscrollback 500to limit history. - What happens if the VPS reboots? All Screen sessions are lost because they are stored in RAM. To survive reboots, you’d need to use a tool like
tmuxwith a plugin, or set up your process as a systemd service.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.