2011年4月26日 星期二

/etc/inittab 設定

轉貼
/etc/inittab 設定
init process
init 是在核心 mount 進 root 後,第一個執行的程式 (第一個 process),init 所負責的工作包括:
  • 執行 /etc/rc.d/rc?.d/ 裡的 scripts
  • 將系統由開機狀態 (boot-up state) 切換至多使用者狀態 (multiuser state)
  • 負責載入 tty (本地端) 的 login shell
  • 負責開機與關機的程序 (procedure)。
開 機完成後,init 仍會保持在背景執行,並同時監視與修改系統執行狀態。init 監視許多系統的動作,適時改變系統的狀態,這些動作定義在 /etc/inittab 檔案裡。系統狀態劃分成七個等級,稱之為 run level,/etc/inittab 即告訴 init,在不同的 run level 下所要執行的程式與命令,Linux 下的 run level 有共七個,分別為:
  • run level 0 關機
  • run level 1 單一使用者模式
  • run level 2 多使用者模式(沒有NFS)
  • run level 3 多使用者模式
  • run level 4 保留
  • run level 5 X11
  • run level 6 重開機
以 Red Hat Linux 的 /etc/inittab 為例
不正確的 /etc/inittab 設會使得系統不正常,而且無法登入,所以修改 /etc/inittab 前請先做好備分,/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



# Things to run in every runlevel.

ud::once:/sbin/update



# 處理 [ctrl]-[alt]-[del] 的按鍵
ca::ctrlaltdel:/sbin/shutdown -t3 -r now



# 如果我們有 UPS,這一行的設定會在斷電時通知我們,並在 2 分鐘後自動關機。 
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"



# 如果在關機前恢復電力,則放棄關機。
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"





# 用來登入系統 (本地端)。
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6



# gdm 程式。
x:5:respawn:/etc/X11/prefdm -nodaemon
每個欄位所代表的義意如下:
  • 第一個欄位,是 init 內部使用的名稱,用來辨認每一個項目,執行命令 "ps ax" 所看到的名稱。
  • 第二個欄位,表示這個項目的 run level。
  • 第 三個欄位,關鍵字欄位,其中 respawn 是一個重要的關鍵字。以 "5:2345:respawn:/sbin/mingetty tty5" 為例說明,init 會依據設定,在本地端開啟一個終端機,當這個終端機超過時間沒有人 login 或太久沒有按鍵時,這個終端機就會結束執行,respawn 即在告訴 init 再重新執行這個終端機,否則,在一段時間後,我們會發現這個終端機消失了,無法利用 [alt]-[f5] 切換終端機。
  • 第四個欄位:要執行的命令與參數
第一行的 "id:3:initdefault:" 是很重要的一行,用來告訴 init,預設的 run level,在這裡我們設定開機後的預設 run level 為 3,那麼下面設定的命令都會被執行:
l3:3:wait:/etc/rc.d/rc 3

ud::once:/sbin/update
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
第二個如果是空白,則表示在所有的 run level 都會執行,例如:
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
表示在按下 [ctrl]-[alt]-[del] 的事件發生時,在所有 run level 狀態下,都會去執行/sbin/shutdown -t3 -r now 的命令。這一欄也可以指定多個 run level,例如:
6:2345:respawn:/sbin/mingetty tty6
表示在進入 run level 2/3/4/5 時,都會執行 "/sbin/mingetty tty6"。
改變 run level
如果我們將預設 run level 改為 5:
id:3:initdefault:
那麼:
x:5:respawn:/etc/X11/prefdm -nodaemon
這一行便會生效,"/etc/X11/prefdm -nodaemon" 的命令就會被執行,也就是我們熟悉的gdm。
我們可藉由 telinit 指令來改變目前的 run level,例如在 run level 3 狀態下執行:
linux# telinit 5
就會馬上進入 run level 5,執行 gdm。在 gdm 下如果想回到 console 模式,可以按下[ctrl]-[alt]-[f1],後然再執行:
linux# telinit 3
即可。

/etc/fstab 的用途

轉貼
/etc/fstab 的用途
/etc/fstab 是設定分割區與 mount 目錄關係的檔案,開機時會依這個檔案的內容 mount 檔案系統,mount 檔案系統的命令為:
linux# mount -av
會依 /etc/fstab 的內容 mount 進除了 root 以外的所有檔案系統。
root 是在開機時由核心所 mount 進來的。root 先由核心 mount 成 read-only,然後由使用者手動 remount 成 read-write。
/etc/fstab 設定格式
一個簡單的 /etc/fstab 設定範例如下:
#Device mount point filesystem type option
LABEL=/ / ext2 defaults 1 1
/dev/sda5 /home ext2 defaults 1 2
/dev/cdrom /mnt/cdrom iso9660 noauto,owner,ro 0 0
/dev/fd0 /mnt/floppy auto noauto,owner 0 0
LABEL=/usr /usr ext2 defaults 1 2
none /proc proc defaults 0 0
none /dev/pts devpts gid=5,mode=620 0 0
/dev/sda7 swap swap defaults 0 0 
每個欄位所代表的義意為:
  • device 指定分割區
  • mount point 指定要 mount 目錄
  • filesystem type 指定檔案系統,比較常用的檔案系統有:
‧ msdos
‧ vfat
‧ nfs
‧ swap
‧ ext2
使用這些檔案系統前,請先確認核心是否支援,要知道目前核心的狀態支援那些檔案系統,可以查看 /proc/filesystems:
ext2
nodev proc

iso9660
nodev devpts
nodev usbdevfs 
表示目前核心支援 ext2/proc/iso9660/devpts/usbdevfs 的檔案格式,其中proc、devpts 與 usbdevfs 前面有一個 nodev 關鍵字,表示這二個檔案系統其實是存在記憶體中的,並非真正存在於儲存裝置中。
  • option 設定參數,如果我們要 mount 的是硬碟的分割區,不管是什麼檔案系統,只要設定為 defaults 即可。
  • 第一個數字,0 或者空白表示不要 dump,1 表示要 dump,dump 是用來將系統備份到磁帶機的指令,備份時,如果想要略過這個分割區,在一欄填 0 即可,如困2這一欄是 1 的話, dump 時就會備份這個分割區。
  • 第 二個數字,是設定當系統有不正常關機情形時,要如何檢查這些檔案系統。首先 fsck 會依序檢查這一欄為 1 的檔案系統,然後同時檢查這一欄為 2 的檔案系統,0 或者空白的話表示不做檢查。一般而言,只會將 root 設定為 1,其它都設定為 0 或 2。一些特定的檔案系統,像是 swap、iso9660、nfs、proc 都設定為0。
proc 是比較特殊的檔案系統,/proc 其實是存在於記憶體中的,/proc 裡的檔案紀錄了目前系統的狀態,藉由觀察這裡面的檔案我們可以得知許多系統的資訊。
例如,要檢查目前 swap partition 的狀態可以查看 /proc/swaps:
Filename Type Size Used Priority
/dev/sda7 partition 136512 4648 -1 
最後舉個例子,例如為電腦新增了一顆硬碟,然後切出了一個 /dev/sdb8 的 partition,並且想將這個 partition 做來做 ftp 的 home,這時就要在 /etc/fstab 裡加上一行:
/dev/sdb8 /home/ftp ext2 defaults 1 2

Install rsyslog

Step 1 - Download Software

Step 2 - Run ./configure

Step 3 - Compile

Step 4 - Install

Step 5 - Configure rsyslogd

 Rsyslogd understands stock syslogd syntax, so you can simply copy over /etc/syslog.conf to /etc/rsyslog.conf.

Note since version 3 rsyslog requires to load plug-in modules to perform useful work (more about compatibilty notes v3). To load the most common plug-ins, add the following to the top of rsyslog.conf:

......

 

Step 6 - Disable stock syslogd

In almost all cases, there already is stock syslogd installed. Because both it and rsyslogd listen to the same sockets, they can NOT be run concurrently. So you need to disable the stock syslogd. To do this, you typically must change your rc.d startup scripts.

Step 7 - Enable rsyslogd Autostart

This step is very close to step 3. Now, we want to enable rsyslogd to start automatically. The rsyslog package contains a (currently small) number of startup scripts. They are inside the distro-specific directory (e.g. debian). If there is nothing for your operating system, you can simply copy the stock syslogd startup script and make the minor modifications to run rsyslogd (the samples should be of help if you intend to do this).
In our Debian example, the actual scripts are stored in /etc/init.d. Copy the standard script to that location. Then, you need to add a symlink to it in the respective rc.d directory. In our sample, we modify rc2.d, and can do this via the command "ln -s ../init.d/rsyslogd S10rsyslogd". Please note that the S10 prefix tells the system to start rsyslogd at the same time stock sysklogd was started.

 

Step 8 - Check daily cron scripts

Most distributions come pre-configured with some daily scripts for log rotation. As long as you use the same log file names, the log rotation scripts will probably work quite well. There is one caveat, though. The scripts need to tell syslogd that the files have been rotated. To do this, they typically have a part using syslogd's init script to do that. Obviously, the default scripts do not know about rsyslogd, so they manipulate syslogd. If that happens, in most cases an additional instance of stock syslogd is started (in almost all cases, this was not functional, but it is at least distracting). It also means that rsyslogd is not properly told about the log rotation, which will lead it to continue to write to the now-rotated files.
So you need to fix these scripts. See your distro-specific documentation how they are located. Under most Linuxes, the primary script to modify is /etc/cron.daily/sysklogd. Watch for a comment "Restart syslogd" (usually at the very end of the file). The restart command must be changed to use rsyslogd's rc script.
Also, if you use klogd together with rsyslogd (under most Linuxes you will do that), you need to make sure that klogd is restarted after rsyslogd is restarted. So it might be a good idea to put a klogd reload-or-restart command right after the rsyslogd command in your daily script. This can save you lots of troubles.