Linux的配置与优化

/ns/wz/sys/data/20020809014315.htm

Linux的配置与优化

本文出自:http://www.swm.com.cn 作者:张文胜


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的启动过程。