How can I set screen so it runs automatically any time a user logs in using a CLI interface? If there is already a screen session running for that user, I would like any new logins to attach to that screen session. It would be really nice if on attaching to the screen session a new window is created for each new login.


First, for the users you are going to do this for, make sure their ~/.screenrc has the line

shell /bin/bash

(or whatever other shell your user currently uses). If you do not have this setting set and you change their shell like we will, the user will not be able to login. You might also consider making the screen startup message go away with this line

startup_message off

Ok, now lets make a runnable script that attaches to the first already running screen session if it exits, and it none is already running it starts one. Run in a terminal

sudo nano /bin/screenlogin.sh

Put in the two following lines:

screen -x -r || screen

Press Ctrl x and then y to save the file. Run

sudo chmod 755 /bin/screenlogin.sh

Finally, run chsh on the target user and put in /bin/screenlogin.sh for the shell when asked.

  • Unfortunatly, this method seems to keep SFTP to that user from working. Not sure why. So I'm still looking for better answers. – Azendale Aug 8 '11 at 5:33
  • It seems that output from the terminal when login shells are run causes problems for SFTP. Would be most thankful if someone knew how to tweak this answer to fix my script. – Azendale Aug 26 '11 at 3:20

Running screen -xR will do basically the same as running screen -xr || screen.

The problem is that as far as I know, you can't pass flags to a login shell. Also, having something non-shell like screen as your login shell would probably break things like SFTP.

So, what we'll do is set bash as our login shell. Then we will add to the end of .bashrc (a file bash runs when you start it) code that runs screen. Screen will then run whatever shell is specified in .screenrc.

We will make it so that this command will run only under a special condition: Only if the current running copy bash has not been started (possibly indirectly) by another copy of bash. This will make it so that if you user wants bash as their shell, and screen starts bash inside of it, the user won't get a Russian dolls explosion of bash inside of screen inside of bash inside of screen inside of bash until the computer runs out of memory.

First, change the shell to /bin/bash, by running chsh, then entering your password, then typing in /bin/bash and pressing enter.

Next edit the hidden .bashrc file in your home folder, adding the following to the end:

if [ ${SHLVL} -eq 1 ]; then
    ((SHLVL+=1)); export SHLVL
    exec screen -xR

Now, edit (create if it doesn't exit) the hidden file .screenrc in the home folder. Add or edit the following settings:

shell /bin/zsh
startup_message off

(Of course, if you want some other shell than Zsh, go ahead and put that in instead. It should even work with bash.)

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.