Skip to content

Instantly share code, notes, and snippets.

@benjaminxscott
Last active July 2, 2025 20:32
Show Gist options
  • Save benjaminxscott/3e4fbd0e53d8db8adebb015d325da7e1 to your computer and use it in GitHub Desktop.
Save benjaminxscott/3e4fbd0e53d8db8adebb015d325da7e1 to your computer and use it in GitHub Desktop.
Using screen via SSH to keep sessions alive

Ref: https://gist.github.com/jctosta/af918e1618682638aa82

Install screen on remote

apt-get install screen

(optional) For Azure, use az cli to write a local SSH configfor your user to use SSO creds

az ssh config --ip 1.2.3.4 --file az_ssh_config

This will automatically use your (ephemeral) Azure keypair generated above for SSH connections. You will have to regenerate those based on their expiry

Launch screen session on remote and connect via SSH

ssh -t -A $hostname screen -R

This will create a new screen session or reattach to the most recent screen

This command can be run multiple times and do what you expect (i.e. it's idempotent)

(optional) Use -S <session_name> to set a name for your session, then use -r <session_name> to attach to that specific session. This is useful when pair-programming and collaborating on the same box, as screen supports multi-user mode

Commands for Screen (keyboard shortcuts)

Hit Ctrl+A then press a key to send a command to screen (typically a single char)

  • d = detach from screen (it will continue running)
  • c = Create new window in same screen session
  • Ctrl+A again - Swap to prior window
  • [#] to switch to window #
  • " = Choose window from list
  • k = kill window (or Ctrl +D or exit)

List all screen sessions on the remote

ssh -t -A $hostname screen -ls

Comments are disabled for this gist.