Three methods for CentOS to set service startup
Use the chkconfig command to configure the boot level
Under CentOS or other RedHat systems, if it is a service installed later, such as httpd, mysqld, postfix, etc., the system will not start automatically by default after installation. Even if the service is started manually by executing /etc/init.d/mysqld start, the system will not automatically start the service as long as the server is restarted. At this time, we need to make a setting after installation to let the system automatically start these services to avoid unnecessary losses and troubles. In fact, the command is very simple, just use chkconfig.
Query all current services of chkconfig
To query the automatic startup configuration of all current services, you can use chkconfig --list. If you want to query a specified service, you can keep up with the service name, for example, to query the sshd service.
[root@localhost ~]# chkconfig --list NetworkManager 0:off 1:off 2:on 3:on 4:on 5:on 6:off abrt-ccpp 0:off 1:off 2:off 3:on 4:off 5:on 6:off abrt-oops 0:off 1:off 2:off 3:on 4:off 5:on 6:off abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off avahi-daemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off bluetooth 0:off 1:off 2:off 3:on 4:on 5:on 6:off certmonger 0:off 1:off 2:off 3:on 4:on 5:on 6:off cpuspeed 0:off 1:on 2:on 3:on 4:on 5:on 6:off crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off cups 0:off 1:off 2:on 3:on 4:on 5:on 6:off dnsmasq 0:off 1:off 2:off 3:off 4:off 5:off 6:off fcoe 0:off 1:off 2:on 3:on 4:on 5:on 6:off firstboot 0:off 1:off 2:off 3:off 4:off 5:off 6:off haldaemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off ipsec 0:off 1:off 2:off 3:off 4:off 5:off 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off irqbalance 0:off 1:off 2:off 3:on 4:on 5:on 6:off iscsi 0:off 1:off 2:off 3:on 4:on 5:on 6:off iscsid 0:off 1:off 2:off 3:on 4:on 5:on 6:off kdump 0:off 1:off 2:off 3:off 4:off 5:off 6:off livesys 0:off 1:off 2:off 3:on 4:on 5:on 6:off livesys-late 0:off 1:off 2:off 3:on 4:on 5:on 6:off lldpad 0:off 1:off 2:on 3:on 4:on 5:on 6:off lvm2-monitor 0:off 1:on 2:on 3:on 4:on 5:on 6:off mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off messagebus 0:off 1:off 2:on 3:on 4:on 5:on 6:off multipathd 0:off 1:off 2:off 3:off 4:off 5:off 6:off mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off network 0:off 1:off 2:off 3:off 4:off 5:off 6:off nfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off nfslock 0:off 1:off 2:off 3:on 4:on 5:on 6:off nscd 0:off 1:off 2:off 3:off 4:off 5:off 6:off nslcd 0:off 1:off 2:off 3:off 4:off 5:off 6:off ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off ntpdate 0:off 1:off 2:off 3:off 4:off 5:off 6:off oddjobd 0:off 1:off 2:off 3:off 4:off 5:off 6:off openct 0:off 1:off 2:on 3:on 4:on 5:on 6:off pcscd 0:off 1:off 2:off 3:on 4:on 5:on 6:off portreserve 0: off 1: off 2: on 3: on 4: on 5: on 6: off postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off psacct 0:off 1:off 2:off 3:off 4:off 5:off 6:off quota_nld 0:off 1:off 2:off 3:off 4:off 5:off 6:off rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off rngd 0:off 1:off 2:off 3:off 4:off 5:off 6:off rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off rpcgssd 0:off 1:off 2:off 3:on 4:on 5:on 6:off rpcidmapd 0:off 1:off 2:off 3:on 4:on 5:on 6:off rpcsvcgssd 0:off 1:off 2:off 3:off 4:off 5:off 6:off rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off smartd 0:off 1:off 2:off 3:off 4:off 5:off 6:off snmpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off snmptrapd 0:off 1:off 2:off 3:off 4:off 5:off 6:off spice-vdagentd 0:off 1:off 2:off 3:off 4:off 5:on 6:off sshd 0:off 1:off 2:off 3:off 4:off 5:off 6:off sssd 0:off 1:off 2:off 3:off 4:off 5:off 6:off svnserve 0:off 1:off 2:off 3:off 4:off 5:off 6:off sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off vboxadd 0:off 1:off 2:on 3:on 4:on 5:on 6:off vboxadd-service 0:off 1:off 2:on 3:on 4:on 5:on 6:off vboxadd-x11 0:off 1:off 2:off 3:on 4:off 5:on 6:off vncserver 0:off 1:off 2:off 3:off 4:off 5:off 6:off wdaemon 0:off 1:off 2:off 3:off 4:off 5:off 6:off wpa_supplicant 0:off 1:off 2:off 3:off 4:off 5:off 6:off ypbind 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@localhost ~]# [root@localhost ~]# chkconfig --list sshd sshd 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@localhost ~]#
Add service to chkconfig list
If the service has not already been added to the chkconfig list, it needs to be added now with the --add parameter:
chkconfig --add sshd
Set or cancel the automatic startup of the service
If you need to set the automatic startup of the service, you can set it through the on command of chkconfig, such as setting the sshd service to automatic startup:
[root@localhost ~]# chkconfig sshd on [root@localhost ~]# chkconfig --list sshd sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@localhost ~]#
At this time, 2~5 are all on, which means that it will start automatically. If you need to cancel the automatic startup of the service, you can cancel it through the off command of chkconfig, such as canceling the automatic startup of the sshd service:
[root@localhost ~]# chkconfig sshd off [root@localhost ~]# chkconfig --list sshd sshd 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@localhost ~]#
Modify the file /etc/rc.d/rc.local:
For example, the self-starting problems of these services such as apache, mysql, samba, and svn can be solved together:
vim /etc/rc.d/rc.local #Add the following commands #!/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 init stuff. touch /var/lock/subsys/local /usr/sbin/apachectl start /etc/rc.d/init.d/mysqld start /etc/rc.d/init.d/smb start /usr/local/subversion/bin/svnserve -d
On CentOS 7 you can use the systemctl command to make the service start by default
Taking docker.service as an example, the current settings are as follows:
[root@localhost ~]# systemctl status docker.service docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled) Active: inactive (dead) Docs: https://docs.docker.com [root@localhost ~]# systemctl list-unit-files | grep docker docker.service disabled docker.socket disabled [root@localhost ~]#
Then use the systemctl command to enable the docker service:
[root@localhost ~]# systemctl enable docker.service ln -s '/usr/lib/systemd/system/docker.service' '/etc/systemd/system/multi-user.target.wants/docker.service' [root@localhost ~]# systemctl list-unit-files | grep docker docker.service enabled docker.socket disabled [root@localhost ~]#
Then restart the server and you can see that the docker service is started:
[root@localhost ~]# systemctl list-unit-files | grep docker docker.service enabled docker.socket disabled [root@localhost ~]# systemctl status docker.service docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled) Active: active (running) since Fri 2015-12-11 10:18:35 CST; 32s ago Docs: https://docs.docker.com Main PID: 1218 (docker) CGroup: /system.slice/docker.service └─1218 /usr/bin/docker daemon -H fd:// Dec 11 10:18:09 localhost.localdomain docker[1218]: time="2015-12-11T10:18:09.045564389+08:00" level=warning msg="Usage of loopback devices is strongly discouraged for productio...v section." Dec 11 10:18:10 localhost.localdomain docker[1218]: time="2015-12-11T10:18:10.741368042+08:00" level=info msg="[graphdriver] using prior storage driver \"devicemapper\"" Dec 11 10:18:12 localhost.localdomain docker[1218]: time="2015-12-11T10:18:12.309207143+08:00" level=info msg="Firewalld running: true" Dec 11 10:18:30 localhost.localdomain docker[1218]: time="2015-12-11T10:18:30.584205823+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0...IP address" Dec 11 10:18:35 localhost.localdomain docker[1218]: time="2015-12-11T10:18:35.662157286+08:00" level=info msg="Loading containers: start." Dec 11 10:18:35 localhost.localdomain docker[1218]: .. Dec 11 10:18:35 localhost.localdomain docker[1218]: time="2015-12-11T10:18:35.780999775+08:00" level=info msg="Loading containers: done." Dec 11 10:18:35 localhost.localdomain docker[1218]: time="2015-12-11T10:18:35.781038888+08:00" level=info msg="Daemon has completed initialization" Dec 11 10:18:35 localhost.localdomain docker[1218]: time="2015-12-11T10:18:35.781068669+08:00" level=info msg="Docker daemon" commit=a34a1d5 execdriver=native-0.2 graphdriver=de...rsion=1.9.1 Dec 11 10:18:35 localhost.localdomain systemd[1]: Started Docker Application Container Engine. Hint: Some lines were ellipsized, use -l to show in full. [root@localhost ~]#