4

I auto start gnu screen when I ssh into one on my own servers. When I am done, I have to d to detach. Then exit to quit ssh. This means that when I ssh into the server again at some point in time, I can continue where I left off.

Can I create a alias (for instance) that will (from within screen) detach and drop ssh?

  • How do you auto-start screen – roaima Jul 1 '18 at 20:28
3

Starting screen when logging in on the remote host (executed on your local machine):

ssh -t user@host screen

In the screen session, on the remote host, to detach and log out (which would end the SSH session unless you are using a persistent connection using a control socket (i.e. using the ControlMaster setting with ControlPersist in ~/.ssh/config)):

screen -d

Later, to log in and re-attach:

ssh -t user@host screen -r

Note that you have to use -t with ssh, forcing pseudo-terminal allocation, to start screen in this way.

The alias on the remote host would be for screen -d, maybe detach='screen -d' or something similar.

On the local host, you could have a section in your ~/.ssh/config file saying

Host *-screen
    RequestTTY force
    RemoteCommand screen -d -R

Host myhost myhost-screen
    HostName myhost.somedomain.example.com
    User myname

Then, whenever you use

ssh myhost-screen

a screen session would be created (or re-attached to) on the specified host, while

ssh myhost

would not use screen.

Note: RemoteCommand was added in OpenSSH 7.6 (2017-10-03).


For tmux:

  • Use tmux new-session -A -s "%n" in ~/.ssh/config instead of screen -d -R.
  • Use prefixd or tmux detach-client or tmux detach to detach.
  • Nice, thx. At home this will work great. I did not know about 'Force pseudo-tty allocation'. I tested this and it works. – Vissie Jul 2 '18 at 6:05
1

When you execute your screen command to recall or create your screen , After you add a a kill of you parent shell , so you will kill you ssh connection .

a example to kill my parent process if the parent is bash

kill $( ps -ax -o pid,ppid,args | grep "^$$ " |  grep bash | awk '{print $2} ' )
  • Thx. If I ssh in from somewhere where I can not specify a app to run (like online ssh app.shellngn.com) , and I modify your line of code to look for ssh sessions, this will work. – Vissie Jul 2 '18 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.