Linux服務管理

1、簡介與分類

1.系統的運行級別

運行級別含義
0 關機
1 單用戶模式,可以想象爲Windows的安全模式,主要用於系統修復
2 不完全的命令行模式,不含NFS服務
3 完全的命令行模式,就是標準字符界面
4 系統保留
5 圖形模式
6 重啓動

運行級別命令

[root@AmorLei ~]# runlevel     # 查看運行級別命令
N 3
[root@AmorLei ~]# init 運行級別     # 修改運行級別命令

系統默認運行級別

[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:
[root@AmorLei ~]# vi /etc/inittab

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

[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!
[root@AmorLei ~]# yum -y install xinetd
[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
[root@AmorLei ~]# chkconfig --list

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/"中的順序號衝突。

[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 ~]# ls /etc/rc3.d/
[root@AmorLei ~]# chkconfig --add apachectl

把源碼包apachectl加入chkconfig命令

4、服務管理總結

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章