Centos6和Centos7的服务启动等差别

CentOS 6 的服务管理机制是 systemv,采用 service 命令来管理所有的服务。

查询已经安装的服务和区分服务
Linux 服务区分 RPM 包默认安装的服务和源码包安装的服务。源码包安装的服务是不能被服务管理命令直接找到的,而且一般会安装到 /usr/local/ 目录中。
也就是说,在 /usr/local/ 目录中的服务都应该是通过源码包安装的服务。RPM 包默认安装的服务都会安装到系统默认位置,所以是可以被服务管理命令(如 service、chkconfig)识别的。

而RPM 包默认安装的服务又区分独立的服务和基于 xinetd 的服务

**独立的服务:**就是独立启动的意思,这种服务可以自行启动,而不用依赖其他的管理服务。因为不依赖其他的管理服务,所以,当客户端请求访问时,独立的服务响应请求更快速。目前,Linux 中的大多数服务都是独立的服务,如 apache 服务、FTP 服务、Samba 服务等。

**基于 xinetd 的服务:**这种服务就不能独立启动了,而要依靠管理服务来调用。这个负责管理的服务就是 xinetd 服务。xinetd 服务是系统的超级守护进程,其作用就是管理不能独立启动的服务。当有客户端请求时,先请求 xinetd 服务,由 xinetd 服务去唤醒相对应的服务。当客户端请求结束后,被唤醒的服务会关闭并释放资源。这样做的好处是只需要持续启动 xinetd 服务,而其他基于 xinetd 的服务只有在需要时才被启动,不会占用过多的服务器资源。

chkconfig 是管理 RPM 包默认安装的服务的自启动的命令,这里仅利用这条命令的查看功能。使用这条命令还能看到 RPM 包默认安装的所有服务。命令格式如下:
[root@localhost ~]# chkconfig --list [服务名]
选项:
–list:列出 RPM 包默认安装的所有服务的自启动状态;

启动管理

(1)使用service命令来启动独立的服务
在 CentOS 系统中,我们还可以依赖 service 命令来启动独立的服务。service 命令实际上只是一个脚本,这个脚本仍然需要调用 /etc/init.d/ 中的启动脚本来启动独立的服务。而且 service 命令是红帽系列 Linux 的专有命令,其他的 Linux 发行版本不一定拥有这条命令,所以我们并不推荐使用 service 命令来启动独立的服务。

service 命令格式如下:
[root@localhost ~]# service 独立服务名 start|stop|restart|…
start:启动服务;
stop:停止服务;
status:查看服务状态;
restart:重启动服务;

例如:
[root@localhost ~]# service httpd restart
停止httpd:
[确定]
正在启动httpd:
[确定]

(2)使用/etc/init.d/目录中的启动脚本来启动独立的服务
既然所有独立服务的启动脚本都存放在 /etc/init.d/ 目录中,那么,调用这些脚本就可以启动独立的服务了。这种启动方式是推荐启动方式,命令格式如下:
[root@localhost ~]#/etc/init.d独立服务名 start| stop|status|restart|…
参数:
start:启动服务;
stop:停止服务;
status:查看服务状态;
restart:重启动服务;

以启动 RPM 包默认安装的 httpd 服务为例,命令如下:
[root@localhost ~]# /etc/init.d/httpd start
正在启动httpd:
[确定]
#启动httpd服务
[root@localhost ~]# /etc/init.d/httpd status
httpd (pid 13313)正在运行…
#查询httpd服务状态,并能够看到httpd服务的PID
[root@localhost ~]#/etc/init.d/httpd stop
停止 httpd:
[确定]
#停止httpd服务
[root@localhost ~]#/etc/init.d/httpd restart
停止httpd:
[失败]
正在启动httpd:
[确定]
重启动httpd服务

从 CentOS 7 开始,服务从原来的由 systemv 管理机制升级到了systemd,统一采用 systemctl 命令来管理所有的服务。

在centos7下安装完apache后,想要启动apache,执行了/etc/init.d/httpd start命令,但是出现了没有文件或目录。
在chkconfig –list 和 /etc/init.d/ 下已经找不到已安装的服务
centos 停止smbd服务 centos6停止服务有什么影响_启动服务
而chkconfig –list已经提示执行systemctl list-unit-files

centos7 已经切换到 systemd,系统指令也有所变化。之前用于启动、重启、停止各种服务的 service 作为向后兼容的指令还能使用。但同时,chkconfig 也改成了 systemctl 了。如一些常用的对应于 service 和 chkconfig 的新的 systemctl 指令。

在 CentOS 7(或 RHEL 7)系统中,虽然可以使用 service 指令。
如:
[root@localhost ~]# service network restart
Restarting network (via systemctl): [ OK ]

[root@localhost ~]# service httpd restart
Redirecting to /bin/systemctl restart httpd.service

[root@localhost ~]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service

但是系统会自动重定向该指令到新的指令 /bin/systemctl 来执行,并给出提示。

使用systemctl命令:
如:
启动服务:
systemctl start httpd

停止服务:
systemctl stop httpd

重启服务(先停止,后启动):
systemctl restart httpd

重新加载(使用新的配置文件):
systemctl reload httpd

显示服务状态:
systemctl status httpd

开机自启动:
systemctl enable httpd

用于设定系统启动时自动运行某服务的指令 chkconfig 改成systemctl。

chkconfig service on
systemctl enable httpd

chkconfig service off
systemctl disable httpd

列举出所有服务的指令
chkconfig –list
systemctl list-unit-files --type=service