Another topic that is related to users is session management.
There's a couple of commands that allow you to manage what's going on for your users.
To start with, there's the old commands who and W, which is showing who is logged in.
W, in fact, is the shortest command on Linux. It's showing who is logging in and also what they are doing as
a part of the system. The manager of everything, Login CTL has been introduced and Login CTL allows for current session management.
You can use Login CTL List Sessions for a list of sessions currently
active and Login CTL show session for more details about a session id.
There is show user, which is giving more information about the user id. And there is login CTL Terminate session,
which allows you to actually stop a session right now that's pretty convenient if you are figuring out that some
session is active where you don't know what is going on so you want to stop it immediately.
Use loginctl terminate to terminate it right now.
Now, Login CTL is a part of systemd and it might not be
available in your environment. If systemctl is not available, you'll also not have
login ctl, but if it's there, it's a convenient command. I'll show you.
Let me start by using W and W is showing everybody who's logged in right now.
Now I want to log in in a different terminal.
Let me show you the virtual terminal. I'm using CHVT6.
This is opening virtual terminal number six. I do need sudo superpowers.
So what is that bringing me?
Okay, good.
So this is giving me a login prompt and I'm going to log in
as user Anna and I'm providing the very secret password of user Anna.
Now I need su student to open a shell as user student and I
need as user student sudo chvt2 to get back to virtual terminal number two.
And now I'm using W. And look at that. We have user Anna on TTY6 that is shown as well.
I really like W because it's also showing the current activity of the user.
And that's not happening for the who command, which is much more compact.
I also like login ctl. So sudo login CTL
list sessions is showing sessions that are currently active.
And I can also use other commands that login ctl. Let's do a
sudo login CTL tab tab where we can see all the different command
line options like list users, which is just showing me the current users.
Now I want to know what is going on for this user Anna. So let
me use a sudo login CTL show user Anna and there we can see current
settings for my user Anna. Hey, if my user Anna is a user that I
don't expect to be here then I can terminate sessions for user Anna.
I can see the sessions currently open for this user that is 89 and 88 but probably
we want to have a look at list sessions again and there we can see user session
88 with the TTY TTY6. That's probably the one that I need to manage. So I am
going to use sudo login CTL terminate session and that allows me to terminate session number 88.
And there we go.
Session 88 is removed and session 89 is still there. Well we probably need to terminate it as well if we
have no idea what this user is doing on the system. And that's how you can manage current user sessions.