CentOS6/7开机启动配置

  • CentOS6/7开机启动配置已关闭评论
  • 124 次浏览
  • A+
所属分类:linux技术
摘要

最近在配置Linux系统的ntp校时,涉及到开机启动问题,总结一下chkconfig 是设置服务在某个运行级别的自动启动状态,如果在某个运行级别状态为on,那么当系统下次进入这个运行级别,就会自动启动这个服务。

最近在配置Linux系统的ntp校时,涉及到开机启动问题,总结一下

两个环境:

CentOS release 6.5 (Final) CentOS Linux release 7.9.2009 (Core)

centos6.5 设置开机启动使用chkconfig方式

chkconfig 是设置服务在某个运行级别的自动启动状态,如果在某个运行级别状态为on,那么当系统下次进入这个运行级别,就会自动启动这个服务。

语法格式:

chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代号>][系统服务][on/off/reset]

参数解析:

参数 说明
--add 增加所指定的系统服务,让 chkconfig 指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。
--del 删除所指定的系统服务,不再由 chkconfig 指令管理,并同时在系统启动的叙述文件内删除相关数据。
--list 查看系统服务状态
--level 指定读系统服务要在哪一个执行等级中开启或关毕

例子:

查看ntpd服务,如果服务没有被加入到chkconfig,先用-add加入

[root@dsview ~]# chkconfig --list ntpd ntpd           	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭

这里显示的0 1 2 3 4 5 6 是级别的意思,查看当前运行级别,使用runlevel命令

[root@dsview ~]# runlevel  N 3

设置开机启动ntpd服务:chkconfig ntpd on

[root@dsview ~]# chkconfig ntpd on [root@dsview ~]# chkconfig --list ntpd ntpd           	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭 [root@dsview ~]#

可以看到2 3 4 5 级别状态改为启用了,重启一下系统查看ntpd状态

[root@dsview ~]# service ntpd status ntpd (pid  1378) 正在运行...

设置开机不启动ntpd服务:chkconfig ntpd off

[root@dsview ~]# chkconfig ntpd off [root@dsview ~]# chkconfig --list ntpd ntpd           	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭

 centos7.9设置开机启动使用systemctl方式

systemctl命令来自于英文词组"system control"的缩写,其功能是用于管理系统服务。从RHEL/CentOS7版本之后初始化进程服务init被替代成了systemd服务,systemd初始化进程服务的管理是通过systemctl命令完成的,从功能上涵盖了之前service、chkconfig、init、setup等多条命令的大部分功能。

语法格式

systemctl [参数] [服务]

参数解析

参数 说明
start 启动服务
stop 停止服务
restart 重启服务
enable 使某服务开机自启
disable 关闭某服务开机自启
status 查看服务状态
list-units --type=service 列举所有已启动服务

例子

查看chronyd服务开机启动状态:systemctl list-unit-files |grep chronyd

[root@NTSServer ~]# systemctl list-unit-files |grep chronyd chronyd.service                               disabled

可以看到chronyd服务状态为disabled禁止的,意思是没有开机启动。

将chronyd服务加入开机启动项中: systemctl enable chronyd.service

[root@NTSServer ~]# systemctl enable chronyd.service [root@NTSServer ~]# systemctl list-unit-files |grep chronyd chronyd.service                               enabled

将chronyd服务从开机启动项中删除:systemctl disable chronyd.service

[root@NTSServer ~]# systemctl disable chronyd.service Removed symlink /etc/systemd/system/multi-user.target.wants/chronyd.service. [root@NTSServer ~]# systemctl list-unit-files |grep chronyd chronyd.service                               disabled

 还是systemctl好用!