17
  • Command systemctl get-default returns user-defined.target.
  • Then I use systemctl isolate multi-user.target to switch to multi-user.target.
  • I can see that a bunch of services that shouldn't be running on user-defined.taret and should be running on multi-user are running, this implies I am on multi-user.target.
  • But, systemctl get-default always returns user-defined.target.

Question is, without looking and sorting through the services, how do I know that I am running on multi-user.target after using isolate ?

iamauser
  • 349
  • 2
  • 3
  • 12
  • 1
    There isn't a single target `systemctl list-units --type target` – user9517 Feb 28 '17 at 22:17
  • Not entirely sure what you mean. I know there isn't a single target, but then why the `default` and `isolate`? What do they represent ? – iamauser Feb 28 '17 at 22:35
  • 1
    @iamauser. `isolate` means change to the specified target. That does not survive a reboot unless you persist the specified target using `set-default` – fpmurphy Mar 01 '17 at 01:12

6 Answers6

16

In systemd, there may be more than one active target at a time.

To inspect the list of all currently active targets:

systemctl list-units --type target --state active

To quickly find out whether a specific target (e.g. user-defined.target) is active or not:

systemctl is-active user-defined.target
Amir
  • 837
  • 8
  • 17
3

There is no systemd command to query the running target or the last target used with isolate.

systemd does ship with a command called runlevel for compatibility for older systems. This will prevent the current "runlevel". The concept is obsolete, but as seen as man runlevel, particular run levels map to particular systemd targets. This command might be helpful as long as standard targets are used. It would not be useful if a custom target was used which did not map to a legacy runlevel.

More discussion about workarounds is on a [https://www.centos.org/forums/viewtopic.php?t=54347](CentOS forum).

Mark Stosberg
  • 3,901
  • 24
  • 28
3

Similarly to the answer previously mentioned you can use:

systemctl list-units --type target | egrep "eme|res|gra|mul" | head -1

What you get as a result is your current target.

If you have installed unit that has in its name one of these four strings above, you could add the ^ character in front of them - egrep "^eme|^res|^gra|^mul"

After I've read 'Eduard Rozenberg' post below I decided to edit my answer, actually to provide additional clarification. I don't know if Eduard tried my solution before posting his answer.
However, if you use this command above you should get right results. Eduard states that he gets both, graphical and multi-user target. We should get both, graphical and multi-user targets only in GUI environment because graphical.target wants multi-user.target.
Head successfully resolves this showing only first line (graphical.target), because results are sorted alphabetically.

@Eduard Rozenberg - if you read this please provide feedback if this command works for you, thanks...

Also I would like to provide information for shorter command/typing:

  • you can use -t instead of --type
Damir
  • 131
  • 2
2

Here are some screenshots of systemctl list-units --type target after pursuing some targets.

emergency target screenshot
emergency target

rescue target screenshot
rescue target

multi-user target screenshot
multi-user target

Robert Siemer
  • 542
  • 9
  • 19
Mighty Max
  • 61
  • 2
1

I think the best command to find out the current target is

# who -r

If its runlevel 3 ==> multi-user.target, runlevel 5 ==> graphical.target

But I'm not sure RH will remove this cmd, as this is very legacy method to check boot process.

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
Jason
  • 11
  • 1
0

Unfortunately none of these answers indicate which runlevel is currently active. They all show in my case that both multi-user.target and graphical.target are loaded/active. So far only the runlevel command indicates the current runlevel.

runlevel | awk '{print $2}'

Ed R
  • 26
  • 2
  • 1
    systemd is, in fact, stateless. There is no current "runlevel", as you can start and stop units all the time. This is why these units are called "targets" and the command is called "isolate". – Bachsau Jun 28 '20 at 10:55
  • systemd doesn't have the concept of runlevels and doesn't come with a runlevel command. Your runlevel command is part of another init system left on your computer and probably won't reflect the current state of your computer. – Starfish Aug 30 '20 at 21:43