运行级就是Linux操作系统当前正在运行的功能级别。存在七个运行级别,编号从0到6。系统可以引导到任何给定的运行级别。运行级别由数字标识。

每个运行级别指定不同的系统配置,并允许访问不同的进程组合。默认情况下,Linux会引导至运行级别3或运行级别5。启动时一次只执行一个运行级别。它不会一个接一个地执行。
系统的默认运行级别在SysVinit系统的/etc/inittab文件中指定。
但是systemd系统不读取此文件,它使用以下文件/etc/systemd/system/default.target来获取默认的运行级别信息。
我们可以使用以下五种方法检查Linux系统当前运行级别。
runlevel命令runlevel打印系统的上一个和当前运行级别。
who命令:打印有关当前登录用户的信息。它将使用“-r”选项打印运行级别信息。
systemctl命令:它控制systemd系统和服务管理器。
使用/etc/inittab文件:系统的默认运行级别在SysVinit System/etc/inittab文件中指定。
使用/etc/systemd/system/default.target文件:系统的默认运行级别在systemd System/etc/systemd/system/default.target文件中指定。
详细的运行级别信息在下表中描述。

系统将根据运行级别执行程序/服务。

1、对于SysVinit系统,它将从以下位置执行:
运行级别0 - /etc/rc.d/rc0.d/

运行级别1 - /etc/rc.d/rc1.d/
运行级别2 - /etc/rc.d/rc2.d/
运行级别3 - /etc/rc.d/rc3.d/
运行级别4 - /etc/rc.d/rc4.d/
运行级别5 - /etc/rc.d/rc5.d/
运行级别6 - /etc/rc.d/rc6.d/
2、对于systemd系统,它将从以下位置执行:
runlevel1.target – /etc/systemd/system/rescue.target
runlevel2.target – /etc/systemd/system/multi-user.target.wants
runlevel3.target – /etc/systemd/system/multi-user.target.wants
runlevel4.target – /etc/systemd/system/multi-user.target.wants
runlevel5.target – /etc/systemd/system/graphical.target.wants
1、使用runlevel命令

runlevel打印系统的上一个和当前运行级别:

[linuxidc@localhost linuxidc.com]$ runlevel

N 5
1]、N:“N”表示自系统启动后运行级别尚未更改。
2]、5:“5”表示系统的当前运行级别。

2、使用who命令

打印有关当前登录用户的信息,它将使用-r选项打印运行级别信息:
[linuxidc@localhost linuxidc.com]$ who -r
运行级别 5

3、使用systemctl命令

systemctl用于控制systemd系统和服务管理器,systemd是Unix操作系统的系统和服务管理器。它可以作为sysvinit系统的直接替代品,systemd是内核启动并保持PID 1的第一个进程。systemd使用.service文件而不是bash脚本(SysVinit使用),systemd将所有守护进程排序到他们自己的Linux cgroup中,可以通过浏览/cgroup/systemd文件来查看系统层次结构:

[linuxidc@localhost linuxidc.com]$ systemctl get-default
graphical.target
4、使用/etc/inittab文件

系统的默认运行级别在SysVinit System/etc/inittab文件中指定,但systemd不读取文件,因此,它仅适用于SysVinit系统而不适用于systemd系统。

CentOS 6是通过修改/etc/inittab来修改默认运行级别的:
[root@localhost ~]# vim /etc/inittab
# inittab is only used by upstart for the default runlevel.
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
# System initialization is started by /etc/init/rcS.conf
# Individual runlevels are started by /etc/init/rc.conf
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
# Default runlevel. The runlevels used are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
id:5:initdefault:
在CentOS 7下运行vim /etc/inittab结果如下:
[linuxidc@localhost linuxidc.com]$ vim /etc/inittab
# inittab is no longer used when using systemd.
#
# 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

可以看到,里面除了注释,什么也没有,并没有CentOS 6中设置默认运行级别的方式。
注释内容大意是说,

# multi-user.target类似于runlevel 3;
# graphical.target类似于runlevel5

获得当前默认运行级别的方式为

systemctl get-default

设置默认运行级别的方式

systemctl set-default TARGET.target

设置运行级别命令格式:

systemctl [command] [unit.target]

需要命令 systemctl
设置默认的运行级别为 1 则命令为:systemctl set-default xxx
中对系统的级别对应是

init级别 systemctl target
0 shutdown.target
1 emergency.target
2 rescure.target
3 multi-user.target
4 无
5 graphical.target
6 无

备注:以上命令均需要超级管理员权限,如果需要临时切换 直接 init + 需要切换到的数字。

5、使用/etc/systemd/system/default.target文件

系统的默认运行级别在systemd System的/etc/systemd/system/default.target文件中指定,它不适用于SysVinit系统:

[linuxidc@localhost linuxidc.com]$ cat /etc/systemd/system/default.target

Linux:检查当前运行级别的五种方法的更多相关文章

  1. Linux入门:运行级别解析

    Linux入门:运行级别解析   一.查看当前运行级别 Ubuntu中,runlevel命令 可以查看当前运行级别: CentOS中,who -r 命令查看当前运行级别:   www.2cto.com ...

  2. linux 清空catalina.out日志 不需要重启tomcat(五种方法)【转】

    1.重定向方法清空文件   [root@localhost logs]# du -h catalina.out  查看文件大小17M catalina.out[root@localhost logs] ...

  3. 【转】Linux 中清空或删除大文件内容的五种方法(truncate 命令清空文件)

    原文: http://www.jb51.net/article/100462.htm truncate -s 0 access.log -------------------------------- ...

  4. 【转】这五种方法前四种方法只支持IE浏览器,最后一个方法支持当前主流的浏览器(火狐,IE,Chrome,Opera,Safari)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. centos7防暴力破解五种方法

    什么是暴力破解,简单来说就是对一个服务器进行无数次尝试登陆,并用不同的密码进行登陆直到可以登陆成功.暴力破解的基本步骤可以分为以下几步: 1. 找到对应的linux服务器    Ip地址 2.扫描端口 ...

  6. [转]这五种方法前四种方法只支持IE浏览器,最后一个方法支持当前主流的浏览器(火狐,IE,Chrome,Opera,Safari)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. Linux 添加开机启动项的三种方法

    linux 添加开机启动项的三种方法. (1)编辑文件 /etc/rc.local 输入命令:vim /etc/rc.local 将出现类似如下的文本片段: #!/bin/sh## This scri ...

  8. html table表格导出excel的方法 html5 table导出Excel HTML用JS导出Excel的五种方法 html中table导出Excel 前端开发 将table内容导出到excel HTML table导出到Excel中的解决办法 js实现table导出Excel,保留table样式

    先上代码   <script type="text/javascript" language="javascript">   var idTmr; ...

  9. HTML用JS导出Excel的五种方法

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

随机推荐

  1. 运行时异常RuntimeException捕获的小测试

    public class ExceptionTest { public static void main(String[] args) throws InterruptedException { ne ...

  2. 使用NPOI或EPPlus来导出Excel文件实例,可在Excel文件加密

    使用NPOI.dll组件来导出Excel文件,并设置样式,Nuget引用即可. packages\NPOI.2.1.3.1\lib\net20\NPOI.dll #region Excel prote ...

  3. pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

    使用pytesseract识别验证码中遇到异常如下: pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installe ...

  4. Linux shell字符串操作

    #!/bin/bash #镜像名 image="asr-server" #镜像版本 ver="5.2.1" #容器名 dname="asr" ...

  5. 转 oracle 正则表达式和查询最大文件号 SQL

    ###sample 1 https://www.cnblogs.com/lxl57610/p/8227599.html Oracle使用正则表达式离不开这4个函数: 1.regexp_like 2.r ...

  6. springboot docker 部署

    1.新建一个最简单的springboot项目 https://code.aliyun.com/859143303/hello-world.git 2.src/main/docker下新建Dockerf ...

  7. ODAC Developer Downloads - Oracle Universal Installer

    https://www.baidu.com/link?url=BL2vRNMWap6AFJcmsFCEKi3KxZ2SgmtVJoihVKhF-SPgzvJHNkbk_j7nz1HdtAAWZ22NM ...

  8. 解决VM Workstation安装VMware Tools显示灰色的办法

    其实虚拟机用了好多次了,但是每次使用配置时还是忘这忘那的,这里就简单地再啰嗦下了. 解决办法如下: 1.关闭虚拟机: 2.在虚拟机设置分别设置CD/DVD.CD/DVD2和软盘为自动检测三个步骤: 3 ...

  9. Android File Transfer Mac: 如何在 macOS 和 Android 系统之间移动数据

    三大 Mac OS X 系统 Android 文件传输软件 谷歌出品的 Android File Transfer 如何在 Mac 系统上使用 Android File Transfer Androi ...

  10. distinct 数组去重,对象去重

    distinct 操作符可以用来去重,将上游重复的数据过滤掉. import { of } from 'rxjs'; import { distinct} from 'rxjs/operators'; ...