[ 永遠的UNIX::UNIX技術資料的寶庫 ]   GB | BIG5

首頁 > 系統管理 > Linux > 正文
Linux的配置與優化
本文出自:http://www.swm.com.cn 作者:張文勝 (2002-07-31 06:02:00)

Linux的配置與優化

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=' 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 <F8> :set hls!<bar>set hls?<cr>

"" Toggle on/off paste mode
map <F9> :set paste!<bar>set paste?<cr>

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

"" 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 <F3> <Esc>:set keywordprg=/usr/local/bin/cdict<Esc>K<Esc>:set keyword=man<CR>
map <F6> <Esc>:set keywordprg=man<Esc>K
" backspace
map <BS> 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的啟動過程。


(http://www.fanqiang.com)
    進入【UNIX論壇

相關文章
Linux系統配置與優化經驗談 (2002-01-08 07:10:00)
 

★  樊強制作 歡迎分享  ★