Linux服务管理
1、简介与分类
1.系统的运行级别
运行级别 | 含义 |
---|---|
0 | 关机 |
1 | 单用户模式,可以想象为Windows的安全模式,主要用于系统修复 |
2 | 不完全的命令行模式,不含NFS服务 |
3 | 完全的命令行模式,就是标准字符界面 |
4 | 系统保留 |
5 | 图形模式 |
6 | 重启动 |
运行级别命令
[root@AmorLei ~]# runlevel # 查看运行级别命令 N 3 [root@AmorLei ~]# init 运行级别 # 修改运行级别命令
系统默认运行级别
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@AmorLei ~]# vi /etc/inittab # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # upstart works, see init(5), init(8), and initctl(8). # # Default runlevel. The runlevels used are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:3:initdefault:
2.服务的分类
独立的服务:可以独立启动服务。其特点是:
- 可以自行独立启动,无需通过其他机制的管理
- 独立服务一旦启动加载到内存后,就会一直占用内存空间和系统资源,直到该服务被停止。
- 由于服务一直在运行,所以对client的请求有更快的响应速度。
基于xinetd服务:通过xinetd来负责启动、管理其它服务(逐渐淘汰)。其特点是:
- 所有的服务由xinetd控管,因此对xinetd可以有安全控管的机制,如网络防火墙
- clinet请求前,所需服务是未启动的;直到client请求服务时,xinetd才会唤醒相应服务;一旦连接结束后,相应服务会被关闭。所以super-daemon方式不会一直占用系统资源
- 既然有请求才会去启动服务,所以server端的响应速度自然不如stand-alone方式来得快
查询已安装的服务
RPM包安装的服务
[root@AmorLei ~]# chkconfig --list network 0:off 1:off 2:on 3:on 4:on 5:on 6:off ...
查看服务自启动状态,可以看到所有RPM包安装的服务
源码包安装的服务
查看服务安装位置,一般是/usr/local/下
启动与自启动
服务启动:就是在当前系统中让服务运行,并提供功能。
服务自启动:自启动是指让服务在系统开机或重启动之后,随着系统的启动而自动启动服务。
3.服务与端口
端口
如果把IP地址比作一间房子,端口就是出入这间房子的门。真正的房子只有几个门,但是一个IP地址的端口可以有65536个。
端口与服务的对应
/etc/services 文件记录了网络服务名和它们对应使用的端口号及协议。
查询系统中开启的服务
netstat -tlunp -t 列出tcp数据 -u 列出udp数据 -l 列出正在监听的网络服务(不包含已连接的网络服务) -n 用端口号来显示服务,而不是用服务名 -p 列出该服务的进程ID(PID)
会列出系统中所有的已经启动的服务
[root@AmorLei etc]# netstat -tlunp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6054/httpd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1407/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1570/master udp 0 0 172.17.190.178:123 0.0.0.0:* 1418/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 1418/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 1418/ntpd [root@AmorLei etc]# netstat -an
2、RPM包服务管理
1.独立服务的管理
RPM安装服务和源码包安装服务的区别
RPM安装服务和源码包安装服务的区别就是安装位置的不同
- 源码包安装在指定位置,一般是/usr/local/
- RPM包安装在默认位置中
usr是Unix System Resource,即Unix系统资源的缩写。
/etc/init.d/ :启动脚本位置 /etc/sysconfig/ :初始化环境配置文件位置 /etc/ :配置文件位置 /etc/xinetd.conf :xinetd配置文件 /etc/xinetd.d/ :基于xinetd服务的启动脚本 /var/lib/ :服务产生的数据放在这里 /var/log/ :日志
独立服务的启动
/etc/init.d/独立服务名 start | stop | status | restart
[root@AmorLei ~]# /etc/init.d/httpd start 正在启动 httpd: [root@AmorLei ~]# /etc/rc.d/init.d/httpd start [root@AmorLei ~]# ls -l /etc/init.d lrwxrwxrwx. 1 root root 11 Aug 24 12:21 /etc/init.d -> rc.d/init.d
service 独立服务名 start|stop|restart|status
只有RedHat系列的Linux系统可以此命令,源码包不可以使用此命令。
[root@AmorLei ~]# service httpd status httpd (pid 13986) 正在运行...
独立服务的自启动
chkconfig [--level 运行级别] [独立服务名] [on|off]
[root@AmorLei ~]# chkconfig --list | grep httpd httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@AmorLei ~]# chkconfig --level 2345 httpd on [root@AmorLei ~]# chkconfig --list | grep httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
修改的是文件的自启动,与服务是否启动无关。
修改/etc/rc.d/rc.local文件(/etc/rc.local)
开机后将自动执行的命令
[root@AmorLei ~]# vi /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
使用ntsysv命令管理自启动
RedHat专有命令,使用图形界面对服务运行级别进行修改。
2.基于xinetd服务的管理
安装xinetd
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@AmorLei ~]# yum -y install xinetd Loaded plugins: fastestmirror Setting up Install Process Determining fastest mirrors base | 3.7 kB 00:00 epel | 4.7 kB 00:00 epel/primary_db | 6.0 MB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 updates/primary_db | 4.7 MB 00:00 Resolving Dependencies --> Running transaction check ---> Package xinetd.x86_64 2:2.3.14-40.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================= Package Arch Version Repository Size ======================================================================================================================================= Installing: xinetd x86_64 2:2.3.14-40.el6 base 122 k Transaction Summary ======================================================================================================================================= Install 1 Package(s) Total download size: 122 k Installed size: 259 k Downloading Packages: xinetd-2.3.14-40.el6.x86_64.rpm | 122 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : 2:xinetd-2.3.14-40.el6.x86_64 1/1 Verifying : 2:xinetd-2.3.14-40.el6.x86_64 1/1 Installed: xinetd.x86_64 2:2.3.14-40.el6 Complete!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@AmorLei ~]# chkconfig --list aegis 0:off 1:off 2:on 3:on 4:on 5:on 6:off agentwatch 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 blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off cloud-config 0:off 1:off 2:on 3:on 4:on 5:on 6:off cloud-final 0:off 1:off 2:on 3:on 4:on 5:on 6:off cloud-init 0:off 1:off 2:on 3:on 4:on 5:on 6:off cloud-init-local 0:off 1:off 2:on 3:on 4:on 5:on 6:off cloud-init-upgrade 0:off 1:off 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 ecs_mq-service 0:off 1:off 2:on 3:on 4:on 5:on 6:off eni-service 0:off 1:off 2:on 3:on 4:on 5:on 6:off ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 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 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 multipathd 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:on 3:on 4:on 5:on 6:off nscd 0:off 1:off 2:off 3:off 4:off 5:off 6:off ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off ntpdate 0:off 1:off 2:off 3:off 4:off 5:off 6:off postfix 0:off 1:off 2:on 3:on 4:on 5:on 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 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 sshd 0:off 1:off 2:on 3:on 4:on 5:on 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 xinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:off xinetd based services: chargen-dgram: off chargen-stream: off daytime-dgram: off daytime-stream: off discard-dgram: off discard-stream: off echo-dgram: off echo-stream: off tcpmux-server: off time-dgram: off time-stream: off
xinetd服务的启动
[root@AmorLei ~]# vi /etc/xinetd.d/ chargen-dgram daytime-dgram discard-dgram echo-dgram tcpmux-server time-stream chargen-stream daytime-stream discard-stream echo-stream time-dgram [root@AmorLei ~]# vi /etc/xinetd.d/rsync service rsync { flags = REUSE 标志为REUSE,设定TCP/IP socket 可重用 socket_type = stream 使用TCP协议数据包 wait = no 允许多个连接同时连接 user = root 启动服务的用户为root server = /usr/bin/rsync 服务的启动程序 log_on_failure += USERID 登录失败后,记录用户的ID disable = no 服务不启动 }
重启xinetd服务
[root@AmorLei ~]# service xinetd restart
Stopping xinetd: [FAILED]
Starting xinetd: [ OK ]
xinetd服务的自启动
[root@AmorLei ~]# chkconfig rsync on
ntsysv
3、源码包服务管理
1.源码包安装服务的启动
使用绝对路径,调用启动脚本来启动。不同的源码包的启动脚本不同。可以查看源码包的安装说明,查看启动脚本的方法。
/usr/local/appache2/bin/apachectl start|stop
2.源码包服务的自启动
[root@AmorLei ~]# vi /etc/rc.d/rc.local
在文件中添加启动命令/usr/local/appache2/bin/apachectl start
3.让源码包服务被服务管理命令识别
让源码包的apache服务能被service命令管理启动:
ln -s /usr/local/apache2/bin/apachectl /etc/init.d
[root@AmorLei ~]# service apachectl start httpd: apr_sockaddr_info_get() failed for AmorLei httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
让源码包的apache服务能被"chkconfig"与"ntsysv"命令管理自启动
[root@AmorLei ~]# vi /etc/init.d/apachectl #!/bin/sh # # chkconfig: 35 86 76 # description: source package apache # chkconfig:35 86 76 # 指定httpd脚本可以被chkconfig命令管理。格式是:"chkconfig:运行级别 启动顺序 关闭顺序" #description:source package apache # 说明,内容随意
启动顺序、关闭顺序不能与"/etc/rc3.d/"中的顺序号冲突。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@AmorLei ~]# ls /etc/rc3.d/ K10saslauthd K89rdisc S10network S25blk-availability S50ecs_mq-service S56xinetd S98agentwatch K74nscd S01sysstat S11auditd S25netfs S51cloud-init-local S58ntpd S99local K75ntpdate S02lvm2-monitor S12rsyslog S26udev-post S52cloud-init S80aegis K87multipathd S07iscsid S13irqbalance S28eni-service S53cloud-config S80postfix K87restorecond S08ip6tables S13iscsi S50aegis S54cloud-final S90crond K89netconsole S08iptables S15mdmonitor S50cloud-init-upgrade S55sshd S95atd
[root@AmorLei ~]# chkconfig --add apachectl
把源码包apachectl加入chkconfig
命令
4、服务管理总结