一、服务的分类
独立的服务是直接放在内存中的,访问速度快,但是比较耗内存
xinetd服务也是放在内存中,但是基于xinetd管理的服务并不在内存中,需要通过xinetd服务来提供服务
chkconfig查询已安装的服务
chkconfig命令只能查看 RPM包安装的服务,并不能查看源码包安装的服务
service XXX start也只能启动RPM包安装的软件,因为service命令就是到指定目录下去找启动脚本的
RPM包安装后安装信息会分配到系统默认的一些地方去,比如配置文件会放到/etc/下,启动脚本文件会放到/etc/init.d/目录下,在/etc/rc.d/init.d/目录中建立了软链接,在删除的时候比较难删干净,一般采用rpm -e进行删除
源码包安装后所有这个软件服务相关的文件都放在一个指定的目录中,一般是/usr/local/下面的目录中,直接干掉该目录即可完全删除该软件的安装信息
二、RPM服务的管理
1 rpm包安装完后相关文件的存储位置:
2 rpm包中独立服务的启动
1./etc/init.d/xxx独立服务名 start|stop|status|restart
2.service xxx独立服务名 start|stop|status|restart
service命令启动本质上也是“/etc/init.d/xxx独立服务名”启动,是红帽开发的用于简化服务管理的命令,所以如果不是红帽系列的linux操作系统,service命令很可能不能用,因此推荐使用第一种方式进行服务管理
[root@wenhaijin ~]# /etc/init.d/httpd status
httpd (pid 3677) is running...
[root@wenhaijin ~]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@wenhaijin ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@wenhaijin ~]#
service --status-all命令会列出所有rpm包形式安装的服务状态
[root@wenhaijin ~]# service --status-all
abrt-ccpp hook is not installed
abrtd is stopped
abrt-dump-oops is stopped
acpid is stopped
Aegis is running
status: Unknown job: agentwatch
atd is stopped
auditd (pid 6600 1559) is running...
Checking for service cloud-init:Checking for service cloud-init:Checking for service cloud-init:Checking for service cloud-init:Checking for service cloud-init-upgrade:cpuspeed is stopped
cupsd (pid 1092) is running...
hald is stopped
htcacheclean is stopped
httpd (pid 4045) is running...
ip6tables: Firewall is not running.
iptables: Firewall is not running.
irqbalance is stopped
Kdump is not operational
lvmetad is stopped
mdmonitor is stopped
messagebus is stopped
SUCCESS! MySQL running (20817)
ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists
netconsole module not loaded
Configured devices:
lo eth0
Currently active devices:
lo eth0
nscd (pid 1112) is running...
ntpd (pid 1275) is running...
portreserve is stopped
Process accounting is disabled.
quota_nld is stopped
rdisc is stopped
rngd is stopped
rsyslogd (pid 792) is running...
sandbox is stopped
saslauthd is stopped
[Running Status]
ServerSpeeder is running!
version 3.10.61.0
[License Information]
License 48299C7A5E8A5436 (valid on current device)
MaxSession unlimited
MaxTcpAccSession unlimited
MaxBandwidth(kbps) unlimited
ExpireDate 2034-12-31
[Connection Information]
TotalFlow 59
NumOfTcpFlows 59
TotalAccTcpFlow 57
TotalActiveTcpFlow 19
[Running Configuration]
accif eth0
acc 1
advacc 1
advinacc 0
wankbps 1000000
waninkbps 1000000
csvmode 0
subnetAcc 0
maxmode 1
pcapEnable 0
ShadowsocksR is running with PID 1527
smartd is stopped
openssh-daemon (pid 1267) is running...
svnserve is stopped
/etc/init.d/ttdfwwxyvs: line 19: /usr/bin/ttdfwwxyvs: No such file or directory
[root@wenhaijin ~]#
3 独立服务的自启动
3.1 通过chkconfig [--level 运行级别] [独立服务名] [on|off]
[root@wenhaijin ~]# chkconfig --list | grep httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@wenhaijin ~]# chkconfig --level 2345 httpd on
[root@wenhaijin ~]# chkconfig --list | grep httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@wenhaijin ~]# chkconfig httpd off
[root@wenhaijin ~]# chkconfig --list | grep httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
3.2 修改/etc/rc.d/rc.local文件(推荐使用)
###以下两个文件是一个软链接,修改哪个都行
[root@wenhaijin ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 220 Jul 10 2014 /etc/rc.d/rc.local
[root@wenhaijin ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Aug 14 2014 /etc/rc.local -> rc.d/rc.local
[root@wenhaijin ~]#
该文件是linux系统本身的服务都启动之后,在输入用户名和密码登入系统之前,linux会执行该文件中的脚本
将apache服务的启动命令写入该脚本,即可实现httpd服务的开机自启动
###加入/etc/rc.d/init.d/httpd start达到apache自启动目的
[root@wenhaijin ~]# vim /etc/rc.d/rc.local
#!/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
/etc/rc.d/init.d/httpd start
该脚本中有一句默认的命令:touch /var/lock/subsys/local
该文件的意思是开机启动前先查看有没有该文件,如果没有则创建,如果有则改变该文件的最后修改时间,因此可以通过查看该文件的最后修改时间来确定系统的最后启动时间
3.3 使用ntsysv管理自启动
该命令不仅能管理独立软件服务自启动,还能管理基于xinetd服务的自启动,但是依然无法处理源码包安装的服务
执行该命令后进入如下图形界面,对于某个具体的RPM服务,如果想自启动则在他前面加"*",不想自启动,就将该"*"去掉即可
该命令也是红帽专有命令,并不适用于所有操作系统
4 基于xinetd服务管理
4.1 xinetd服务及基于xinetd的telnet服务安装
xinetd服务的安装
[root@wenhaijin ~]# yum -y install xinetd
通过telnet服务的安装来演示基于xinetd服务管理
###linux本身是安装了telnet客户端的,执行下面的语句跟新了telnet客户端服务
[root@wenhaijin ~]# yum -y install telnet
Loaded plugins: security
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package telnet.x86_64 1:0.17-47.el6_3.1 will be updated
---> Package telnet.x86_64 1:0.17-48.el6 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Updating:
telnet x86_64 1:0.17-48.el6 base 58 k
Transaction Summary
================================================================================
Upgrade 1 Package(s)
Total download size: 58 k
Downloading Packages:
telnet-0.17-48.el6.x86_64.rpm | 58 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : 1:telnet-0.17-48.el6.x86_64 1/2
Cleanup : 1:telnet-0.17-47.el6_3.1.x86_64 2/2
Verifying : 1:telnet-0.17-48.el6.x86_64 1/2
Verifying : 1:telnet-0.17-47.el6_3.1.x86_64 2/2
Updated:
telnet.x86_64 1:0.17-48.el6
Complete!
###安装telnet服务器端,telnet server 是不安全的,所以linux默认是没有安装的
[root@wenhaijin ~]# yum -y install telnet-server
Loaded plugins: security
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package telnet-server.x86_64 1:0.17-48.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
telnet-server x86_64 1:0.17-48.el6 base 37 k
Transaction Summary
================================================================================
Install 1 Package(s)
Total download size: 37 k
Installed size: 54 k
Downloading Packages:
telnet-server-0.17-48.el6.x86_64.rpm | 37 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : 1:telnet-server-0.17-48.el6.x86_64 1/1
Verifying : 1:telnet-server-0.17-48.el6.x86_64 1/1
Installed:
telnet-server.x86_64 1:0.17-48.el6
Complete!
[root@wenhaijin ~]#
telnet服务端口(23)默认是没有开启的
[root@wenhaijin ~]# netstat -tlun
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:9092 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:137 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:59097 0.0.0.0:* LISTEN
udp 0 0 0.0.0.0:137 0.0.0.0:*
udp 0 0 0.0.0.0:631 0.0.0.0:*
udp 0 0 172.18.69.197:123 0.0.0.0:*
udp 0 0 127.0.0.1:123 0.0.0.0:*
udp 0 0 0.0.0.0:123 0.0.0.0:*
[root@wenhaijin ~]#
4.2 xinetd服务的启动
telnet服务并不是独立的服务,所以无法通过service直接启动
[root@wenhaijin ~]# service telnet restart
telnet: unrecognized service
[root@wenhaijin ~]#
[root@wenhaijin ~]#
###将基于xinetd服务的disable状态改为no就会在启动xinetd服务的时候启动该服务(disable是不可用的意思)
[root@wenhaijin ~]# vim /etc/xinetd.d/telnet
# default: on
# description: The telnet server serves telnet sessions; it uses \
# unencrypted username/password pairs for authentication.
service telnet
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
disable = no
}
~
~
~
"/etc/xinetd.d/telnet" 14L, 304C written
[root@wenhaijin ~]#
修改完之后重启 xinetd服务
[root@wenhaijin ~]# service xinetd restart
Stopping xinetd: [FAILED]
Starting xinetd: [ OK ]
[root@wenhaijin ~]#
重启后23号端口(telnet的默认端口)已经开启
[root@wenhaijin ~]# netstat -tlun
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:9092 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:137 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:23 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:59097 0.0.0.0:* LISTEN
udp 0 0 0.0.0.0:137 0.0.0.0:*
udp 0 0 0.0.0.0:631 0.0.0.0:*
udp 0 0 172.18.69.197:123 0.0.0.0:*
udp 0 0 127.0.0.1:123 0.0.0.0:*
4.3 基于xinetd服务的自启动
可以使用chkconfig telnet on开启telnet服务的自启动功能
也可以
注意:通过chkconfig来开启或关闭自启动服务会影响到当前服务的启动状态,
通过修改/etc/xinetd.d/telnet文件来更改基于xinetd服务状态也会同时更改该服务的自启动状态,也就是说基于xinetd服务启动和自启动是想通的,这一点与我们常规思路不同
三、源码包服务管理
1.源码包服务的启动
使用绝对路径,调用启动脚本来启动。不同源码包的启动脚本不同。可以查看源码包的安装说明,查看启动脚本的方法。
2.源码服务的自启动
源码包的自启动方法与rpm包自启动的第二种方式是一样的
3. 让源码包服务被服务管理命令识别
3.1让源码包服务能被service命令识别
可以通过创建软链接的形式将启动脚本链接到/etc/init.d目录下,service命令会读取该目录下的文件
这种方式不推荐使用,容易将源码包服务和rpm包服务混为一谈,会将自己搞晕
3.2 让源码包服务能被chkconfig与ntsysv命令识别
与源码包安装的apache为例
首先要在/etc/init.d/目录下找到对应的源码包服务文件,然后加入两行注释(虽然是注释,但是不能省略,而且需要遵循一定的规则)
然后将apache源码包服务加入chkconfig命令管理系列
这样,源码包安装的apache服务就能被chkconfig与ntsysv命令识别了,这种方式也不太推荐使用,容易将自己搞晕,如果不会将自己搞晕,是完全可以这么做的。
四、小结:不同类型的软件服务具有不容的启动管理和自启动管理命令