KDE Connect / GSConnect: How To Lock/Unlock Your Linux Desktop Using An Android Device

KDE Connect and GSConnect support running commands from an Android device to your Linux desktop. This article explains how to lock and unlock your Linux desktop using a single tap on your Android device, via the KDE Connect Android app. You'll also find a workaround for GNOME 3.34 which has its session managed by systemd, and complicates this a bit.

KDE Connect is a tool that enables devices like a desktop running Linux and Android devices to communicate with each other. To use it, you must install the desktop component on your Linux desktop, and the Android app on an Android device. GSConnect is a complete implementation of KDE Connect created for the GNOME Shell desktop.

It's worth noting that the commands we'll use in this article for locking / unlocking your screen are desktop-agnostic, and work with any desktop environment (and any Linux distribution) that uses logind.

What's more, there's no need to enter your password when unlocking the screen. That's because the lock/unlock commands we'll use below don't require super user privileges; also, the unlock command is able to unlock your session without a password because KDE Connect is able to run commands on your Linux desktop even when the screen is locked.

Edit: Take a look at a video showing screen locking/unlocking using the KDE Connect Android app and GSConnect installed on Fedora 31 (with GNOME 3.34; I'm using scrcpy to show my Android phone screen on the desktop):

To be able to lock / unlock your screen using your Android device open KDE Connect / GSConnect and:

  • KDE Connect: in the application plugins list, make sure the "Run commands" plugin is enabled, then click the Run commands configure button
  • GSConnect: click on your device name and you'll find a Commands section in the sidebar

KDE Connect commands
KDE Connect commands

Next, add the following commands in KDE Connect / GSConnect with GNOME 3.32 or older (this won't work with GNOME 3.34 or newer!) on your desktop:

  • Lock screen: loginctl lock-session
  • Unlock screen: loginctl unlock-session

After adding these commands on your Linux desktop, they will be shown in the KDE Connect Android app, under Run Command:

KDE Connect Android lock/unlock screen

Since GNOME switched to having its session managed using systemd (with GNOME 3.34), these loginctl lock/unlock commands stopped working by themselves, requiring the session ID to lock or unlock the screen. I managed to put together a workaround for this though -- a command (see below) that uses loginctl to list all sessions, searches for the current user in the session list, and retrieves that session's ID, then uses loginctl lock-session or unlock-session followed by the session ID.

GSConnect Commands
GSConnect commands

So if you use GSConnect with GNOME 3.34 (or newer), instead of the commands I mentioned above, use these commands:

  • Lock screen:
SESSION=$(loginctl list-sessions | grep $(whoami) | awk '{print $1}'); loginctl lock-session $SESSION

  • Unlock screen:
SESSION=$(loginctl list-sessions | grep $(whoami) | awk '{print $1}'); loginctl unlock-session $SESSION

I only have one computer running GNOME 3.34 to test this right now, and this worked there (using Ubuntu 19.10), hopefully it works on other Linux distributions as well. Edit: I also gave this a try on Fedora 31 and it worked as expected.

KDE Connect Android widget

The KDE Connect Android app supports widgets. So you can skip having to open the KDE Connect Android app each time you want to lock or unlock your computer, and simply add a widget that locks and unlocks your Linux desktop with a single tap.

A few more useful commands that you can use with KDE Connect / GSConnect can be found on the this page.