GDM: how to switch user in command line?

Is there any official/bult-in way to switch user using command line? I know that I can lock screen by gnome-screensaver-command --lock (which is part of GNOME Flashback BTW) or loginctl lock-session but I want to lock screen AND switch to GDM greeter.

Basically I want to have shortcut to switch user but there is no such option in Settings/Devices/Keyboard. There is only “lock screen” which does not satisfy me. :wink: Now, I solved this problem by creating script in my ~/.local/bin (which I have in $PATH) and using it in manually added keyboard shortcut in desktop settings. The script is here:

#!/bin/bash

loginctl lock-session

gdbus call \
    --system \
    --object-path /org/gnome/DisplayManager/LocalDisplayFactory \
    --dest org.gnome.DisplayManager \
    --method org.gnome.DisplayManager.LocalDisplayFactory.CreateTransientDisplay \
    > /dev/null

Is there better solution?

1 Like

Edit: Argh, sorry, that’s completely wrong. I had misread the part I’m quoting and thought it was about logging out, but it’s about locking the screen. Good job on reading comprehension @bochecha! :tired_face:


I don’t know how to answer your question, but for this specific bit:

The proper way, which should work on both GNOME and GNOME Flashback, would be:

gnome-session-quit --logout
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

I created issue on gnome gitlab: https://gitlab.gnome.org/GNOME/gnome-control-center/issues/727

Also, as temporary solution, I wrote new script for this task. It’s here:

#!/bin/bash

# First, try to lock screen through logind.
loginctl lock-session

# logind may fail if this script will be ran from gnome-terminal when GNOME
# session is managed by systemd. Instead, try to use d-bus method provided by
# gnome-shell or gnome-screensaver (the latter is in GNOME Flashback).
if [[ $? != 0 ]]; then
    gdbus call \
       --session \
       --object-path /org/gnome/ScreenSaver \
       --dest org.gnome.ScreenSaver \
       --method org.gnome.ScreenSaver.Lock \
       > /dev/null

# If screen was locked by logind, wait a moment. Without this, screen image
# visible before locking may be cached on user TTY which may violate user privacy
# if someone try to switch to that TTY. This is not needed with second method
# because `gdbus` command will wait until work is finished.
else
    sleep 0.4
fi

# Then, try to switch GDM's login screen. If login screen uses Xorg or if greeter
# session was not killed yet (GDM may kill its greeter if Wayland is used,
# for performance) it's needed to switch to login screen's TTY.
loginctl activate \
    $(loginctl list-sessions | grep -m 1 gdm | awk '{print $1}')

# If GDM killed its greeter it's needed to call internal GDM's d-bus method to
# start new greeter session. In this case, GDM will automatically switch to proper
# TTY. This *won't work* if greeter session is already alive.
if [[ $? != 0 ]]; then
    gdbus call \
        --system \
        --object-path /org/gnome/DisplayManager/LocalDisplayFactory \
        --dest org.gnome.DisplayManager \
        --method org.gnome.DisplayManager.LocalDisplayFactory.CreateTransientDisplay \
        > /dev/null
fi
1 Like