Linux:检查当前运行级别的五种方法

运行级就是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打印系统的上一个和当前运行级别:

[[email protected] linuxidc.com]$ runlevel

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

2、使用who命令

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

3、使用systemctl命令

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

[[email protected] linuxidc.com]$ systemctl get-default
graphical.target

4、使用/etc/inittab文件

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

CentOS 6是通过修改/etc/inittab来修改默认运行级别的:

[[email protected] ~]# 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结果如下:

[[email protected] 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系统:

[[email protected] linuxidc.com]$ cat /etc/systemd/system/default.target

原文地址:https://www.cnblogs.com/linuxprobe-sarah/p/10941686.html

时间: 2024-07-28 14:44:59

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

linux关机重启五种方法(sysrq shutdown halt init ipmi)

linux关机重启五种方法(sysrq shutdown halt init) 当前版本 cat /etc/debian_version 5.0.1 一.linux关机命令(前三种) 1.shutdown -h now(-h:halt after shutdown.) halt init 0 2.linux重启命令 shutdown -r(-r:reboot after shutdown.) reboot init 6 二.特殊情况重启和关机(能ping通 上述命令无法使用情况下 也就是系统挂起

五种方法实现Linux批量重命名文件

五种方法实现Linux批量重命名文件 Linux批量重命名文件是指对某些特定的文件统一进行重新命名,以改变原来一批文件的名称,这里介绍五种方法来实现. AD: Linux批量重命名文件会涉及到改变一个字母.改变一些相连字母.改变某些位置的字母.在最前面加上某些字母.或者改变字母的大小写.完成这里五个方法基本上就会解决了Linux批量重命名的工作. 1.我想把它们的名字的第一个1个字母变为"q",其它的不变 [[email protected] mailqueue]# for i in

Linux 7个运行级别(0:关机,停机模式、1:单用户模式、2:多用户模式、3:完整的多用户文本模式、4:系统未使用,保留一般不用、5:图形化模式、6:重启模式)、重置root密码方法

init是Linux系统操作中不可缺少的程序之一.init进程,它是一个由内核启动的用户级进程.内核会在过去曾使用过init的几个地方查找它,它的正确位置(对Linux系统来说)是/sbin/init.如果内核找不到init,它就会试着运行/bin/sh,如果运行失败,系统的启动也会失败. Linux 7个运行级别(0:关机,停机模式.1:单用户模式.2:多用户模式.3:完整的多用户文本模式.4:系统未使用,保留一般不用.5:图形化模式.6:重启模式),运行级就是操作系统当前正在运行的功能级别.

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

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>html 表格导出道</title> <script language="JavaScript" type="text/javascript"> //第一种方法 function method1(table

设置数据库兼容级别的两种方法以及区别

转:http://blog.csdn.net/htl258/article/details/5696325 --设置数据库兼容级别的两种方法 --以设置兼容SQL Serve 2005 为例 --法一: ALTER DATABASE database_name SET COMPATIBILITY_LEVEL = 90 GO --法二: EXEC sp_dbcmptlevel database_name,90 GO database_name 要修改为数据库的名称. 各参数值对应的数据库版本: 8

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

linux 添加开机启动项的三种方法. (1)编辑文件 /etc/rc.local 输入命令:vim /etc/rc.local 将出现类似如下的文本片段: #!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V sty

Linux开机自动加载的几种方法

linux 添加开机启动项的三种方法. (1)编辑文件 /etc/rc.local 输入命令:vim /etc/rc.local 将出现类似如下的文本片段: #!/bin/sh #This script will be executed after all the other init scripts.#You can put your own initialization stuff in here if you don't#want to do the full Sys V style in

Linux中执行shell脚本的4种方法

这篇文章主要介绍了Linux中执行shell脚本的4种方法总结,即在Linux中运行shell脚本的4种方法,需要的朋友可以参考下. bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/root/bin目录中并已有执行权限(添加权限的方法:chmod +x hello.sh). 方法一:切换到shell脚本所在的目录(此时,称为工作目录)执行shell脚本: ./ 的意思是说在当前的工作目录下执行hello.sh.如果不加上

js去掉字符串前后空格的五种方法

第一种:循环检查替换[javascript]//供使用者调用  function trim(s){  return trimRight(trimLeft(s));  }  //去掉左边的空白  function trimLeft(s){  if(s == null) {  return "";  }  var whitespace = new String(" \t\n\r");  var str = new String(s);  if (whitespace.i