Home » Commands » How to Use Loginctl

How to Use Loginctl

The loginctl command is part of the systemd toolkit for Linux system management. It can help manage sessions, view logged-in users, screen lock and unlock, kill sessions and processes of session, etc.

Previously, we had to use several utilities and configuration files to do this. But now, everything is in one place. In this article, we will dive into managing sessions using loginctl.


Table of Contents

Loginctl Basics

The loginctl utility allows to interact with everything concerning the user's authentication. The official documentation says that the loginctl command is designed to control the system login manager - systemd-logind. The command operates these entities:

  • User - any user that is registered in Linux system;
  • Session - user's authorization. Each login, except user switching using su or sudo commands, is a dedicated session. You can log in using TTY, SSH or a graphical login manager and all those are new sessions;
  • Seat - this concept allows connecting multiple sets of hardware (mouse, keyboard, display) and using it by multiple users at the same time. Usually, this feature is not used and the utility shows only one seat.

Now, let's have a look at the loginctl syntax and options.

Syntax and Options

The syntax is very simple. Just enter options, command and arguments:

$ loginctl options command arguments

Here are the most used commands which I will describe in this article:

  • list-sessions - shows a list of sessions;
  • session-status <id> - shows the session state, login date and time, and a list of processes of the session;
  • show-session <id> - shows properties of the session;
  • activate <id> - switches to the specified session;
  • lock-session <id> - locks screen for the specified session;
  • unlock-session <id> - unlocks screen for the specified session;
  • kill-session <id> - terminates the session or sends the specified signal to all session's processes;
  • terminate-session <id> - terminates session;
  • list-users - shows user names who are currently logged in;
  • user-status <id> - works as well as session-status, prints login date, state, and active services with their processes as a tree;
  • show-user <id> - prints information about the user and his current session, without a list of services;
  • terminate-user <id> - terminates all user sessions and their processes;
  • kill-user <id> - sends a signal to all processes of a user. SIGTERM will be sent by default;
  • list-seats - shows a list of available seats;
  • seat-status <id> - shows all processes that are related to the seat;
  • show-seat <id> - prints settings for the seat;
  • terminate-seat <id> - terminates all processes that are related to the seat.

I will not cover seat configuration in this article, so I will not even give commands for it. Note that many commands can work only for sessions with GUI. For example, look-session and unlock-session are not supported for TTY or SSH sessions. You can read more about loginctl options on its man page

How to Use Loginctl

1. Get Session Information

You can look through all current sessions using the list-sessions command:

loginctl list-sessions

In this case you can see two sessions. The first one is an autologin in Gnome GUI, and the second one is a console session in TTY3. Here you can see the session identifiers, user ID, and TTY. If you want to view more detailed information, use the session-status command. Here is the GUI session:

loginctl session-status 1

You can see when the session has been started, its type (X11, TTY, or Wayland), and its main processes. The information about the TTY session will look like this:

loginctl session-status 3

If you want to see session settings without the services, use the show-session command:

loginctl show-session 1

You can specify which exact properties you want to get using the -p or --property option:

loginctl show-session 1 --p Type

If you want to get only the value of the property, use the --value option:

loginctl show-session 1 --p Type --value

2. Get Information About Users

The process of getting information about logged-in users is exactly the same as getting information about sessions. You can get all logged-in users using the following command:

loginctl list-users

If you want to get details for each user, use user-status command. Note, that you will see info about sessions and authorizations but not user data:

loginctl user-status 1000

You can find all user sessions in the Sessions field, the active session will be marked with an asterisk. Below, you will see Systemd services that belong to this user. By default, the process list can be not full. If you want to see all processes use -l or --full option:

loginctl user-status 1000 --full

If you want to print all processes without pagination, use the --no-pager option:

loginctl user-status 1000 --no-pager

3. Switch to Another Session

You can switch sessions using GUI, for example, Gnome or KDE. But also you can do it in the command line using the loginctl command. For example, if you want to switch to the session with the 3, use this command:

loginctl activate 3

After switching, you can switch back. It may be more convenient than switching to another TTY using hotkeys. Also, you can lock the screen for the previous session after switching. Use the session-lock command to do this. For example:

loginctl lock-session 1

4. Terminate Sessions and Processes

You can terminate the process that belongs to a specific user or session using kill-user and kill-session commands. These commands work as well as the kill command. It means, that you can not only terminate the processes but also send any signal to them. If you pass only the session identifier to the kill-session, the session will be terminated:

loginctl kill-session 3

You can send a specific signal using the --signal option. By default, the command sends SIGTERM signal. For example, if you want to send SIGKILL, use this command:

loginctl kill-session 3 --signal SIGKILL

The same command exists for terminating all processes that belong to a user. It is called kill-user. In addition, there is a session-terminate command. It does almost the same thing, but it does not allow you to set a signal and just terminates all processes:

loginctl terminate-session 3

5. Autostart User Instances at System Boot

Systemd supports services that can be run from regular users, without superuser privileges. You can manage them without su or sudo. It is very convenient. Many desktop environments already use this feature. For example, Gnome.

These services require to use the --user option with the systemctl command to interact with them. Usually, these services start when the user logs in the first time and stop when he logs out. But in some cases, you may want the user's services to be started at the system start and not to be killed at log out. You can configure it using the linger feature.

For example, if you want to configure it for the Sergiy user, run this command:

loginctl enable-linger sergiy

You can check the state of this parameter using the user-status command. Use the disable-linger command to disable it:

loginctl disable-linger sergiy

Wrapping Up

In this article, I have explained how to use loginctl to show information about sessions, logged-in users, and how to terminate user's sessions. As you can see, the utility is very simple and allows doing everything in the same place.

Rate the Article

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Leave a Comment