เรื่องน่ารู้เกี่ยวกับ start/stop dhcp server ทันทีที่เปิด server

พวกเราที่ใช้งาน ubuntu server และที่ติดตั้ง dhcp server เพื่อทำหน้าที่แจกจ่าย IP Address ให้กับเครื่อง client ก็มักจะเลือกใช้ dhcp server ของ ISC (www.isc.org) ซึ่งก็จะติดตั้งได้ง่ายๆผ่านคำสั่ง apt-get install (ผมละคำว่า sudo ที่ต้องใส่นำหน้าคำสั่งเอาไว้ในฐานที่เข้าใจ)

แต่เดิมใน ubuntu 10.04 จำได้ว่าวิธีติดตั้ง dhcp server ก็จะใช้คำสั่ง apt-get install dhcp3-server แล้วต่อมาเมื่อเปลี่ยนเป็น ubuntu 12.04 วิธีการติดตั้งก็เปลี่ยนไปเป็น apt-get install isc-dhcp-server

คราวนี้ก็มาถึงเรื่องที่อยากมาเล่าสู่กันฟัง คือ พวกเราจะรู้ไม๊ครับว่า หากเราต้องการจะ start หรือ stop dhcp server จะทำอย่างไร และโจทย์ของผมคือ ในการทำ dhcp server ทดสอบผมไม่ต้องการให้ dhcp server มันทำงานทันทีที่เปิดเครื่อง จะต้องทำอย่างไร

เราเคยรู้มาว่า อ๋อ ก็ใช้คำสั่ง update-rc.d ถ้าต้องการเปิดใช้งาน ก็สั่ง update-rc.d isc-dhcp-server defaults ถ้าไม่ต้องการให้ทำงานทันทีที่เปิดเครื่อง ก็สั่ง update-rc.d -f isc-dhcp-server remove แบบนี้ เชื่อไม๊ครับว่า หลังจาก reboot เครื่องแล้ว dhcp server มันก็ทำงานอยู่ โดยใช้คำสั่งในการเช็ค คือ

ps ax | grep dhcp

แสดงผลลัพธ์แบบนี้

mama@ubuntu:~$ ps ax | grep dhcp
 2491 ?        Ss     0:00 /usr/sbin/dhcpd -f -q -4 -pf /run/dhcp-server/dhcpd.pid -cf /etc/dhcp/dhcpd.conf eth0

หรือ

service isc-dhcp-server status

แสดงผลแบบนี้

mama@ubuntu:~$ sudo start isc-dhcp-server
isc-dhcp-server start/running, process 2491

เอาหละสิ แล้ว dhcp server มันเปิดขึ้นมาได้อย่างไร ค้นๆดูก็พบว่ามันมีไดเรกทอรี /etc/init/ ที่จะเป็นที่เก็บโปรแกรมสมัยใหม่ที่เรียกกันว่าแบบ upstart แทนการใช้งานแบบ SysVinit (/etc/init.d/) และก็เจอไฟล์ isc-dhcp-server.conf อยู่ใน /etc/init/ เมื่อแก้ไขที่ไฟล์นี้ก็สำเร็จ ใส่ comment “#” เพื่อว่าจะไม่ start เมื่อเปิดเครื่อง (บรรทัดที่4 #start on runlevel [2345])

ก็ลองค้นดูเรื่องราวใน google ก็พบบทความหนึ่ง เรื่อง “What is the difference between /etc/init/ and /etc/init.d/?”

ก็ได้คำตอบว่า ปัจจุบันนี้ผู้พัฒนาโปรแกรมบน ubuntu สามารถเลือกใช้ init ได้ 2 แบบ SysVinit กับ Upstart

แบบ SysVinit เวลาสั่ง start จะทำดังนี้ /etc/init.d/dhcp3server start
แบบ Upstart เวลาสั่ง start จะทำดังนี้ start isc-dhcp-server

แบบ SysVinit ตั้งค่าโปรแกรมทำงานเมื่อเปิดเครื่อง แก้ไขด้วย update-rd.d
แบบ Upstart ตั้งค่าโปรแกรมทำงานเมื่อเปิดเครื่อง แก้ไขที่ไฟล์ config ใน /etc/init/

ขออนุญาต ลอกข้อความจาก Link http://askubuntu.com/questions/5039/what-is-the-difference-between-etc-init-and-etc-init-d มาให้อ่าน

“/etc/init.d contains scripts used by the System V init tools (SysVinit). This is the traditional service management package for Linux, containing the init program (the first process that is run when the kernel has finished initializing¹) as well as some infrastructure to start and stop services and configure them. Specifically, files in /etc/init.d are shell scripts that respond to start, stop, restart, and (when supported) reload commands to manage a particular service. These scripts can be invoked directly or (most commonly) via some other trigger (typically the presence of a symbolic link in /etc/rc?.d/).

/etc/init contains configuration files used by Upstart. Upstart is a young service management package championed by Ubuntu. Files in /etc/init are configuration files telling Upstart how and when to start, stop, reload the configuration, or query the status of a service. As of lucid, Ubuntu is transitioning from SysVinit to Upstart, which explains why many services come with SysVinit scripts even though Upstart configuration files are preferred. In fact, the SysVinit scripts are processed by a compatibility layer in Upstart.”

ผมคิดว่าน่าจะมาเล่าสู่กันฟัง เท่าที่มีความรู้ หากมีคำแนะนำเพิ่มเติม บอกได้นะครับ