在Ubuntu16.04下,使用sysv-rc-conf管理服务启动项

Linux系统主要启动步骤

  1. 读取 MBR 信息,启动 Boot Manager。Linux 通常使用功能强大、配置灵活的GRUB作为Boot Manager。

  2. 加载系统内核,启动init进程。init进程是Linux的根进程,所有的系统进程都是它的子进程。

  3. init进程读取/etc/inittab文件中的信息,并进入预设的运行级别,按顺序运行该运行级别对应文件夹下的脚本。脚本通常以 start参数启动,并指向系统中的一个程序。通常情况下,/etc/rcS.d/目录下的启动脚本首先被执行,然后是/etc/rcN.d/目录。例如您设定的运行级别为3,那么它对应的启动目录为/etc/rc3.d/。

  4. 根据/etc/rcS.d/文件夹中对应的脚本启动Xwindow服务器xorg。Xwindow为Linux下的图形用户界面系统。

  5. 启动登录管理器,等待用户登录Ubuntu系统。默认使用GDM作为登录管理器,您在登录管理器界面中输入用户名和密码后,便可以登录系统。(您可以在/etc/rc3.d/文件夹中找到一个名为S13gdm 的链接)


Ubuntu运行级别

在任何时候,Linux系统都跑在一个指定的运行级别上,并且在不同运行级别的程序和服务,所要完成的工作也不同。Linux系统可以在这些运行级别之间进行切换,以完成不同的工作。

运行级别(Runlevel)是指Unix或者Linux等操作系统不同的运行模式,通常分为7等,从0到6。但如果必要的话也可以更多,例如,大多数linux操作系统,共有如下6个典型的运行级别:

  • 0,停机

  • 1,单用户,无网络连接,不能启动守护进程,不允许非root用户登录(Does not configure network interfaces, start daemons, or allow non-root logins)

  • 2,多用户,无网络连接,不能启动守护进程(Does not configure network interfaces or start daemons)

  • 3,多用户,启动网络连接(Starts the system normally.)

  • 4,用户自定义

  • 5,多用户,带图形化界面

  • 6,重启

查看当前系统的运行级别

# runlevel

切换运行级别

# init [0123456Ss]

即在init命令后跟一个参数,此参数是要切换到的运行级的运行级代号,例如,用init 0命令关机,用init 6命令重新启动。


/etc/rc*目录

对于这些运行级别我们不必太过纠结到底是什么意思,只需要知道在Debian/Ubuntu(Ubuntu是Debian的分支)Linux默认都是运行在Runlevel2级别。

在Debian Linux中,/etc/rc*路径下对应不同的运行级别目录。当系统启动时,通过其中的脚本文件来启动相应的服务。

/etc/rc0.d Run level 0 
/etc/rc1.d Run level 1 
/etc/rc2.d Run level 2 
/etc/rc3.d Run level 3 
/etc/rc4.d Run level 4 
/etc/rc5.d Run level 5 
/etc/rc6.d Run level 6 

下面,我们去/etc/目录下寻找这些目录,在终端中输入ls /etc/rc*。对于输出,以K开头的文件,系统在相关运行级别将终止对应的服务;对于以S开头的文件,系统在相关运行级别将启动对应的服务。

/etc/rc.local

/etc/rc0.d:
K01watchdog      K02lightdm        K02n2n                  K02urandom         K05rsyslog       K10cryptdisks
K02atd           K02lvm2-lvmetad   K02open-iscsi           K02uuidd           K06hwclock.sh    K11cryptdisks-early
K02avahi-daemon  K02lvm2-lvmpolld  K02open-vm-tools        K03cgroupfs-mount  K06umountnfs.sh  K12umountroot
K02bluetooth     K02lxcfs          K02plymouth             K03iscsid          K07rpcbind       K13mdadm-waitidle
K02docker        K02lxd            K02resolvconf           K03openibd         K08networking    K14halt
K02irqbalance    K02mdadm          K02unattended-upgrades  K04sendsigs        K09umountfs      README

/etc/rc1.d:
K01watchdog      K02docker        K02lvm2-lvmpolld  K02open-iscsi     K03cgroupfs-mount  K07rpcbind    S02single
K02atd           K02irqbalance    K02lxcfs          K02open-vm-tools  K03iscsid          README
K02avahi-daemon  K02lightdm       K02lxd            K02ufw            K03openibd         S01killprocs
K02bluetooth     K02lvm2-lvmetad  K02mdadm          K02uuidd          K05rsyslog         S01n2n

/etc/rc2.d:
README            S01openibd              S02acpid           S02mdadm         S03lightdm      S04watchdog
S01apport         S01open-vm-tools        S02atd             S02rsync         S03lxcfs
S01lvm2-lvmetad   S01rsyslog              S02cgroupfs-mount  S02ssh           S04grub-common
S01lvm2-lvmpolld  S01unattended-upgrades  S02cron            S03avahi-daemon  S04ondemand
S01lxd            S01uuidd                S02dbus            S03bluetooth     S04plymouth
S01n2n            S01wd_keepalive         S02irqbalance      S03docker        S04rc.local

/etc/rc3.d:
README            S01openibd              S02acpid           S02mdadm         S03lightdm      S04watchdog
S01apport         S01open-vm-tools        S02atd             S02rsync         S03lxcfs
S01lvm2-lvmetad   S01rsyslog              S02cgroupfs-mount  S02ssh           S04grub-common
S01lvm2-lvmpolld  S01unattended-upgrades  S02cron            S03avahi-daemon  S04ondemand
S01lxd            S01uuidd                S02dbus            S03bluetooth     S04plymouth
S01n2n            S01wd_keepalive         S02irqbalance      S03docker        S04rc.local

/etc/rc4.d:
README            S01openibd              S02acpid           S02mdadm         S03lightdm      S04watchdog
S01apport         S01open-vm-tools        S02atd             S02rsync         S03lxcfs
S01lvm2-lvmetad   S01rsyslog              S02cgroupfs-mount  S02ssh           S04grub-common
S01lvm2-lvmpolld  S01unattended-upgrades  S02cron            S03avahi-daemon  S04ondemand
S01lxd            S01uuidd                S02dbus            S03bluetooth     S04plymouth
S01n2n            S01wd_keepalive         S02irqbalance      S03docker        S04rc.local

/etc/rc5.d:
README            S01openibd              S02acpid           S02mdadm         S03lightdm      S04watchdog
S01apport         S01open-vm-tools        S02atd             S02rsync         S03lxcfs
S01lvm2-lvmetad   S01rsyslog              S02cgroupfs-mount  S02ssh           S04grub-common
S01lvm2-lvmpolld  S01unattended-upgrades  S02cron            S03avahi-daemon  S04ondemand
S01lxd            S01uuidd                S02dbus            S03bluetooth     S04plymouth
S01n2n            S01wd_keepalive         S02irqbalance      S03docker        S04rc.local

/etc/rc6.d:
K01watchdog      K02lightdm        K02n2n                  K02urandom         K05rsyslog       K10cryptdisks
K02atd           K02lvm2-lvmetad   K02open-iscsi           K02uuidd           K06hwclock.sh    K11cryptdisks-early
K02avahi-daemon  K02lvm2-lvmpolld  K02open-vm-tools        K03cgroupfs-mount  K06umountnfs.sh  K12umountroot
K02bluetooth     K02lxcfs          K02plymouth             K03iscsid          K07rpcbind       K13mdadm-waitidle
K02docker        K02lxd            K02resolvconf           K03openibd         K08networking    K14reboot
K02irqbalance    K02mdadm          K02unattended-upgrades  K04sendsigs        K09umountfs      README

/etc/rcS.d:
README             S02resolvconf        S05procps                  S09urandom                S13rpcbind
S01console-setup   S02screen-cleanup    S06hwclock.sh              S10cryptdisks             S14mountnfs.sh
S02apparmor        S02ufw               S07checkroot.sh            S11checkfs.sh             S14open-iscsi
S02hostname.sh     S02x11-common        S08cryptdisks-early        S12mountall.sh            S15mountnfs-bootclean.sh
S02mountkernfs.sh  S03udev              S09checkroot-bootclean.sh  S12networking             S16bootmisc.sh
S02plymouth-log    S04keyboard-setup    S09kmod                    S13iscsid
S02pppd-dns        S05mountdevsubfs.sh  S09lvm2                    S13mountall-bootclean.sh

现在对于Debian/Ubuntu Linux的启动过程大家应该有所了解,总结一下:在/etc/rc0.drc6.d目录下,分别存放的是在06运行级别时要启动的相关服务文件,由于Ubuntu默认是在runlevel2启动,那么我们只需要修改rc2.d中的文件,从而禁止某些服务启动,例如n2n。

回顾刚才/etc/rc2.d目录下的文件,我们可以发现与n2n有关的文件是S01n2n。我们查看n2n的文件属性,系统将开机自启动以S开头文件对应的服务。

# file S01n2n

S01n2n: symbolic link to ../init.d/n2n

查找n2n在其他运行级别下的状态

# 查看当前系统的运行级别
# runlevel

N 5

# find / -name S01n2n

/etc/rc1.d/S01n2n
/etc/rc2.d/S01n2n
/etc/rc3.d/S01n2n
/etc/rc4.d/S01n2n
/etc/rc5.d/S01n2n

我们会发现n2n在运行级别1,2,3,4,5之下都是开机启动的,所以我们要做的是把S91n2n修改成以K******,但是我们并不知道应该修改成多少,这里需要借助一个管理工具进行管理,即sysv-rc-conf。


sysv-rc-conf

sysv-rc-conf是一个强大的服务管理程序,群众的意见是sysv-rc-conf比chkconfig、rcconf好用。在Ubuntu下,用apt-get安装sysv-rc-conf

# sudo apt-get install sysv-rc-conf

运行sysv-rc-conf,查看在各个系统运行级别的服务启动项是否开启

# sudo sysv-rc-conf

在这里插入图片描述

第一行service后面是运行级别,有1,2,3,4,5,0,6,S,与之前介绍的运行级别基本符合,最后S项这里不做介绍。

图中打X的即表示运行在对应Runlevel时服务是否开机启动,这里我们可以看到n2n在Runlevel1,2(系统默认运行级别),3,4,5(当前系统运行级别)均为开机启动,对应之前/etc/rc1.d ~ /etc/rc5.d目录下均为S01n2n文件,再次验证了之前的内容。

将光标移动到n2n那行的第2列,按空格进行将启动项反选,按q退出之后配置即完成。现在我们再来看一下/etc/rc2.d/目录下的内容

ls /etc/rc*

在对应运行级别的文件夹下,已经出现了K…n2n文件,而之前的S01n2n文件不在了。到这里配置成功,重启系统之后,查看n2n服务运行状态

service n2n status 

注:同理,如果想要某个服务在某个运行级别下开机自启,就选中对应的X。


常见的系统服务信息

  • acpi-support,高级电源管理支持

  • acpid acpi,守护程序。这两个用于电源管理,非常重要

  • alsa,声音子系统

  • alsa-utils,anacroncron的子系统,将系统关闭期间的计划任务,在下一次系统运行时执行

  • apmd,acpi的扩展

  • atd,类似于cron的任务调度系统。建议关闭

  • binfmt-support,核心支持其他二进制的文件格式,建议开启

  • bluez-utiles,蓝牙设备支持

  • bootlogd,启动日志。开启它

  • cron,任务调度系统,建议开启

  • cupsys,打印机子系统

  • dbus,消息总线系统(message bus system)。非常重要

  • dns-clean,使用拨号连接时,清除dns信息

  • evms,企业卷管理系统(Enterprise Volumn Management system)

  • fetchmail,邮件用户代理守护进程,用于收取邮件

  • gdm,gnome登录和桌面管理器

  • gdomap,gpm终端中的鼠标支持

  • halt,别动它

  • hdparm,调整硬盘的脚本,配置文件为/etc/hdparm.conf

  • hibernate,系统休眠

  • hotkey-setup,笔记本功能键支持。支持类型包括:HP, Acer, ASUS, Sony, DellIBM

  • hotplug and hotplug-net,即插即用支持,比较复杂,建议不要动它

  • hplip,HP打印机和图形子系统

  • ifrename,网络接口重命名脚本。如果您有10块网卡,您应该开启它

  • inetd,在文件/etc/inetd.conf中,注释掉所有你不需要的服务。如果该文件不包含任何服务,那关闭它是很安全的

  • klogd,重要

  • linux-restricted-modules-common,受限模块支持。/lib/linux-restricted-modules文件夹中的模块为受限模块。例如某些驱动程序,如果您没有使用受限模块,就不需要开启它

  • lvm,逻辑卷管理系统支持

  • makedev,创建设备文件,非常重要

  • mdamd,磁盘阵列

  • module-init-tools,从/etc/modules加载扩展模块,建议开启

  • networking,网络支持。按/etc/network/interfaces文件预设激活网络,非常重要

  • ntpdate,时间同步服务,建议关闭

  • pcmcia,pcmcia设备支持

  • powernowd,移动CPU节能支持

  • ppp and ppp-dns,拨号连接readahead预加载库文件

  • reboot,别动它

  • resolvconf,自动配置DNS

  • rmnologin,清除nologin

  • rsync,rsync守护程序

  • sendsigs,在重启和关机期间发送信号

  • single,激活单用户模式

  • ssh,ssh守护程序。建议开启

  • stop-bootlogd,在2,3,4,5运行级别中停止bootlogd服务

  • sudo,检查sudo状态。重要

  • sysklogd,系统日志

  • udev & udev-mab,用户空间dev文件系统(userspace dev filesystem)。重要

  • umountfs,卸载文件系统

  • urandom,随机数生成器

  • usplash,开机画面支持

  • vbesave,显卡BIOS配置工具。保存显卡的状态

  • xorg-common,设置X服务ICE socket

  • adjtimex,调整核心时钟的工具

  • dirmngr,证书列表管理工具和gnupg一起工作

  • hwtools,irqs优化工具

  • libpam-devperm,系统崩溃之后,用于修理设备文件许可的守护程序

  • lm-sensors,板载传感器支持

  • mdadm-raid,磁盘陈列管理器

  • screen-cleanup,清除开机屏幕的脚本

  • xinetd,管理其他守护进程的一个inetd超级守护程序


参考文章

https://blog.csdn.net/gatieme/article/details/45251389

猜你喜欢

转载自blog.csdn.net/yjk13703623757/article/details/88777354