10.Linux服务器管理
10.1.服务分类
1、服务的分类
Linux服务分立 2种:
RPM包默认安装的服务
源码包安装的服务
RPM包默认安装的服务又分为2种:
独立的服务
基于xinetd服务
2、启动与自启动
服务启动:就是在当前系统中让服务运行,并提供功能。
服务自启动:自启动是指让服务在系统开机或重启之后,随着系统的启动而自启动服务。
3、查询已安装的服务
RPM包安装的服务
时间看服务自启动状态,可以看到所有RPM安装的服务,但不能看源码包安装的服务。
chkconfig –list
查看运行的进程:
ps aux
源码包安装的服务,查看服务位置,一般时/usr/local/下。
源码安装包和RPM包的默认安装位置不同,导致源码包的服务没有命令可以查询,只能通过目录查看
service查找服务时的配置文件,从这个目录中/etc/rc.d/init.d查找要管理的服务。
[root@localhost init.d]# ls
functions netconsole network README
[root@localhost init.d]# pwd
/etc/rc.d/init.d
10.2.独立服务的管理
1、独立服务
配置文件 |
作用 |
/etc/init.d/ |
启动脚本位置 |
/etc/sysconfig/ |
初始化环境配置文件位置 |
/etc/ |
配置文件位置 |
/etc/xinetd.conf |
xinetd配置文件 |
/etc/xinetd.d/ |
基于xinetd服务的启动脚本 |
/var/lib/ |
服务产生的灵气放在这里 |
/var/log/ |
日志 |
/etc/init.d和/etc/rc.d/init.d是软链接,是同样的内容
所有的RPM安装包的启动脚本都在这里。
2、独立服务的启动
两种方式:
使用绝对路径:
/etc/init.d/独立服务名 start|stop|status|restart
使用service命令
service 独立服务名 start|stop|status|restart
3、独立服务的自启动
第1种方法:使用chkconfig命令
chkconfig [--level 运行级别] [独立服务名] [on|off]
配置apache服务自启动,2345是默认的,可以不指定
chkconfig --level 2345 httpd on
取消自启动
chkconfig httpd off
第2种方法:修改/etc/rc.d/rc.local文件
这个文件在系统每次启动后,会执行这个脚本文件
[root@localhost rc.d]# cat rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
文件末尾的touch命令并不定代表是创建新文件,如果文件存在的话,是不会去创建的只会更新一下访问时间。
如果在文件末尾加上启动apache服务的命令,这样每次开机就可以自启动了。
service httpd start
第3种方法:使用ntsysv命令管理自启动
类似setup的一种管理工具,是redhat特有的功能。
推荐使用第2种管理办法,易查看
10.3.基于xinetd服务管理
1、安装xinetd和telnet
yum -y install xinetd
yum -y install telnet
远程管理不再使用telnet,不是非常安全,这里用作演示,做完演示就删除掉。
目前基于xinetd服务越来越少了,大家可了解。
启动时报错
[root@localhost rc.d]# service telnet status
Redirecting to /bin/systemctl status telnet.service
Unit telnet.service could not be found.
2、xinetd服务的启动
编辑服务启动配置:
[root@localhost xinetd.d]# pwd
/etc/xinetd.d
[root@localhost xinetd.d]# vim telnet
配置内容:
#服务名称为telnet
service telnet
{
flags = REUSE #标志为REUST,设定TCP/IP socket可重用
socket_type = stream #使用TCP协议数据包
wait = no #允许多个连接同时连接
user = root #启动服务的用户为root
server = /usr/bin/telnet #服务的启动程序
log_on_failure += USERID #登录失败后,记录用户的ID
disable = no #服务启动
}
重启xinetd服务,即重启了telnet
3、xinetd服务的自启动
两种方法:
chkconfig telnet on
ntsysv
这节的教程和我的centos7完全不一样啊。。。。怎么都找不到telnet的监听
去网上查使用方法吧。
10.4.源码包服务的管理
1、源码包安装服务的启动
使用绝对路径,调用启动脚本来启动。不同的源码包的启动脚本不同。可以查看源码的安装说明,查看启动脚本的方法。
如下命令启动或者关闭apache服务
/usr/local/apache/bin/apachectl start|stop
2、源码包服务的自动
在/etc/rc.d/rc.local加入apache服务的启动命令即可
/usr/local/apache/bin/apachectl start
3、让源码包服务被服务管理命令识别
创建一个apache启动脚本的软连接到目录/etc/init.d/即可
ls –s /usr/local/apache/bin/apachectl /etc/init.d/apache
4、让chkconfig与ntsysv识别源码包服务
vim /etc/init.d/apache
# chkconfig 35 86 76
# 指定http脚本可以被chkconfig命令管理。格式是:
# chkconfig: 运行级别 启动顺序 关闭顺序
#description:source package apache
#说明,内容随意
启动顺序 关闭顺序不能和现有的有冲突,如下所示:
如下服务启动顺序为10,关闭顺序为50
[root@localhost rc.d]# ll rc2.d/ rc3.d/
rc2.d/:
total 0
lrwxrwxrwx. 1 root root 20 Apr 22 16:01 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 17 Apr 22 16:01 S10network -> ../init.d/network
rc3.d/:
total 0
lrwxrwxrwx. 1 root root 20 Apr 22 16:01 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 17 Apr 22 16:01 S10network -> ../init.d/network