以下文章轉載自http://www.weithenn.idv.tw/
==============================================

CentOS 5.1-初始環境設定

【前言】

以下前言內容擷於[CentOS - Wikipedia];CentOS(Community ENTerprise Operating System) 是 Linux發行版之一,它是來自於 Red Hat Enterprise Linux 依照開放原始碼規定釋出的原始碼所編譯而成。由於出自同樣的原始碼,因此有些要求高度穩定性的伺服器以 CentOS 替代商業版的 Red Hat Enterprise Linux 使用。兩者的不同,在於 CentOS 並不包含封閉原始碼軟體,簡單說就是不用錢的 RHEL 不過享受自由當然也得不到商業支援了。

【作業環境】

CentOS 5.1 (Linux 2.6.18-53.el5)

【安裝及設定】

以 下是記錄我個人在安裝 CentOS 後會做的一些設定,至於如何安裝 CentOS?由於網路上文件夠多了所以就不重新造輪子了,要注意的大概就是 /var 不要切太小因為使用 yum update 時暫存資料都放在 /var/cache/yum,其它都差不多...那就開始吧。

設定 IP Address

方式一、文字對話模式安裝

 #system-config-network

方式二、修改檔案

 #cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 //網路卡名稱
BOOTPROTO=static //BOOTP協定
BROADCAST=192.168.202.255 //廣播 IP Address
HWADDR=00:15:C5:E5:99:B1 //MAC Address
IPADDR=192.168.202.100 //IP Address
NETMASK=255.255.254.0 //Netmask (遮罩)
NETWORK=192.168.202.0 //網段
ONBOOT=yes //開機自動啟動

選擇上列其中之一方式設定後,重新啟動網卡服務

 #/etc/rc.d/init.d/network restart   //方式一
#service network restart //方式二

關閉 SELinux

Linux 核心是 2.6 以上的就會自動載入 SELinux,但因為才剛開始玩 CentOS 對它還不熟為了減少挫折就先關了吧,等以後熟點再說,關於 SELinux 是什可以參考這三篇詳細的說明。[鳥哥的 Linux 私房菜 -- 認識網路安全 SELinux][SELinux - Wikipedia][SELinux 設定範例]

 #vi /etc/sysconfig/selinux
SELINUX=enforcing //預設值 (阻擋不安全的有取行為)
SELINUX=disabled //修改後 (關閉 SELinux 功能)

如何查看目前系統的 SELinux 狀態?

 #sestatus
SELinux status: enabled //可知是有啟用的
SELinuxfs mount: /selinux
Current mode: permissive
Mode from config file: disabled
Policy version: 21
Policy from config file: targeted

以下訊息可知目前系統完全關閉 SELinux 功能

 #sestatus
SELinux status: disabled

新增使用者、搬移家目錄、設定使用的 shell

我習慣把 user 放在同一個資料夾下(/home/user),而 adduser 預設會把使用者家目錄建立在 /home/weithenn。

 #mkdir /home/user                          //建立放置使用者家目錄的目錄
#adduser -d /home/user/weithenn weithenn //指定家目錄位置、新增使用者名稱 weithenn
#passwd weithenn //設定使用者 weithenn 密碼
Changing password for user weithenn.
New UNIX password: //輸入密碼
Retype new UNIX password: //再輸入一次密碼以確認
passwd: all authentication tokens updated successfully.

CentOS 預設支援的 Shell 如下,而我個人是喜歡使用 tcsh shell

 #cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh

修改使用者家目錄路徑及 shell

 #vipw
weithenn:x:500:500::/home/weithenn:/bin/bash //預設值
weithenn:x:500:500::/home/user/weithenn:/bin/tcsh //修改後

建立 ~/.cshrc

設定 ~/.cshrc (/home/user/weithenn/.cshrc) 以便登入時載入相關環境設定,內容如下

 set path = (/usr/kerberos/sbin /usr/kerberos/bin /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin $HOME/bin)
if ($?prompt) then
# An interactive shell -- set some stuff up
if ( ! $?WINDOW ) then
set prompt = '[%B%m%b] %B%~%b %# '
else
set prompt = "[%B%m:$WINDOW%b] %B%~%b %# "
if ( ! $?SUDO_USER ) then
eval "`tset -s $TERM`"
endif
endif
endif
setenv LANG en_US.UTF-8
setenv LC_ALL en_US.UTF-8
setenv LESSCHARSET latin1
setenv LC_TIME C
setenv EDITOR vim
alias ls ls --color=tty
alias h history 25
alias j jobs -l
alias la ls -a --color=tty
alias lf ls -FA --color=tty
alias ll ls -lA --color=tty

建立 ~/.vimrc

設定 ~/.vimrc (/home/user/weithenn/.vimrc) 以便使用 vim 時載入相關環境設定,內容如下

 set fileencoding=taiwan
set guifontset=8x16,kc15f,-*-16-*-big5-0
set cindent
set enc=taiwan
set fileencoding=taiwan
set hls
set nocompatible
set sw=2
syntax on
highlight Comment ctermfg=darkcyan
highlight Search term=reverse ctermbg=4 ctermfg=7

設定 sudo、封鎖 root 密碼

設定 sudo 才知道哪些人用 root 權限做了哪些事,若不喜歡用 sudo 還有許多其它方式來禁止 root 存取的方式,詳細方式可參考[Red Hat Enterprise Linux 4: 安全性設定手冊 第 4章. 工作站的保全]

 #which sudo visudo                 //確定 sudo 是否安裝
/usr/bin/sudo
/usr/sbin/visudo

設定 visudo 不用再鍵入密碼就可變成 root、開啟 sudo log 功能,修改完 visudo 後若有人執行 sudo command 則 /var/log/sudo.log 會自動建立。

 #visudo
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL //預設值
%wheel ALL=(ALL) NOPASSWD: ALL //修改後
#Sudo log //並加入如下二行
Host_Alias SERVER = 192.168.202.100 //指定本機 IP
Defaults@SERVER log_year, logfile=/var/log/sudo.log //指定 log 格式及路徑

設定 sudo log 二種格式 (log_host、log_year)寫入 /var/log/sudo.log 內容的差異

 Dec 27 18:08:27 2007 : weithenn : TTY=pts/2 ; PWD=/home/user/weithenn ;    USER=root ; COMMAND=/usr/sbin/ntsysv                   //使用 log_year
Dec 27 18:13:27 : weithenn : HOST=centos-jiakai : TTY=pts/2 ; PWD=/home/user/weithenn ; USER=root ; COMMAND=/usr/sbin/visudo //使用 log_host

將使用者 weithenn 加入 wheel 郡組,也就是只有 weithenn 才能變成 root

 #vi /etc/group
wheel:x:10:root //預設值
wheel:x:10:root,weithenn //修改後

封鎖 root 密碼,因為我不喜歡再記 root 的密碼且放著也覺得不好所以就把 root password 給封了吧,方式就是修改 /etc/shadow 的第二欄位將內容改成 * 即可。

 #vipw
root:$1$qa1xL66K$DSp11kLKJZOx05FZsEFle0:13874:0:99999:7::: //預設值
root:*:13874:0:99999:7::: //修改後

設定 hostname

若安裝時 hostname 沒設定好就設定一下吧。

 #vi /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=centos-test.weithenn.idv.tw //設定 hostname

設定本地時間及每天自動校對時間

若安裝時不小心選到 UTC 的話就改回成 CST Taipei 吧。

 #cp /usr/share/zoneinfo/Asia/Taipei /etc/localtime
#date
Thu Dec 27 17:07:29 CST 2007

若區網內沒架設 NTP 就寫個簡單的 shell 去每天對時吧,把 script (此例叫 ntpdate.sh) 放在 /etc/cron.daily 並給予 755 的權限就會每天自動對時了。

 #vi /etc/cron.daily/ntpdate.sh                               //建立對時 script 名稱為 ntpdate.sh 內容如下
#!/bin/sh
ntpdate -s watch.stdtime.gov.tw
#chmod 755 /etc/cron.daily/ntpdate.sh //給予 755 權限讓系統能自動執行

今天安裝 CentOS 5.2 (32bit) 預設居然沒有 /usr/sbin/ntpdate 這個檔案,所以當您執行 /etc/cron.daily/ntpdate.sh 變會說 ntpdate: command not found. 請利用 yum 安裝 ntp 套件吧。

 #yum -y install ntp

查看機器 Listen 哪些 Port

查看機器到底 Listen 哪些 Port (FreeBSD 的話就用 sockstat 吧!!)

顯示服務名稱

 #netstat -tl                                    
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:ssh *:* LISTEN

顯示 Port Number

 #netstat -tnl                                   
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::22 :::* LISTEN

查看機器啟動哪些 Service

 chkconfig --list [name]                 //顯示指定服務 runlevel 情形
chkconfig --add <name> //新增指定服務
chkconfig --del <name> //刪除指定服務
chkconfig [--level <levels>] <name> <on|off|reset|resetpriorities>

或是利用 ntsysv 也可設定開機是否啟動指定服務

RunLevel

若是使用文字介面來安裝那預設就會使用文字介面來開機 (init 3),若使用圖形介面安裝則預設就會使用圖形介面 (Gnome or KDE) 來開機 (init 5)。詳細可參考[Linux啟動程序][RunLevel - DebianWiki]

 #cat /etc/inittab
# 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) //多人模式,無 NFS 功能 (同 init 3 若你不需要網路的話)
# 3 - Full multiuser mode //多人模式 (文字介面)
# 4 - unused
# 5 - X11 //多人模式 (視窗介面)
# 6 - reboot (Do NOT set initdefault to this) //重開機
#
id:3:initdefault: //3 文字模式,5 X-WIN

設定 yum mirror 為台灣地區

Yum (Yellow dog Updater, Modified) 為 Fedora 及 CentOS 用來管理 RPM 安裝,可解決套件相依性問題。詳細內容可參考[Yellow Dog Updater Modified - Wikipedia]

當然預設 Mirror Site 一定是找國外的,所以我們把站台設回國內 Mirror Site 這樣使用 YUM 安裝、更新套件上速度都會加快。

 #cd /etc/yum.repos.d
#cp CentOS-Base.repo CentOS-Base.repo.bak
#sed 's,mirror.centos.org/centos,ftp.cse.yzu.edu.tw/pub/CentOS,g' CentOS-Base.repo.bak > CentOS-Base.repo //元智大學
or
#sed 's,mirror.centos.org/centos,ftp.cs.pu.edu.tw/Linux/CentOS,g' CentOS-Base.repo.bak > CentOS-Base.repo //靜宜大學
or
#sed 's,mirror.centos.org/centos,ftp.isu.edu.tw/pub/Linux/CentOS,g' CentOS-Base.repo.bak > CentOS-Base.repo //義守大學

系統對時,為了避免因為系統時間不準而造成執行 yum 時失敗,我們先以 ntpdate 進行網路對時

 #ntpdate -s watch.stdtime.gov.tw

以下列出常用的 yum 指令

 yum update           //更新套件 (一個一個問)
yum update -y //更新所有套件 (所有回答都回答 Y)
yum check-update //列出需要更新的套件清單
yum upgrade //升級套件
yum install aaa //安裝套件
yum remove aaa //移除套件
yum clean all //清除安裝時的暫存檔
yum search aaa //搜尋套件
yum list //列出可安裝的套件清單
yum info aaa //列出套件訊息
yum grouplist //列出郡組安裝套件清單
yum groupinstall "GNOME Desktop Environment" //安裝 Gnome 套件郡組
yum groupupdate //更新套件群組
yum groupremove //移除套件群組
yum groupinfo //列出郡組安裝套件內含哪些套件
Mandatory Packages: //強制安裝套件
Default Packages: //預設安裝套件
Optional Packages: //建議安裝套件 (自已視需求另外安裝)

安裝 RPMforge

雖然將 yum mirror 設定為台灣地區,不過除了必要的套件之外很多都沒有 (ex. lighttpd...),所以我們可以透過設定 [RPMforge] 來增加套件數量。

執行安裝 [RPMforge] 套件

 #rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
Retrieving http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
warning: /var/tmp/rpm-xfer.k3FbwC: Header V3 DSA signature: NOKEY, key ID 6b8d79e6
Preparing... ########################################### [100%]
1:rpmforge-release ########################################### [100%]

升級 [RPMforge] 套件後在搜尋 lighttpd 就可找到。

 #yum search lighttpd | grep lighttpd.i386
lighttpd.i386 1.4.18-1.el5.rf rpmforge

rpm 基本指令

有時還是需要用到 rpm 指令就記錄一下吧

 rpm -ivh aaa         //安裝 rpm
rpm -e aaa //刪除 rpm
rpm -Uvh aaa //升級 rpm 套件
rpm -qa |grep aaa //查尋該套件版本
rpm -qi aaa //列出該套件詳細資訊
rpm -ql aaa //列出該套件相關檔案
rpm -qc aaa //列出該套件設定檔及啟動檔

關閉不需要的服務

CentOS 為了讓你使用方便所以預設為開啟很多服務,而若我們是把 CentOS 拿來當 Server 在用的話其實有很多服務是用不到的,所以就列一下把不需要的服務關閉一方便減少安全性問題 (有的服務會開 Port),一方面也可以增加系統效能 (因為每個服務都會佔記憶體),以下指令可以知道你的系統預設把多少服務給開啟。

 #chkconfig --list |grep "3:on" |awk '{print $1}' |sort

以我的習慣為例的話,我安裝時就只會選 Desktop KDE (因為公司有用 APC UPS 需要 VNC 連過去設定,且只有 GUI 介面),其它有需要就用 yum 安裝,所以安裝完成後預設會開啟下列服務,至於哪些要停用哪些要啟用就看你的需求吧,以下是我個人的建議 (也是套用我機器上的設定)

預設開啟的服務 建議 說明
acpid 停用 Advanced Configuration and Power Interface 電源進階設定,常用在 Laptop 上
apmd 停用 Adventage Power Management daemon 偵測系統的電池電量,常用在 Laptop 上
atd 停用 /etc/at.allow,/etc/at.deny 我大概都使用 crond 來達成
autofs 停用 自動掛載檔案系統與週邊裝置 (如光碟、USB),常用在 Laptop 上
avahi-daemon 停用 當區網沒有 DNS 服務時會試著尋找提供 zeroconf 協定的主機
bluetooth 停用 Bluetooth 藍芽,常用在 Laptop 上
cpuspeed 停用 控制 CPU 速度主要用來省電,常用在 Laptop 上
cups 停用 Common UNIX Printing System 使系統支援印表機
firstboot 停用 安裝完成後第一次啟動時的服務,設定 auth、firewall、keyboard...
gpm 停用 記錄週邊裝置的檔案資料,例如文字模式下可使用滑鼠
haldaemon 停用 使系統支援 plug and play 裝置
hidd 停用 使系統支援藍芽裝置,例如滑鼠,鍵盤
hplip 停用 使系統支援 HP 相關型號印表機
ip6tables 停用 使系統支援 IPTables Filrewall IPv6
isdn 停用 使系統支援 Integrated Services Digital Network (ISDN) 環境
lm_sensors 停用 使系統支援偵測主機版或硬體,常用在 Laptop 上
messagebus 停用 使系統支援 plug and play 裝置
nfslock 停用 使系統支援 NFS Server 資料一致性功能
pcscd 停用 使系統支援 Smart Card
portmap 停用 使系統支援 NFS Service
rpcgssd 停用 使系統支援 NFSv4 Service
rpcidmapd 停用 使系統支援 NFSv4 Service
yum-updatesd 停用 使系統支援 YUM 自動更新服務 (上線機器還是先確認好再更新)
anacron 視個人需求 執行在 crontab 設定的時間沒有執行到的工作
auditd 視個人需求 稽核系統 /var/log/audit/audit.log,若 auditd 未執行中則稽核訊息會寫入 /var/log/messages (配合 SELinux)
iptables 視個人需求 使系統支援 IPTables Filrewall
irqbalance 視個人需求 使系統支援多個 CPU 環境當系統中斷請求時進行負載平衡的程序,所以單 CPU 的機器就不需要
lvm2-monitor 視個人需求 使系統支援 Logical Volume Manager 邏輯磁區
mcstrans 視個人需求 主要用於配合 SELinux 服務
mdmonitor 視個人需求 使系統支援 RAID 相關程序
microcode_ctl 視個人需求 使系統支援 Intel IA32 CPU 特色,所以 AMD CPU 不需要
netfs 視個人需求 Network File System Mounter 網路掛載系統 (如 NFS、Samba、NCP)
restorecond 視個人需求 使系統支援 SELinux 監控檔案狀態
smartd 視個人需求 使系統支援檢查硬碟狀態 (Hardware RAID 要改設定檔)
sendmail 視個人需求 使系統支援 Sendmail
xfs 視個人需求 使系統支援 X Window 字型
crond 啟動 /etc/cron.* 排程服務
kudzu 啟動 使系統每次開機會都自動執行硬體偵測
network 啟動 使系統支援網卡
readahead_early 啟動 使系統再啟動時預先將相關檔案讀到記憶體內來加快啟動速度
sshd 啟動 使系統支援 SSH Service
syslog 啟動 使系統支援 System Log Service

決定哪些服務要開哪些服務要關就來執行關閉服務及關閉啟動機器時不啟動服務,以下是我個人的需求。

 service acpid stop ; chkconfig acpid off
service apmd stop ; chkconfig apmd off
service atd stop ; chkconfig atd off
service auditd stop ; chkconfig auditd off
service autofs stop ; chkconfig autofs off
service avahi-daemon stop ; chkconfig avahi-daemon off
service bluetooth stop ; chkconfig bluetooth off
service cpuspeed stop ; chkconfig cpuspeed off
service cups stop ; chkconfig cups off
service firstboot stop ; chkconfig firstboot off
service gpm stop ; chkconfig gpm off
service haldaemon stop ; chkconfig haldaemon off
service hidd stop ; chkconfig hidd off
service hplip stop ; chkconfig hplip off
service ip6tables stop ; chkconfig ip6tables off
service isdn stop ; chkconfig isdn off
service lm_sensors stop ; chkconfig lm_sensors off
service mcstrans stop ; chkconfig mcstrans off
service messagebus stop ; chkconfig messagebus off
service netfs stop ; chkconfig netfs off
service nfslock stop ; chkconfig nfslock off
service pcscd stop ; chkconfig pcscd off
service portmap stop ; chkconfig portmap off
service restorecond stop ; chkconfig restorecond off
service rpcgssd stop ; chkconfig rpcgssd off
service rpcidmapd stop ; chkconfig rpcidmapd off
service yum-updatesd stop ; chkconfig yum-updatesd off
service smartd stop ; chkconfig smartd off

【參考】

[Minimal Services on CentOS 4.4 Mini-HowTo >> anchorite.org]

[迷你CentOS]

[轉載: FEDORA 相關服務 Yahoo!奇摩部落格]

[Services in Fedora Core 6]

[Red Hat Enterprise Linux 4 Update 2 發行公告]

[分享Fedora 7安裝和配置 - Red Hat / Fedora / CentOS - Linux - www.allhabit.com 香港討論區,香港論壇 - Powered by Discuz!]

[Linux守護進程列表 - ◇Linux新手入門 - 開源快訊論壇 - Powered by Discuz!]

關閉多餘的 tty

大部份都使用 putty 來連線到 CentOS 處理,所以 tty (本機 console) 常用不到預設會開啟 6 個,修改成只開啟 2 個。

 #vi /etc/inittab
# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
#3:2345:respawn:/sbin/mingetty tty3 //mark 掉
#4:2345:respawn:/sbin/mingetty tty4 //mark 掉
#5:2345:respawn:/sbin/mingetty tty5 //mark 掉
#6:2345:respawn:/sbin/mingetty tty6 //mark 掉
arrow
arrow
    全站熱搜

    ITMan 發表在 痞客邦 留言(0) 人氣()