Linux的配置與優化

tolywang發表於2005-01-19
                Linux作為一個免費的類似UNIX的作業系統,正日益受到人們的重視。本人作為一名Linux愛好者,出於學習,比較了各種不同的Linux發行套件,安裝過各種Linux的發行套件,但是每一次安裝完成,大量的配置與優化工作花費筆者許多時間,並且非常容易遺漏一些細節。本文以安裝與使用RedHat 6.1為例,參考了/usr/doc/HOWTO/Config-HOWTO文件,結合自己的工作經驗,談一些自己的做法。以下的做法基本上適合各種liunx發行套件。


一、關於硬碟分割槽
關於安裝Linux如何建立硬碟分割槽、交換分割槽等問題,已經超出本文範圍,但是我的經驗是安裝Linux時一定要建立單獨的分割槽,用於儲存使用者的私人資料,同時可以用於備份系統的配置檔案(如像域名伺服器,Samba的配置檔案等),便於以後配置。

二、編輯/etc/inittab檔案
大部分Linux的發行套件都建立六個虛擬控制檯,實際上三個已經足夠,並且可以節省可貴的記憶體空間。編輯/etc/inittab檔案,在下面三行的前面加上#。 
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
然後執行init q,使系統重新讀取/etc/inittab檔案,關閉tty4、tty5、tty6虛擬控制檯。如果不考慮系統的安全,你可以利用第四個虛擬控制檯來檢視各種系統資訊,編輯/etc/syslog.conf檔案,在最後一行加入:
*.* /dev/tty4
最後再執行killall -HUP syslog。

三、建立或者編輯/etc/inputrc、/etc/profile、/etc/bashrc檔案
雖然最近幾年個人計算機的潮流已經從命令列方式轉向圖形方式,採用滑鼠操作,但是shell在Linux中依然有很強的生命力。shell有好幾種,主要有bash、ksh、tcsh、zsh、ash, 用得最多的是bash。只要編輯/etc/inputrc檔案,設定INPUTRC的環境變數,利用Tab鍵的命令補全功能,就可以實現類似於MSDOS的DOSKEY的功能。這樣對檔案與目錄的各種操作,也一樣方便與快捷。(附/etc/inputrc檔案)
__________________________________________________________________
set bell-style none # don't beep
set meta-flag on # allow 8-bit input 
set convert-meta off # don't strip 8-bit characters
set output-meta on # display 8-bit characters correctly
set horizontal-scroll-mode On # scroll long command lines
set show-all-if-ambiguous On # after TAB is pressed 

"e[1~": beginning-of-line # home
"e[2~": insert-last-argument # insert
"e[3~": delete-char # delete
"e[4~": end-of-line # end
"e[5~": backward-kill-word # page up
"e[6~": kill-word # page down

# define F7 F8 like msdos doskey
"e[18~": history-search-forward # F7
"e[19~": history-search-backward # F8
__________________________________________________________________

編輯/etc/profile檔案,在其尾部插入下面內容:
_________________________________________________
# add by zws
# customize less
#LESS='-M-Q-r'
PS1="[u@h w]$ "
INPUTRC=/etc/inputrc
LESS='-M-r' # -r 為了ls | less 支援彩色。
LESSEDIT="%E ?lt+%lt. %f"
LESSOPEN="| lesspipe.sh %s"
LESSCHARSET=latin1
PAGER=less
export LESS LESSEDIT LESSOPEN LESSCHARSET INPUTRC
_________________________________________________

建立/usr/bin/lesspipe.sh檔案,內容如下:
__________________________________________________________________________
#!/bin/sh
# This is a preprocessor for 'less'. It is used when this environment
# variable is set: LESSOPEN="|lesspipe.sh %s"
lesspipe() {
case "$1" in
*.tar.bz2) bzip2 -cd $1 $1 2>/dev/null | tar tvvf - ;;
*.tar) tar tf $1 2>/dev/null ;; # View contents of .tar and .tgz files
*.tgz|*.tar.gz|*.tar.Z|*.tar.z) tar ztf $1 2>/dev/null ;;
*.Z|*.z|*.gz) gzip -dc $1 2>/dev/null ;; # View compressed files correctly
*.zip) unzip -l $1 2>/dev/null ;; # View archives
*.arj) unarj l $1 2>/dev/null ;;
*.rpm) rpm -qpil $1 2>/dev/null ;;
*.cpio) cpio --list -F $1 2>/dev/null ;;
*.bz2) bzip2 -dc $1 2>/dev/null ;; # View compressed files correctly
*.1|*.2|*.3|*.4|*.5|*.6|*.7|*.8|*.9|*.n|*.l|*.man) FILE=`file -L $1`
FILE=`echo $FILE | cut -d ' ' -f 2`
if [ "$FILE" = "troff" ]; then
groff -s -p -t -e -Tascii -mandoc $1
fi ;;
*) file $1 | grep text > /dev/null ;
if [ $? = 1 ] ; then # it's not some kind of text
strings $1
fi ;;
esac
}
lesspipe $1
_________________________________________________________________這樣就可以使用less檢視以上檔案格式的資訊與內容。
注意要利用chmod 755 /usr/bin/lesspipe.sh命令將此檔案設定為可執行!
/etc/bashrc檔案的例子:
____________________________________________
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# For some unknown reason bash refuses to inherit
# PS1 in some circumstances that I can't figure out.
# Putting PS1 here ensures that it gets loaded every time.
# PS1="[u@h w]$ "

eval `dircolors -b`
alias ls='ls --color=yes -F -N' # ls 支援彩色
alias l='ls --color=yes -l -F -N' # l 
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
alias df='df -h'
alias ln='ln -i'
lrpm() # list information on an .rpm file
{
if [ $# != 0 ]; then rpm -qilf `which $1` | less; fi
}
lsrpm() # list information on an .rpm file
{
if [ $# != 0 ]; then rpm -qif `which $1` | less; fi
}
set -o noclobber # 避免覆蓋與重寫檔案
____________________________________________

四、編輯/etc/rc.local檔案
如果需要定製自己的登入資訊,注意要修改/etc/rc.d/rc.local檔案,因為每次執行/etc/rc.d/rc.local檔案,都需要修改/etc/issue與/etc/issue.net檔案,可以註解掉這些命令,編輯定製自己的/etc/issue檔案,加快Linux引導過程,並且加入以下內容:
echo "base=0xd8000000 size=0x800000 type=write-combining" >> /proc/mtrr
# Enable NumLock 
for tty in /dev/tty[1-3]; do
setleds -D +num < $tty
done
說明:關於第一行命令請看/usr/src/linux/Documentation/mtrr.txt檔案。

五、定製vim的環境
在Linux環境中,有許多編輯器,主要有emacs、vim、joe等,對於從沒有使用過UNIX或者Linux的人,我的建議是最好找幾本有關的參考書,先了解這些編輯器的使用方法,這樣安裝好Linux後,在編輯與檢視一些配置檔案時,不至於茫然不知所措。特別是vi,一開始難以掌握,最好開始能夠得到高手的指點,一旦上手,簡直就像駕駛一輛高速賓士的賽車,初學者可以到ftp://ftp.cs.pdx.edu/pub/elvis或者http://www.vim.org下載相應的版本,它提供支援MSDOS、Windows 98等作業系統的版本,解壓後就可以在自己熟悉的環境學習與使用vi。 
以下是/usr/share/vim/vimrc的例子:
___________________________________________________________
"" (for Chinese)
" Set file encoding format as 'prc' for Simplified Chinese
" Valid values for 'set fe='http://www.itdba.com/blog/ is ansi, japan, korea, prc, taiwan
" We set fe to prc for default, if you need single-byte editing,
" just type ':set fe=ansi' will do the work.

set fe=prc

"" (for Chinese)
"" Set ruler on, you must set this to ensure
"" the Chinese functionality of gvim
set ru

"" For syntax color
if &term=="xterm"
set term=rxvt
set t_Co=8
set t_Sb=^[4%dm
set t_Sf=^[3%dm
endif
syntax on

"" Set visual bell and disable screen flash
" :set vb t_vb=

set bs=2

"" Toggle on/off highlightsearch
map :set hls!set hls?

"" Toggle on/off paste mode
map :set paste!set paste?

"" You can toggle the syntax on/off with this command
if has("syntax_items") | syntax off | else | syntax on | endif
map :if has("syntax_items") syntax off else syntax on endif

"" Set non-compatible with vi
set nocompatible 

"" Set backup extension
" set backup
" set backupext=.bak

" add by zws
set autoindent
set ignorecase
set shiftwidth=4 
set showmode
set tabstop=4
set nowrap
"if &t_Co > 1
" syntax on
"endif
"" Map for parenthesis matching
map %
"" search English word
map :set keywordprg=/usr/local/bin/cdictK:set keyword=man
map :set keywordprg=manK
" backspace
map X
___________________________________________________________

六、編輯/etc/mtools.conf檔案
一般情況下,都是在自己的機器安裝Windows 98與Linux兩種作業系統,通過安裝mtools包,可以不用安裝相應的檔案系統,允許在MSDOS、Windows 98與Linux的檔案系統之間,實現檔案的讀、寫、移動、顯示等操作。假設Windows 98安裝在/dev/hda1、/dev/hda5分割槽下,修改/etc/mstools.conf檔案,加入以下內容:
drive c: file="/dev/hda1"
drive d: file="/dev/hda5"
這樣就可以執行mdir c:等命令。 

七、拷貝各種配置檔案
根據機器的用途,編輯拷貝相應的配置檔案,例如:samba的配置檔案/etc/smb.conf,域名伺服器的配置檔案,DHCP的配置檔案/etc/dhcpd.conf等檔案,在一般情況下,這些檔案一旦配置好,很少發生改動,這樣可以節約許多時間,注意原來的配置檔案一定要做好備份,如果配置檔案發生改動也要做好備份!

八、編譯Linux核心
一般各種Linux系統套件安裝的核心,都適合大部分的機器,但缺點是大而全,模組中包含有許多自己根本不需要的模組。只有根據自己的要求及機器硬體配置,來配置核心才能達到最優配置。注意在編譯核心前,一定要對自己機器有一個全面的瞭解,這樣才能做到心中有數。

以下是編譯核心的簡要步驟:(以redhat6.1為例說明) 
1.進入/usr/src/linux,編輯Makefile檔案,修改EXTRAVERSION= -xx,其中xx表示編譯的版本號,最好用數字,並且大於原來的數值。
2.根據自己機器的硬體配置,選擇相應的選項來配置核心。
#make menuconfig
3.理順各檔案的依存關係,清理以前生成的目標檔案及其他檔案。
#make clean ; make dep
4.編譯核心與安裝,並且安裝模組。
#make bzImage
#make modules; make modules_install
5.安裝核心。
# cp arch/i386/boot/bzImage /boot/vmlinuz-y.y.yy-xx
將核心複製到/boot目錄下,其中y.y.yy表示Linux版本號,xx表示編譯的版本號。
# cp System.map /boot/System.map-y.y.yy-xx
# cd /boot
# ln -s System.map-y.y.yy-xx System.map
6.編輯lilo的配置檔案/etc/lilo.conf(略)。
注意最好保留原來的核心,這樣如果新建的核心引導Linux不成功,可以利用舊的核心引導Linux作業系統。 
7.測試新核心。
引導完成後,注意檢查/lib/modules/y.y.yy-xx目錄下,是否建立modules.dep檔案。如果檔案存在,可以修改/etc/rc.d/rc.sysinit,將有關執行模組依賴的命令的內容註解掉,這樣可以加快Linux的啟動過程。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/35489/viewspace-84239/,如需轉載,請註明出處,否則將追究法律責任。

相關文章