Terminal auto-lock with zsh and vlock

I’m always concerned about leaving terminal sessions open. I’ve used for many and many years the $TMOUT environment variable to close my sessions if idle for N seconds.

Just by exporting the TMOUT variable to the number of desired timeout seconds will close your shell (Bash, Ksh, Zsh and some others).

The following example will timeout in 300 seconds (5 minutes)

I am currently reading the book Secure Coding: Principles & Practices and the authors cited this timeout technique as pretty ineffective since it annoys more than it helps. I was obliged to agree. I got pretty mad with it some good times.

So I started looking for alternatives.

I’ve found a console application called vlock. It should be available on most distro’s repositories.

Just invoke vlock and the terminal session will be locked awaiting the user password to unlock. Pretty nice. Locking is definitely better than killing the session.

So I just started to wonder how to integrate vlock with zsh and after some research I’ve discovered that the shell will only be killed within TMOUT if no trap function for signal ALARM is set.

If you set an ALARM trap function, it will be called instead of killing the session. Perfect.

So I ended up with this in my .zshrc:

And now zsh locks my sessions after 10 minutes. It’s working perfectly even within tmux.

As the comment from the reader Ehtesh Choudhury we can accomplish that in tmux only by adding to your configuration:

4 responses to “Terminal auto-lock with zsh and vlock”

  1. Ehtesh Choudhury says:

    With recent versions of tmux, you can also use:

    set -g lock-command vlock

    in your .tmux.conf

  2. Jan Seidl says:

    Thanks Etesh!

    Added your tip to the post

  3. Patrick Roy Lapratte says:

    It will lock the current window only in TMUX… Create another one or jump to another created window not locked will give access to your computer anyway…

  4. Jan Seidl says:

    It will lock terminals that runs your rc file. It won’t any other. For eg, if the vlock setting is in your zshrc but you spawn an bash shell, it will give access anyway :)

Leave a Reply

Your email address will not be published. Required fields are marked *