Terminal auto-lock with zsh and vlock | wroot
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)
export TMOUT=300
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:
export TMOUT=600
function TRAPALRM() { vlock }
And now zsh locks my sessions after 10 minutes. It’s working perfectly even within tmux.