ThisCalculator

Everyday

Install and Use GNU Screen on Ubuntu VPS

Updated · Standard formula, shown below

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 ubuntu user on most VPS images works).
  • Basic familiarity with the command line.

Step-by-Step

  1. 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.
  2. Update your package list. Run sudo apt update to refresh the repository index. This ensures you get the latest version of Screen.
  3. Install Screen. Run sudo apt install screen -y. The -y flag automatically confirms the installation. Wait for the process to finish.
  4. Verify the installation. Type screen --version. You should see output like Screen version 4.09.00 (the exact number may vary). If not, re-run the install command.
  5. Start a new Screen session. Simply type screen and press Enter. You’ll see a welcome message; press Space or Enter to continue. You’re now inside a new shell that Screen manages.
  6. Run a long command. For example, start a process that takes time: ping google.com or sudo apt upgrade. This will run in the foreground of your Screen session.
  7. Detach from the session. Press Ctrl+A then D (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.
  8. Reattach to the session. Type screen -r to reattach to the most recent detached session. If you have multiple sessions, you’ll see a list; use screen -r session_id to attach to a specific one.
  9. Create multiple windows. Inside a Screen session, press Ctrl+A then C to create a new window (like a new terminal). You can switch between windows with Ctrl+A then N (next) or Ctrl+A then P (previous).
  10. List all sessions. From outside any session, run screen -ls to see all active Screen sessions with their IDs and status.
  11. Kill a session. To end a session completely, reattach to it and type exit or press Ctrl+D in the shell. Alternatively, from outside, run screen -X -S session_id quit.
  12. Use named sessions (optional). Start a session with a name: screen -S mytask. Then reattach with screen -r mytask. This helps when you have multiple sessions.

Common Mistakes

  • Forgetting the Ctrl+A prefix. Many shortcuts require pressing Ctrl+A first. If you press just D, you’ll type a ‘d’ in the terminal, not detach.
  • Using screen -r when no session exists. If you get an error like “No Sockets found”, you need to start a new session first with screen.
  • Detaching instead of exiting. When you’re done with a session, use exit to close it. If you only detach, the session stays in memory, consuming resources.
  • Running screen as 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 -ls to see if you already have a session you meant to reattach to.

Tips

  • Scrollback buffer. To scroll up in a Screen window, press Ctrl+A then Esc, use arrow keys, and press Esc again to exit copy mode.
  • Split screen. You can split the terminal horizontally with Ctrl+A then S, switch to the new region with Ctrl+A then Tab, and create a new window there with Ctrl+A then C. Remove splits with Ctrl+A then Q.
  • Lock the screen. Press Ctrl+A then X to lock the terminal with a password (uses your system password).
  • Use a config file. Create ~/.screenrc to set options like a status bar: hardstatus alwayslastline and hardstatus 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 -r to pick up where you left off.

Troubleshooting

  • Screen not found after install. If screen --version fails, the install may have failed. Run sudo apt install screen again 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 with screen -d -r session_id.
  • Screen session dies when SSH disconnects. This usually means you didn’t detach properly. Always use Ctrl+A then D to detach before closing the terminal. If you lost connection abruptly, the session should still be running—check with screen -ls.
  • Ctrl+A not working. Some terminals or SSH clients intercept Ctrl+A. Try pressing Ctrl+A twice quickly, or use Ctrl+A then A to send a literal Ctrl+A. Alternatively, you can remap the escape key in ~/.screenrc with escape ^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=xterm before starting Screen, or install the ncurses-term package: sudo apt install ncurses-term.
  • Multiple sessions and you don’t know which to attach to. Run screen -ls to see the list. Each session has a unique ID (e.g., 12345.pts-0.ubuntu). Use screen -r 12345 to attach to that specific one.

Frequently Asked Questions

  1. 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.
  2. 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 screen before installing.
  3. How do I exit a Screen session without killing the process? Detach with Ctrl+A then D. The session and its processes continue running in the background.
  4. Can I share a Screen session with another user? Yes, by setting the session to multiuser mode. Inside the session, type Ctrl+A then : and enter multiuser on, then add users with acladd username. The other user can then attach with screen -x.
  5. 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+A then : and set scrollback 500 to limit history.
  6. 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 tmux with 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.