Screen

From Indie IT Wiki
Revision as of 12:43, 27 August 2024 by Plittlefield (talk | contribs) (→‎HOWTO:)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

HOWTO:

Start Screen and Run a Command

Just type screen and then the command you want to run in the screen session ...

screen top

... then you can detach from that screen session in the normal way.

To re-attach to it, type ...

screen -list
screen -rd 256628.pts-0.paully-laptop

SSH and Attach and See Display and Detach

This is how to take a 'peek' at a running screen session...

ssh user@somehost "screen -x -X hardcopy /tmp/hardcopy.txt; cat /tmp/hardcopy.txt"

Thanks :-)

SSH and Attach

ssh -t server screen -rd

Start, Detach, Re-attach and Close

Start:

screen

Detach:

CTRL+A then D

Re-attach:

screen -rd

Close:

CTRL+D

Display Running Sessions

CTRL+A then W

Kill A Detached Screen Session

sudo screen -list
sudo screen -X -S [session # you want to kill] quit

Thanks to Stackoverflow.com

Delete A Screen Session

screen -ls
There are screens on:
       23487.pts-0.devxxx      (Detached)
       26727.pts-0.devxxx      (Attached)
2 Sockets in /tmp/uscreens/S-xxx.
screen -X -S 23487.pts-0.devxxx kill
screen -ls
There is a screen on:
       26727.pts-0.devxxx      (Attached)
1 Socket in /tmp/uscreens/S-xxx.

Log In And Attach To A Running Session On A Server

Log In > Switch To Root > Attach To Screen

ssh server > su - root > screen -rd

Log Out And Detach From A Running Session On A Server

Detach > Log Off Root > Log Out

CTRL+A then D > CTRL+D > CTRL+D

Connect To A Serial Port Device

I use this command to log in to a Cisco ASA5505...

screen /dev/ttyS0 9600,cs8

...then press Enter.

Thanks to Cyberciti.biz

HOWTO: CUSTOMISE:

The Perfect ~/.screenrc

  • stop the welcome nag screen
  • give you plenty of scrolling back room
  • make your mouse wheel work to scroll back
  • make command line auto-completion work
startup_message off
defscrollback 1000
termcapinfo xterm|xterms|xs|rxvt ti@:te@
defshell -bash

Add BASH Auto Completion

Add this to your ~/.screenrc file...

defshell -bash

Thanks to ServerFault.com

Add Hard Status Line On Terminal Title

Add the following line to your ~/.screenrc file...

hardstatus string "[screen %n%?: %t%?] %h"

Add A Custom Prompt #1

# Set up a decent prompt giving you the full hostname!
HOST=$( hostname -f )
if [[ $TERM =~ screen ]] ; then
    PS1="\[\033[01;31m\]\u@$HOST\[\033[01;34m\] \w \$\[\033[00m\] (screen) "
else
    PS1="\[\033[01;31m\]\u@$HOST\[\033[01;34m\] \w \$\[\033[00m\] "
fi

Thanks to Moretrix.com

Add A Custom Prompt #2

Edit your ~/.screenrc file and add the following line...

caption always "%{= bk} %-w%{bY}%n %t%{w}%+w %= [%H] %D %Y-%m-%d %c %A "

Add Scroll Back In A Screen Session

The Nice Way

Edit your ~/.screenrc and add the following lines...

defscrollback 5000
termcapinfo xterm* ti@:te@

Thanks to Slaptijack.com

The Not So Nice Way

CTRL+A [   (to enter copy mode)
PageUp or PageDown
ESC    (to leave copy mode)

Thanks to Saltycrane.com

Turn Welcome Message Off

Add the following line to your ~/.screenrc file...

startup_message off

HOWTO: FIX:

Mouse Scroll Wheel

UPDATE: May 2018 - XFCE has changed the 'name' of the terminal to 'xterm-256color' (found by typing 'env |grep term'), so you have to add that to the Screen setting file...

Add the following line to your ~/.screenrc file...

termcapinfo xterm|xterm-256color|xterms|xs|rxvt ti@:te@