โปรแกรม init, Runlevel และ Services
ในช่วงของการ boot ระบบนั้น หลังจากที่เคอร์เนลรันโปรแกรม init init ก็จะมีการรันคำสั่งต่างๆ และอ่านค่าที่กำหนดไว้ในไฟล์ /etc/inittab เพื่อกำหนด Runlevel และค่าอื่นๆ ของระบบ
สำหรับทั้ง Redhat/Fedora และ Debian มีไฟล์ที่ใช้กำหนด runlevel เหมือนกันคือ /etc/inittab แต่มีการกำหนดสถานะไว้ต่างกัน
# บางส่วนของ Fedora /etc/inittab
#
# inittab This file describes how the INIT process should set up
# the system in a certain run-level.
#
# Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
# Modified for RHS Linux by Marc Ewing and Donnie Barnes
#
# Default runlevel. The runlevels used by RHS 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:
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
นั่นคือใน Redhat นั้นจะมีการกำหนดไว้เลยว่า runlevel 3 นั้นจะเป็น muti-user ในแบบเท็กท์โหมด และ runlevel 5 นั้นจะเป็น multi-user ในแบบกราฟิกโหมด ดังนั้นถ้าเราต้องการรันในโหมดไหนก็แค่กำหนดหมายเลข runlevel ในไฟล์ /etc/inittab บรรทัดที่มีคำว่า initdefault
id:3:initdefault:
# ในที่นี้คือ runlevel 3 ถ้าต้องการรันในกราฟฟิกโหมดก็แค่เปลี่ยนจาก 3 เป็น 5 (แต่ก็แน่นอนว่าจะต้องลงส่วนประกอบทางด้านกราฟฟิกเอาไว้ด้วย)
# บางส่วนจาก Debian /etc/inittab
# /etc/inittab: init(8) configuration.
# $Id: inittab,v 1.91 2002/01/25 13:35:21 miquels Exp $
# The default runlevel.
id:2:initdefault:
# Boot-time system configuration/initialization script.
# This is run first except when booting in emergency (-b) mode.
si::sysinit:/etc/init.d/rcS
# What to do in single-user mode.
~~:S:wait:/sbin/sulogin
# /etc/init.d executes the S and K scripts upon change
# of runlevel.
#
# Runlevel 0 is halt.
# Runlevel 1 is single-user.
# Runlevels 2-5 are multi-user.
# Runlevel 6 is reboot.
l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6
# Normally not reached, but fallthrough in case of emergency.
z6:6:respawn:/sbin/sulogin
จากไฟล์ /etc/inittab ของ Debian บรรทัดที่ว่า
# Boot-time system configuration/initialization script.
# This is run first except when booting in emergency (-b) mode.
si::sysinit:/etc/init.d/rcS
จะเห็นได้ว่า โดยปกติแล้วสำหรับ Debian นอกจากระบบจะมีการรันคำสั่งใน /etc/rcx.d ตามแต่ละ Runlevel เหมือนกับ Redhat แล้ว ระบบจะมีการรันคำสั่งใน /etc/init.d/rcS ทุกครั้งที่ระบบ boot ด้วย
Debian นั้นกำหนดว่า Runlevel 2-5 เป็น multi-user mode (Default คือ Runlevel 2) แต่ไม่ได้กำหนดว่า Runlevel ไหนเป็นโหมดกราฟฟิก
ไฟล์เชลล์สคริปต์จริงๆ ที่ใช้รันเซอร์วิสต่างๆ ของทั้งสองค่ายนั้นจะอยู่ทีไดเรกตอรี /etc/init.d แต่ในแต่ละ Runlevel นั้นจะมีไดเรกตอรี /etc/rcx.d (ใน Redhat จริงๆ แล้วมันลิงค์ไปยัง /etc/rc.d/rcx.d อีกชั้น) โดย x คือหมายเลขของแต่ละ Runlevel ซึ่งเก็บไฟล์ที่ symbolic linkไปยังสคริปต์ที่ต้องการ start หรือ stop ใน /etc/init.d
# Fedora
[root@myfedora ~]# ls -ld /etc/rc*
lrwxrwxrwx 1 root root 7 Mar 22 10:55 /etc/rc -> rc.d/rc
lrwxrwxrwx 1 root root 10 Mar 22 10:55 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx 1 root root 10 Mar 22 10:55 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx 1 root root 10 Mar 22 10:55 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx 1 root root 10 Mar 22 10:55 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx 1 root root 10 Mar 22 10:55 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx 1 root root 10 Mar 22 10:55 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx 1 root root 10 Mar 22 10:55 /etc/rc6.d -> rc.d/rc6.d
drwxr-xr-x 10 root root 4096 Mar 22 10:55 /etc/rc.d
lrwxrwxrwx 1 root root 13 Mar 22 10:55 /etc/rc.local -> rc.d/rc.local
lrwxrwxrwx 1 root root 15 Mar 22 10:55 /etc/rc.sysinit -> rc.d/rc.sysinit
[root@myfedora ~]# ls -l /etc/rc3.d/
…
lrwxrwxrwx 1 root root 19 Apr 8 23:01 K02haldaemon -> ../init.d/haldaemon
lrwxrwxrwx 1 root root 14 Apr 8 23:01 K10cups -> ../init.d/cups
lrwxrwxrwx 1 root root 19 Apr 8 23:01 K12dc_client -> ../init.d/dc_client
lrwxrwxrwx 1 root root 15 Apr 8 23:01 K15httpd -> ../init.d/httpd
lrwxrwxrwx 1 root root 18 Apr 8 23:01 K15lighttpd -> ../init.d/lighttpd
lrwxrwxrwx 1 root root 15 Apr 8 23:01 K25squid -> ../init.d/squid
lrwxrwxrwx 1 root root 17 Apr 8 23:01 K30postfix -> ../init.d/postfix
lrwxrwxrwx 1 root root 18 Apr 8 23:01 K34dhcrelay -> ../init.d/dhcrelay
lrwxrwxrwx 1 root root 17 Apr 8 23:01 K35dovecot -> ../init.d/dovecot
lrwxrwxrwx 1 root root 19 Apr 8 23:01 S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx 1 root root 14 Apr 8 23:01 S55sshd -> ../init.d/sshd
lrwxrwxrwx 1 root root 15 Apr 8 23:01 S90crond -> ../init.d/crond
lrwxrwxrwx 1 root root 17 Apr 8 23:01 S95anacron -> ../init.d/anacron
lrwxrwxrwx 1 root root 13 Apr 8 23:01 S95atd -> ../init.d/atd
lrwxrwxrwx 1 root root 11 Mar 22 10:55 S99local -> ../rc.local
…
# Debian
mydebian:~# ls -ld /etc/rc*
-rwxr-xr-x 1 root root 306 Apr 12 01:29 /etc/rc.local
drwxr-xr-x 2 root root 4096 Apr 12 03:12 /etc/rc0.d
drwxr-xr-x 2 root root 4096 Apr 12 03:12 /etc/rc1.d
drwxr-xr-x 2 root root 4096 Apr 12 03:34 /etc/rc2.d
drwxr-xr-x 2 root root 4096 Apr 12 03:26 /etc/rc3.d
drwxr-xr-x 2 root root 4096 Apr 12 03:12 /etc/rc4.d
drwxr-xr-x 2 root root 4096 Apr 12 03:12 /etc/rc5.d
drwxr-xr-x 2 root root 4096 Apr 12 03:12 /etc/rc6.d
drwxr-xr-x 2 root root 4096 Apr 12 01:40 /etc/rcS.d
mydebian:~# ls -l /etc/rc2.d
-rw-r–r– 1 root root 556 Jan 31 05:27 README
lrwxrwxrwx 1 root root 13 Apr 12 02:07 K79gdm -> ../init.d/gdm
lrwxrwxrwx 1 root root 20 Apr 12 03:31 K79nfs-common -> ../init.d/nfs-common
lrwxrwxrwx 1 root root 18 Apr 12 01:29 S10sysklogd -> ../init.d/sysklogd
lrwxrwxrwx 1 root root 15 Apr 12 01:29 S11klogd -> ../init.d/klogd
lrwxrwxrwx 1 root root 17 Apr 12 01:39 S18portmap -> ../init.d/portmap
lrwxrwxrwx 1 root root 15 Apr 12 02:09 S19hplip -> ../init.d/hplip
lrwxrwxrwx 1 root root 15 Apr 12 01:30 S20acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 20 Apr 12 01:40 S20bittorrent -> ../init.d/bittorrent
lrwxrwxrwx 1 root root 16 Apr 12 03:34 S20cupsys -> ../init.d/cupsys
lrwxrwxrwx 1 root root 14 Apr 12 01:40 S20dbus -> ../init.d/dbus
lrwxrwxrwx 1 root root 15 Apr 12 01:39 S20exim4 -> ../init.d/exim4
lrwxrwxrwx 1 root root 22 Apr 12 01:40 S20hotkey-setup -> ../init.d/hotkey-setup
lrwxrwxrwx 1 root root 17 Apr 12 01:29 S20makedev -> ../init.d/makedev
lrwxrwxrwx 1 root root 23 Apr 12 01:29 S20openbsd-inetd -> ../init.d/openbsd-inetd
lrwxrwxrwx 1 root root 17 Apr 12 01:40 S89anacron -> ../init.d/anacron
lrwxrwxrwx 1 root root 13 Apr 12 01:39 S89atd -> ../init.d/atd
lrwxrwxrwx 1 root root 14 Apr 12 01:29 S89cron -> ../init.d/cron
lrwxrwxrwx 1 root root 18 Apr 12 01:29 S99rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 19 Apr 12 01:29 S99rmnologin -> ../init.d/rmnologin
lrwxrwxrwx 1 root root 23 Apr 12 01:29 S99stop-bootlogd -> ../init.d/stop-bootlogd
ลิงค์ที่ขึ้นต้นด้วย K คือเซอร์วิสที่จะ stop เมื่อเข้าสู่ Runlevel นั้นๆ
ลิงค์ที่ขึ้นต้นด้วย S คือเซอร์วิสที่จะ start เมื่อเข้าสู่ Runlevel นั้นๆ
ใน Redhat นั้นการจะกำหนดให้เซอร์วิสทำงานในแต่ละ Runlevel นั้นจะใช้คำสั่ง ntsysv หรือ chkconfig
ใน Debian นั้น จะมีคำสั่ง update-rc.d (รู้สึกว่าจะเหมาะกับ service maintainer มากกว่า ไม่ค่อยเหมาะกับมือใหม่ซักเท่าไร) และยังสามารถติดตั้งเพิ่มอีกได้ เช่น sysv-rc-conf ซึ่งจะเป็นโปรแกรมที่รันได้ทั้งแบบคอมมานด์ไลน์ (ใช้งานคล้ายกับ chkconfig) และแบบเมนูในคอนโซล (ใช้งานคล้ายกับ ntsysv) ซึ่งใช้งานง่ายกว่า หรือไม่เช่นนั้นก็ต้องแก้ไขเอง โดยใน Debian 4.0ได้มีคำแนะนำไว้ในไฟล์ /etc/rc2.d/README ไว้ด้วยว่า ถ้าต้องการปิดเซอร์วิสใด ก็ให้เปลี่ยนชื่อไฟล์ จาก S นำหน้าเป็น K นำหน้า ตามด้วยตัวเลขที่เกิดจากการลบตัวเลขเดิมออกจาก 100 เช่น ถ้าต้องการปิดเซอร์วิสทางด้านกราฟฟิก (gdm) ใน Runlevel 2 ใช้คำสั่ง
mv /etc/rc2.d/S21gdm /etc/rc2.d/K79gdm
แต่ถ้าต้องการให้กลับมาทำงานเหมือนเดิมก็เปลี่ยนกลับเป็นเหมือนเดิม
การใช้งาน sysv-rc-conf
1. ก่อนอื่นก็ต้องติดตั้งเพิ่มเติมโดยใช้คำสั่ง
apt-get install sysv-rc-conf
ถ้าไม่สามารถติดตั้งได้ เช่นในกรณีที่ติดตั้งจากดีวีดี ระบบจะเรียกหาแผ่นตอนติดตั้ง แต่ถ้าเราต้องการติดตั้งผ่านอินเตอร์เน็ต ก็ต้องเข้าไปแก้ไขไฟล์ /etc/apt/sources.list
mydebian:~# nano /etc/apt/sources.list
#
# deb cdrom:[Debian GNU/Linux 4.0 r0 _Etch_ - Official i386 DVD Binary-1 20070407-11:40]/ etch contrib main
# Comment บรรทัดด้านล่าง เพื่อยกเลิกการใช้แผ่นดีวีดีในการติดตั้งโปรแกรมเพิ่มเติม
#deb cdrom:[Debian GNU/Linux 4.0 r0 _Etch_ - Official i386 DVD Binary-1 20070407-11:40]/ etch contrib main
# เพิ่ม mirror ที่ต้องการเข้าไปในบรรทัดนี้
deb http://ftp.us.debian.org/debian/ etch main contrib
# Uncomment 2 บรรทัด ด้านล่าง ถ้าเกิดมี comment อยู่
deb http://security.debian.org/ etch/updates main contrib
deb-src http://security.debian.org/ etch/updates main contrib.
เสร็จแล้วรันคำสั่ง
apt-get update
apt-get install sysv-rc-conf
2. ถ้าต้องการใช้งานคล้ายๆ กับ ntsysv ของ Redhat เรียกใช้คำสั่ง
sysv-rc-conf

#See Also
man init
man runlevel
man update-rc.d
man sysv-rc-conf
# apt-get install rcconf
ใช้งานเหมือน ntsysv บน Red Hat
ความเห็น โดย โสทร — เมษายน 11, 2007 @ 10:59 pm
@ P’ โสทร
ขอบคุณครับ
ความเห็น โดย suchart — เมษายน 12, 2007 @ 10:59 am