I'd like to configure SSH and screen such that a login will always reattach to a screen session. Ideally, that session would not quit but detach when I press C-d. How can this be achieved? And what other useful settings are there to make my SSH-life easier?
I just did the following for all of my servers so that when I connect via SSH I am automatically put into a Screen session.
Add the following to the ~/.bashrc for your user accounts:
This will start a screen session named main if it doesn't exist or reconnect to it if it does. This was done because I have several other screen sessions running detached for various services and didn't want to connect to them.
I have the following in my .bash_profile on my remote servers:
This does two things: first, sets up a shell function to replace the shell and carry forward the ssh-agent connection, second it calls the function after pausing for a few seconds (in case you don't want screen to start).
The last part first removes any dead sessions (
First, if you use byobu you can configure a new shell session to always automatically start byobu (which is essentially a nice screen configuration, although newer versions may use tmux as the backend). If you really don't want byobu then you can probably set screen to connect manually by editing your
If you don't like that, you can use the
As for disconnect on Ctrl-D, screen allows you to set up key mappings in your
Maybe totally irrelevant to your question, but have you ever considered using TMUX instead. It does a lot of functionality that you are looking for out of the box.
Eg. if you disconnect from an SSH session when you are under TMUX, all you need to do is to ssh back and run "tmux a" to attach to your session. Since I started using TMUX, I forgot what nohup/disown commands are... if you need to run something and logout - run it inside tmux and detach. The command output will be buffered, so you can review it later.
to attach automatically you can add something like below to .bashrc:
or even easier
It has lower memory footprint and overall IMHO much better/easier to use option.
The meta-d shortcut is the default mapping to detach in TMUX.
Briefly explaining the switches:
If the first screen command (the one before