如何更改 systemd 上的运行级别?

gno*_*med 34 boot runlevel systemd

很简单,我正在尝试更改运行级别。我在网上找到的所有内容都指向位于以下位置的文件:

/etc/init/rc-sysinit.conf
Run Code Online (Sandbox Code Playgroud)

在这里,我尝试将“DEFAULT_RUNLEVEL”更改为 3 或其他任何值,但没有任何区别(原始值为 2,这也没有多大意义)。无论如何,我的机器完全启动,当我检查运行级别命令时,我每次都看到“N 5”作为结果。

如何更改运行级别?我宁愿不通过 grub 或其他一些变通机制来覆盖它。而且我不是在寻找如何专门禁用 X。

我在网上找到的所有说明都有些旧,16.04 有什么变化吗?

Ron*_*Ron 66

Ubuntu 16.04 使用 systemd 而不是 init,因此 的概念runlevels被术语targets. 所以确实存在基于 init 的运行级别和基于 systemd 的目标之间的映射:

   Mapping between runlevels and systemd targets
   ???????????????????????????????
   ?Runlevel ? Target            ?
   ???????????????????????????????
   ?0        ? poweroff.target   ?
   ???????????????????????????????
   ?1        ? rescue.target     ?
   ???????????????????????????????
   ?2, 3, 4  ? multi-user.target ?
   ???????????????????????????????
   ?5        ? graphical.target  ?
   ???????????????????????????????
   ?6        ? reboot.target     ?
   ???????????????????????????????
Run Code Online (Sandbox Code Playgroud)

现在,只需更改 16.04 中的“运行级别”,您可以使用例如:

sudo systemctl isolate multi-user.target
Run Code Online (Sandbox Code Playgroud)

要使其成为默认的“运行级别”,您可以使用:

sudo systemctl enable multi-user.target
sudo systemctl set-default multi-user.target
Run Code Online (Sandbox Code Playgroud)

man systemctl

   isolate NAME
       Start the unit specified on the command line and its dependencies and stop all others. If
       a unit name with no extension is given, an extension of ".target" will be assumed.

       This is similar to changing the runlevel in a traditional init system. The isolate command
       will immediately stop processes that are not enabled in the new unit, possibly including
       the graphical environment or terminal you are currently using
Run Code Online (Sandbox Code Playgroud)

另请查看man systemd.special以了解有关 systemd 中目标的更多信息。

  • @nyxee - 是的,`sudo systemctl set-default graphics.target` 是恢复到 GUI 桌面环境的正确方法。您的登录失败听起来与运行级别之间的切换无关 - 听起来更像是 nVidia [引导循环](https://askubuntu.com/questions/762831/ubuntu-16-stuck-in-login-loop-after-installing-nvidia -364-驱动程序“引导循环”)问题。要确定,请返回运行级别 3,删除 nVidia 和 Cuda 驱动程序,从存储库安装 nVidia 驱动程序,将默认值设置为运行级别 5 (graphical.target),然后重新启动并查看是否可以登录。 (2认同)