Changing Systemd Boot Target in Linux

Many Linux distros, such as RHEL/CentOS 7, Fedora, Ubuntu 16, are now using systemd instead of init as the init system. It is common for Linux users to set Linux to boot to “GUI” or “Text” mode. The old way of changing ‘/etc/inittab’ for choosing Linux runlevels is not working for sytemd. This post will introduce the way for systemd systems to select the “runlevels”.

For systemd, the concept of runlevels is replaced by the term “targets”. For those that are familiar with the init runlevels, there is a “mapping” between the init runlevels and systemd targets:

   ┌─────────┬───────────────────┐
   │Runlevel │ Target            │
   ├─────────┼───────────────────┤
   │0        │ poweroff.target   │
   ├─────────┼───────────────────┤
   │1        │ rescue.target     │
   ├─────────┼───────────────────┤
   │2, 3, 4  │ multi-user.target │
   ├─────────┼───────────────────┤
   │5        │ graphical.target  │
   ├─────────┼───────────────────┤
   │6        │ reboot.target     │
   └─────────┴───────────────────┘

2 common targets are the most common ones:

  • multi-user.target: analogous to runlevel 3, Text mode
  • graphical.target: analogous to runlevel 5, GUI mode with X server

Change the “runlevels”/targets after booting

Even after the Linux system is booted to a target, you can change it to another target/runlevel. For example, to change Linux to “multi-user” target:

# systemctl isolate multi-user.target

The command used is isolate. It starts the unit specified on the command line and its dependencies and stop all others.

This is similar to changing the runlevel in a traditional init system using init 3.

Change the boot “runlevels”/targets

This changes the default boot target.

For example, to make “multi-user.target” the default “runlevel”, you can do:

# systemctl enable multi-user.target
# systemctl set-default multi-user.target

Next time you reboot, the default target booted to will be “multi-user”.

Bonus: the manual way

This is not suggested way for changing the boot target. The command using systemctl is the best way. Just for those who are curious.

The default target is controlled by /etc/systemd/system/default.target which is a symbolic to the real .target file.

To set a default target, change the symbolic to point to the target you want.

For example, to change the runlevel to ‘multi-user’ (3):

# rm -f /etc/systemd/system/default.target; 
# ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

9 comments:

  1. Good article, thanks!
    Is there a way to set the target via grub? I’d like to have an entry in my grub menu for booting to graphical/multi-user.

  2. To put Ubuntu 19.04 into Runlevel 2 (Terminal):
    sudo systemctl set-default multi-user.target

    To reverse:
    sudo systemctl set-default graphical.target

  3. Is there a way to give a user with minimal permissions just the NON GUI access and the root user will have full GUI access? I basically have an Ubuntu system shared by multiple users. I want to be the root user with full access. On boot, when a non-root user logs in, it should boot to non-GUI version of Ubuntu with my app running. But when I login with my root account, I should have the GUI version boot up. Thank you!

    1. If you have limited the non-root users’ ability to run your applications only, one possible way is to run the system in multi-user target/text mode. For root, after login, you can use startx command to start the X session from the terminal.

    2. Of course you can restrict users from running certain applications, but if you are about to give them a general shell, they may still find a way to circumvent that. What you want is a restricted shell, e.g. add your app to “/etc/shells”, then set it as the shell of any restricted user in “/etc/passwd”. As long as your application doesn’t allow the user to break out of it, you should be fine.

      As for the root user’s graphical environment, set just set your default to multi-user.target, then use startx after login to start your desktop. Disable or uninstall any display managers.

  4. You don’t need to enable targets. At least on my system this produces an error message, and trying to disable one does nothing. Furthermore, since graphical.target depends on multi-user.target, it is active anyway. While in sysv-init, runlevels used to be separate from each other, systemd builds a dependency tree, so one target may include another.

Leave a Reply

Your email address will not be published. Required fields are marked *