linux runlevel运行级别

1、linux通过设定runlevel来设定系统使用不同的服务启动,从而使得linux运行的环境有所不同。

2、当系统启动后会按照以下步骤完成初始化:运行/sbin/init程序加载器配置文件/etc/inittab,根据配置文件来决定要以哪个/etc/rc.d/rc#.d脚本,完成初始化

(/sbin/init→/etc/inittab→/etc/rc.d/rc#.d)。

3、/etc/inittab文件内容如下  

# 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)   ####多用户模式但是没有NFS
#   3 - Full multiuser mode   ###完全的多用户模式,含有网络功能的纯文本模式,可以设置成默认运行级别
#   4 - unused   ####系统保留
#   5 - X11     ####与runlevel3类似,但是可以使用xwindows,可以设置成默认运行级别
# 6 - reboot (Do NOT set initdefault to this) ###系统重启,不能将其设置成默认运行级别

id:
5:initdefault:

  ①文件中有0-6七个运行级别

  ②id:5:initdefault: 代表开机后系统默认运行与5级别,并使用/etc/rc.d/rc5.d/*完成运行环境初始化

  ③切换系统运行级别:runlevel # 

    例如runlevel 35

  ④查看运行级别:who -r或runlevel

4、/etc/rc.d/rc5.d/目录下有以下文件

  ①K开头的文件代表系统初始化时要关闭的服务

  ②S开头的文件代表系统初始化时要启动的服务

  ③这些文件都被链接至/etc/init.d/目录下

  

5、/etc/rc.d/rc#.d/*下的脚本被称为rc脚本,并且他们都被链接至了/etc/init.d/下。

  ①rc脚本可以接受参数   {start | stop | restart | status },从而控制服务

  ②当使用yum安装完服务时,我们使用/etc/init.d/SERVER_NAME  {start | stop | restart | status }来改变服务运行状态。

  ③之所以,在centos6上使用service SERVER_NAME  {start | stop | restart | status }来改变服务运行状态,是因为service命令能够找到/etc/init.d/下的服务名,以致可以用  service命令,改变服务运行状态。

6、那么开机自启是如何实现的呢?

  其实,要达到开机自启的目的,需要将/etc/init.d/的服务链接至 /erc/rc.d/rc#.d/下。创建链接不必使用ln,使用装有命令chkconfig即可。

7、chkconfig命令使用语法

NAME
       chkconfig - updates and queries runlevel information for system services

SYNOPSIS
       查看:chkconfig [--list] [name]
       添加:chkconfig --add name
       删除:chkconfig --del name
       修改指定的链接类型:chkconfig [--level levels]  name <on|off|reset|resetpriorities>

  示例,将vsftpd添加到自启动。

[root@localhost rc5.d]# service httpd status 
httpd.worker (pid  3076) 正在运行...
[root@localhost rc5.d]# ls /etc/init.d/httpd
/etc/init.d/httpd
[root@localhost rc5.d]# ls /etc/rc.d/rc5.d/|grep httpd  ###可以看到httpd是以K开头的即开机后不会自动启动
K15httpd
[root@localhost rc5.d]# chkconfig httpd on              ###使用chkconfig命令设置开机自启
[root@localhost rc5.d]# ls /etc/rc.d/rc5.d/|grep httpd
S85httpd

补充知识。

  当系统一句/etc/rc.d/rc#.d/*,初始化完成后,还会去读取/etc/rc.d/rc/local(链接至/etc/rc.local)。一些不便或不需要写服务脚本的程序,若希望它能开机自动运行,可以将其写入rc.local脚本中。  

  

  

posted on 2018-11-23 11:29  HowOldAreYou  阅读(595)  评论(0编辑  收藏  举报

导航