0

I've been using gnu screen for many years, and I've been looking into switching to tmux.

I can't figure out how to make tmux require a password when attaching to a session. In gnu screen, I use the :password command to set a password. Then after detach, subsequent attaches to the session using screen -x requires the password to attach.

How do I configure tmux to do the same? I've done some googling, and I get the impression that this isn't possible. That's a show-stopper for me. I like tmux, but if it doesn't support password-protected session attaches, I can't use it.

1

This isn't possible with tmux yet. You can only lock the terminal so far. The developers argue something about if someone has access to another shell on the system then you are toast anyway.

However, I've come up with a quick solution using file permissions and attributes to lock things up. I've put it together into 3 bash files which you would have to add to your $PATH.

start.sh

#!/bin/bash

if [[ ! -d ~/.tmux-sessions ]]; then
    mkdir ~/.tmux-sessions
fi

tmux -S ~/.tmux-sessions/secure

Once you want to exit your session, do so normally with ctrl+b,d -- then you would want to run lock.sh shown below.

lock.sh

#!/bin/bash

chmod 000 ~/.tmux-sessions
sudo chattr +i ~/.tmux-sessions
sudo -k

This will lock the socket used to connect to tmux. Nobody will be able to access it until you remove the immutable attribute from the socket directory and change the permissions back to normal. Luckily we can create a script for this, too:

attach.sh

#!/bin/bash

sudo chattr -i .tmux-sessions
chmod 770 .tmux-sessions
tmux -S ~/.tmux-sessions/secure attach

With that you should just be able to run attach.sh, which will ask you for your user password to re-open.

Hope this hack helps!

For just locking your terminal.

First you need to have vlock installed.

sudo apt install vlock

or whatever package manager command you use.

Then you set tmux to use vlock

echo "set-option -g lock-command vlock" >> ~/.tmux.conf

Then when you are in tmux, you can do:

ctrl+b,:lock-session[enter]

That will lock the terminal. The caveat is someone could still attach to the session from another terminal. I'm looking to see if there is a way to prevent that as well.

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.