6

X screen lockers are fine, but I sometimes have stuff open on other TTYs that I want to lock as well. For this reason I would like to use vlock -a to lock all TTYs at the same time. However, I am typically in an X session when I want to run this command.

vlock cannot be run from within X because it complains that the current terminal is not a virtual console. I tried to make some solution with chvt but I don't know how to run the command in that console afterwards.

How can I lock all TTYs without exiting X (preferably using vlock, but I'm open to other suggestions)?

3 Answers 3

4

See this command line option:

   -n,--new
          Switch to a new virtual console before locking all console sessions.

What I always do is sudo vlock -ans, works inside / outside Xorg.

It will jump back to the original VT when you unlocked it.

EDIT

Too bad, Arch Linux replaced the original vlock package with the one provided by kbd package. That one can lock only the current console, which is useless to you. (none of -a -n -s is supported, sigh)

The workaround can be tricky, you must manually install the original vlock, and mask kbd from upgrading in pacman.conf

See this link for further information

2
  • I do not have this option in my version of vlock (Arch Linux). Any idea how to get it?
    – Zhao Xi
    Apr 25, 2014 at 5:04
  • @user66104 Too bad, check my update please
    – daisy
    Apr 25, 2014 at 5:14
3

If your heart is not set on using vlock, you should take a look at physlock. Physlock does exactly what it seems that you are looking for: it switches to a new VT, locks it and disables console switching. Upon unlocking, you are returned to whatever console you were on when you invoked it (and it can be invoked from X or from a TTY).

It has a great set of options and remains incredibly light-weight; I actually use it for my default screenlocker in combination with xautolock.

0

tl;dr your idea with chvt was almost correct this is the command line

sudo openvt -s -- sudo -u "$(whoami)" "$SHELL" -c \
  'vlock -a; chvt '"$(sed 's/^tty//' < /sys/devices/virtual/tty/tty0/active)" 

it uses openvt -s/--switch to run the vlock on a newly opened tty and switches back after unlock previous tty as retrieved via /sys/devices/virtual/tty/tty0/active (i.e. your tty with X running).

long story

while your question's title

us[e] vlock without changing VT from X

appears to suggest that you desire an answer that prevents a change of the vt/tty it appears you yet would like the vlock -a functionality and attempted chvt. This answer provides a solution that follows your original idea of chvt.

# get the currently active tty 
# (via https://unix.stackexchange.com/q/554600/24394)
ACTIVE_TTY="$(cat /sys/devices/virtual/tty/tty0/active)"
# get current user 
ACTIVE_USER="$(whoami)"

# get the current shell or fallback
test -x "$SHELL" && ACTIVE_SHELL="$SHELL" || ACTIVE_SHELL="/bin/bash"

# 1. sudo open and switch to new tty 
# 2. sudo run as ACTIVE_USER the ACTIVE_SHELL with
# 3. vlock -a to lock down
# 4. and upon unlock print unlocked and
# 5. switch back to previous shell
sudo openvt --switch --\
  sudo -u "$ACTIVE_USER" "$ACTIVE_SHELL" -c \
    'vlock -a; echo unlocked; chvt '"${ACTIVE_TTY/tty/}"  


sudo openvt --switch -- sudo -u "$(whoami)" "$SHELL" -c 'vlock -a; chvt '"$(sed 's/^tty//' < /sys/devices/virtual/tty/tty0/active)"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .