欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

runlevel和init及Ubuntu20.04修改启动runlevel

程序员文章站 2022-07-14 13:38:38
...

runlevel

  • linux操作系统自从开始启动至启动完毕需要经历几个不同的阶段,这几个阶段就叫做runlevel
 "Runlevels" are an obsolete way to start and stop groups of services used in SysV init. 

使用runlevel查看当前系统运行的

[email protected]:~$ runlevel --help
runlevel [OPTIONS...]

Prints the previous and current runlevel of the init system.

Options:
     --help      Show this help

See the runlevel(8) man page for details.

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     │
       ├─────────┼───────────────────┤

简介

  • 0 停机

  • 1 单用户模式

  • 2 多用户,没有 NFS

  • 3 完全多用户模式 (命令行)

  • 4 没有用到

  • 5 图形界面

  • 6 重新启动

  • S s Single user mode
    用到最多的时3和5还有0

  • 系统默认安装后,若有图形界面则,默认进入 level 5

  • level 3 是最常用的命令行模式

  • 关机时为level 0

  • 更改密码或修复系统用到单用户模式level 0

init

  • init命令是Linux下的进程初始化工具,init进程是所有Linux进程的父进程,它的进程号为1。init命令是Linux操作系统中不可缺少的程序之一,init进程是Linux内核引导运行的,是系统中的第一个进程。
 systemd, init - systemd system and service manager

使用init切换用户模式

  • init 0 执行关机动作,需root权限
  • init 3 切换至命令行界面
  • init 5 切换至图形界面
  • init -s 切换至单用户模式

设置Linux启动时的runlevel

  • CentOS
[22:09:38 [email protected] ~]#cat /etc/inittab
# inittab is no longer used.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
  • Ubuntu
[email protected]:~$ cat /run/utmp
~~~reboot5.4.0-42-generic{?Y`	35~~~runlevel5.4.0-42-genericGY`®7¤tty1tty1tty1LOGIN¤?Y`=	¶pts/0ts/[email protected]`^® 
Epts/1ts/[email protected]`

上面那个就是看看,配置文件在哪我暂时没找到,等找到了再发过来。

  • 设置开机命令行
sudo systemctl enable multi-user.target
sudo systemctl set-default multi-user.target
或者
sudo systemctl set-default runlevel3.target

-设置开机图形

sudo systemctl enable graphical.target
sudo systemctl set-default graphical.target
或者
sudo systemctl set-default graphical.target

Ubuntu 20.04 设置开机启动由图形界面改为直接进命令行

[email protected]:~$ cat /etc/init/lightdm.conf 
cat: /etc/init/lightdm.conf: No such file or directory
[email protected]:~$ sudo systemctl enable multi-user.target
[sudo] password for u20: 
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
 
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.
[email protected]:~$ sudo systemctl set-default multi-user.target
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/multi-user.target. ##实际上是更改了软链接
[email protected]:~$ runlevel
N 3

经过实验发现,Ubuntu 20启动runlevel修改方式并不保存在配置文件中,而是通过更改软连接的方式实现了启动runlevel的修改。