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?

6 Answers 6


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:

# Auto-screen invocation. see: http://taint.org/wk/RemoteLoginAutoScreen
# if we're coming from a remote SSH connection, in an interactive session
# then automatically put us into a screen(1) session.   Only try once
# -- if $STARTED_SCREEN is set, don't try it again, to avoid looping
# if screen fails for some reason.
if [ "$PS1" != "" -a "${STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" != x ] 
  screen -RR -S main || echo "Screen failed! continuing with normal bash startup"
# [end of auto-screen snippet]

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 just wanted to add that the CTRL-d key combination is standard in screen to detach from a running screen session. Oct 2, 2012 at 15:37

I have the following in my .bash_profile on my remote servers:

if [ -z "${STY}" -a -t 0 -a X${USER} = Xarcege ]; then
    reattach() {
        if [ -n "${SSH_AUTH_SOCK}" ]; then
            ln -snf "${SSH_AUTH_SOCK}" "${HOME}/.ssh/agent-script"
            SSH_AUTH_SOCK="${HOME}/.ssh/agent-script" export SSH_AUTH_SOCK
        exec screen -A -D -RR ${1:+"$@"}
    screen -wipe
    echo 'starting screen... (type Ctrl-C to abort)'
    sleep 5 && reattach

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 first if clause will be true if not already running screen (-z "${STY}") is attached to a terminal (-t 0) and I'm not running sudo (which sometimes doesn't change $USER).

The reattach function will first check if ssh was called with an ssh-agent port active (-n "${SSH_AUTH_SOCK}"). Then it will replace whatever is at ~/.ssh/agent-script with the currently active socket file and replace the environment variable ($SSH_AUTH_SOCK) with the new value. Then the script will replace the current shell with a single screen session (making sure that only one should exist). Any arguments to the reattach function are passed to the command (${1:+"$@"}).

The last part first removes any dead sessions (screen -wipe), lets the user (me) know that screen will be starting shortly and can instead return to the shell by pressing Ctrl-C. Then it waits for 5 seconds and calls the reattach function.


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 .profile or other shell config script to exec screen if TERM != "screen".

If you don't like that, you can use the authorized_keys file to run a specific command for connections with a specific key (and you can have as many keys as you like). See man sshd for details. I'd suggest you make the command run a script that checks if screen is running and connects, or else starts a new session.

As for disconnect on Ctrl-D, screen allows you to set up key mappings in your .screenrc. See man screen for details. You're looking for "bindkey".


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:

[ -z $TMUX ] && tmux list-sessions 2>/dev/null && tmux a

or even easier

tmux a 2>/dev/null

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.


screen -dAr default || screen -AS default works for me. I add that to my ~/.bashrc.

Briefly explaining the switches:

  • d - Detach the screen if it's still attached to, from another session.
  • A - Adapt the windows in the screen to the new terminal size.
  • r default - Attach to a screen called default.

If the first screen command (the one before ||) does not succeed, the second one is run:

  • A - As explained above...
  • S default - Create the new screen session with a the name default.

I suggest using byobu. This tool uses tmux and provides a nice toolbar & other convenient window switching facilities, hotkeys etc.

echo "new-session" >> ~/.byobu/.tmux.conf
echo "if [ -n '\$TMUX' ]; then :; else byobu attach; fi" >> ~/.bashrc

With these above commands after a successful SSH login the system with try and attach to an existing byobu-tmux session, if not it will create a new session.

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.