2013年9月10日 星期二

安裝 Debian GNU/Linux 7 (Wheezy)

過去我在部落格上貼過一些 Linux 的安裝記錄,不過對於 Debian GNU/Linux 安裝的部份,似乎就沒有自己整理一些比較連貫的文章。這幾天我將手邊跑 Debian 那台 ThinkPad X61 重新安裝,順便整理了一下安裝的過程。至於本文的擷圖部份,為了方便操作,所以我利用 KVM / libvirt 也裝了一個同樣的 Debian 環境,然後在 Windows 下配合 XshellXming 秀出視窗,再用 PicPick 擷圖。

Step 1.

對於安裝 Debian GNU/Linux,我經常會參考這兩篇文章:

在第一篇文章裡介紹了圖形化的安裝方式 (Graphical install),不過我比較偏好採用文字介面的方式 (Install) 來安裝 Debian GNU/Linux。

在安裝過程中,我會先安裝 SSH 伺服器和基本的工具 (Standard system utilities):

這是因為這陣子以來,我習慣將 Debian 作為遠端登入操作的選擇,所以並不特別需要安裝桌面套件。在安裝完基本的系統後,就可以在文字介面登入 Debian 了。

Step 2.

當第一次登入 Debian 後,我會執行 su - 指令變成 root 身份,然後執行下面的指令安裝 ufw 及 sudo。

$ sudo apt-get install ufw sudo

接著,參考《在 Debian 上使用 ufw 設定防火牆》這篇文章啟用 ufw 並設定防火牆。

然後,再執行 visudo 指令修改 /etc/sudoers,讓安裝基本系統時建立的使用者帳號能使用 sudo 指令。

Step 3.

修改 /etc/apt/sources.list 的內容如下,目前我偏向採用 Stable 的版本,雖然它的軟體不一定是當前的最新版本,但穩定度很高。而我的習慣是將 Debian 當作伺服器來用,所以就選擇這個版本了。

# stable
deb http://http.us.debian.org/debian/ stable main contrib non-free
deb-src http://http.us.debian.org/debian/ stable main contrib non-free

# stable-updates, previously known as 'volatile'
deb http://http.us.debian.org/debian/ stable-updates main contrib non-free
deb-src http://http.us.debian.org/debian/ stable-updates main contrib non-free

# security / stable
deb http://security.debian.org/ stable/updates main contrib non-free
deb-src http://security.debian.org/ stable/updates main contrib non-free

Step 4.

一直以來,我將安裝 Debian GNU/Linux 時需要安裝的套件寫成了腳本,接著只要執行這個腳本就可以將大部份需要的套件全部安裝好。

#!/bin/bash

# Get information from Debian GNU/Linux
VERSION=`cat /etc/debian_version`
ARCH=`uname -a | awk '{print $8}'`

# Get Account's ID
THIS_ID=`echo $HOME | cut -d '/' -f 3`

echo Hello $THIS_ID, your system is Debian GNU/Linux $VERSION, $ARCH.

PKG_BASIC="xserver-xorg-core xinit"
PKG_I18N="ttf-arphic-ukai ttf-arphic-uming ttf-kochi-gothic ttf-kochi-mincho ttf-sazanami-gothic ttf-sazanami-mincho ttf-sawarabi-gothic ttf-sawarabi-mincho ttf-wqy-microhei ttf-wqy-zenhei msttcorefonts"

# Development
PKG_EDITOR="vim ctags cscope global"
PKG_COMPILER="make gdb gcc build-essential gcc-multilib g++-multilib gdb-multiarch"
PKG_LIBS="libncurses5-dev libnetpbm10-dev libsdl-dev libesd0-dev zlib1g-dev liblzo2-dev libusb-dev libreadline6-dev binutils-dev libc6-dev linux-libc-dev libx11-dev xorg-dev libpam-dev libxrender-dev libgl1-mesa-dev libgl1-mesa-glx libxml2-dev x11proto-core-dev"
PKG_UTILITIES="gnupg minicom lrzsz netpbm libtool bison flex doxygen indent fakeroot valgrind gperf zip curl uboot-mkimage tmux binutils ccache intltool mtools alien gawk pngcrush schedtool mingw32 tofrodos python-setuptools python-markdown xsltproc libxml2-utils python-libxml2 genext2fs genisoimage syslinux squashfs-tools"
PKG_DOCS="pinfo info2man manpages manpages-dev manpages-posix manpages-posix-dev glibc-doc glibc-doc-reference"
PKG_KERNEL="linux-headers-$(uname -r) kernel-package firmware-linux firmware-linux-free firmware-linux-nonfree"
PKG_VCTRL="git-core git-cola git-cvs git-svn gitk cvs subversion mercurial mercurial-git"
PKG_JAVA="openjdk-7-jdk"
PKG_EXTRA=""

# Service 
PKG_SERVICE="samba samba-tools libpam-smbpass ssh"

# Packages depond on 32-bit or 64-bit
case $ARCH in
 "x86_64")
  PKG_ARCH="libc6-dev-i386 lib32ncurses5-dev lib32readline6-dev lib32z-dev binutils-multiarch"
  ;;
 "i686")
  PKG_ARCH=""
  ;;
esac

# All packages
PACKAGES="$PKG_BASIC $PKG_I18N $PKG_EDITOR $PKG_COMPILER $PKG_LIBS $PKG_UTILITIES $PKG_DOCS $PKG_KERNEL $PKG_VCTRL $PKG_JAVA $PKG_EXTRA $PKG_SERVICE $PKG_ARCH"

# If running Gnome, you can use DEBIAN_FRONTEND='gnome' to show Gnome's dialog.
sudo apt-get update
sudo apt-get -y --force-yes dist-upgrade
sudo apt-get install -y --force-yes $PACKAGES

echo 'done.'

Step 5.

Debian GNU/Linux 7 並沒有收錄 Oracle 的 Java JDK / JRE,因此在安裝時我會先安裝上 OpenJDK 7。但,接著我仍然會再將 Oracle JDK 6 安裝起來。

首先自 Oracle 的網站找到 JDK 6 的 Linux 版本,由於這裡我安裝的是 64 位元的 Debian,因此下載了 jdk-6u45-linux-x64.bin 這個檔案。

執行 jdk-6u45-linux-x64.bin 這個檔案,會解壓縮並得到 jdk1.6.0_45 這個目錄,將這個目錄搬移到 /usr/lib/jvm/ 目錄內。

接著建立 /usr/lib/jvm/.jdk1.6.0_45.jinfo 這個檔案,內容如下:

name=jdk1.6.0_45
alias=java-6-oracle
priority=63
section=non-free

jre ControlPanel /usr/lib/jvm/jdk1.6.0_45/jre/bin/ControlPanel
jre java /usr/lib/jvm/jdk1.6.0_45/jre/bin/java
jre java_vm /usr/lib/jvm/jdk1.6.0_45/jre/bin/java_vm
jre javaws /usr/lib/jvm/jdk1.6.0_45/jre/bin/javaws
jre jcontrol /usr/lib/jvm/jdk1.6.0_45/jre/bin/jcontrol
jre keytool /usr/lib/jvm/jdk1.6.0_45/jre/bin/keytool
jre policytool /usr/lib/jvm/jdk1.6.0_45/jre/bin/policytool
jre pack200 /usr/lib/jvm/jdk1.6.0_45/jre/bin/pack200
jre rmid /usr/lib/jvm/jdk1.6.0_45/jre/bin/rmid
jre rmiregistry /usr/lib/jvm/jdk1.6.0_45/jre/bin/rmiregistry
jre unpack200 /usr/lib/jvm/jdk1.6.0_45/jre/bin/unpack200
jre orbd /usr/lib/jvm/jdk1.6.0_45/jre/bin/orbd
jre servertool /usr/lib/jvm/jdk1.6.0_45/jre/bin/servertool
jre tnameserv /usr/lib/jvm/jdk1.6.0_45/jre/bin/tnameserv
jre jexec /usr/lib/jvm/jdk1.6.0_45/jre/lib/jexec
jdk appletviewer /usr/lib/jvm/jdk1.6.0_45/bin/appletviewer
jdk apt /usr/lib/jvm/jdk1.6.0_45/bin/apt
jdk extcheck /usr/lib/jvm/jdk1.6.0_45/bin/extcheck
jdk idlj /usr/lib/jvm/jdk1.6.0_45/bin/idlj
jdk jar /usr/lib/jvm/jdk1.6.0_45/bin/jar
jdk jarsigner /usr/lib/jvm/jdk1.6.0_45/bin/jarsigner
jdk javac /usr/lib/jvm/jdk1.6.0_45/bin/javac
jdk javadoc /usr/lib/jvm/jdk1.6.0_45/bin/javadoc
jdk javah /usr/lib/jvm/jdk1.6.0_45/bin/javah
jdk javap /usr/lib/jvm/jdk1.6.0_45/bin/javap
jdk jconsole /usr/lib/jvm/jdk1.6.0_45/bin/jconsole
jdk jdb /usr/lib/jvm/jdk1.6.0_45/bin/jdb
jdk jhat /usr/lib/jvm/jdk1.6.0_45/bin/jhat
jdk jinfo /usr/lib/jvm/jdk1.6.0_45/bin/jinfo
jdk jmap /usr/lib/jvm/jdk1.6.0_45/bin/jmap
jdk jps /usr/lib/jvm/jdk1.6.0_45/bin/jps
jdk jrunscript /usr/lib/jvm/jdk1.6.0_45/bin/jrunscript
jdk jsadebugd /usr/lib/jvm/jdk1.6.0_45/bin/jsadebugd
jdk jstack /usr/lib/jvm/jdk1.6.0_45/bin/jstack
jdk jstat /usr/lib/jvm/jdk1.6.0_45/bin/jstat
jdk jstatd /usr/lib/jvm/jdk1.6.0_45/bin/jstatd
jdk native2ascii /usr/lib/jvm/jdk1.6.0_45/bin/native2ascii
jdk rmic /usr/lib/jvm/jdk1.6.0_45/bin/rmic
jdk schemagen /usr/lib/jvm/jdk1.6.0_45/bin/schemagen
jdk serialver /usr/lib/jvm/jdk1.6.0_45/bin/serialver
jdk wsgen /usr/lib/jvm/jdk1.6.0_45/bin/wsgen
jdk wsimport /usr/lib/jvm/jdk1.6.0_45/bin/wsimport
jdk xjc /usr/lib/jvm/jdk1.6.0_45/bin/xjc
plugin xulrunner-1.9-javaplugin.so /usr/lib/jvm/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so
plugin firefox-javaplugin.so /usr/lib/jvm/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so
plugin iceape-javaplugin.so /usr/lib/jvm/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so
plugin iceweasel-javaplugin.so /usr/lib/jvm/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so
plugin mozilla-javaplugin.so /usr/lib/jvm/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so
plugin midbrowser-javaplugin.so /usr/lib/jvm/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so
plugin xulrunner-javaplugin.so /usr/lib/jvm/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so

再建立 /usr/lib/jvm/debian-orcale-jdk6-alternatives.sh 這個檔案,內容如下:

#!/bin/bash
# "Register JDK7 for update-alternatives Script in Debian Way"
# Based on 1. codeslinger's "install_java7_alternatives" from https://gist.github.com/445930
#          2. oracle-java6-plugin.deb postint script (for plugin)
# Modified by Cheng-Wei Chien<e.cwchien@gmail.com>, 2011-07-30
# ------------------------------------------------------------------------------
# Note:  This script works on x86 Debian GNU/Linux.
#        If you use other arch or distro, you may have to modify some parameters.
#
# Usage: 1. Extract jdk7 tar.gz to, for example, /usr/lib/jvm/java-7-oracle-1.7.0,
#           create a symbolic link /usr/lib/jvm/java-7-oracle to it.
#        2. Download java-7-oracle.jinfo from https://gist.github.com/1114515,
#           move it to /usr/lib/jvm/.java-7-oracle.jinfo, do some modification if necessary.
#        3. Run this script with sudo
#        4. sudo update-java-alternatives -s java-7-oracle
#        5. Have fun with jdk7!
#
# Trouble Shooting: If something goes wrong,
#        1. Uncomment "remove" commands and comment "install" commands.
#        2. Run this script again to rollback.
#        3. Fix what causes the problem, maybe typo or something else.
#        4. Comment "remove" commands and uncomment "install" commands
#        5. Run this script to update

# Force the shell to exit immediately if something goes wrong
set -e

# Follow the Debian's priority rule for java5 = 53, java6 = 63 .. I guess
priority=63

# Your jdk7 home, in this case, it's /usr/lib/jvm/java-7-oracle-1.7.0
# But I create a symbolic link /usr/lib/jvm/java-7-oracle to meet Debian's way
basedir=/usr/lib/jvm/jdk1.6.0_45

# Your man1 dir, you don't need to change this
mandir=$basedir/man/man1

# Variables from /usr/lib/jvm/.java-6-oracle.jinfo except HtmlConverter (not in jdk7)
jdk_var='appletviewer apt extcheck idlj jar jarsigner javac javadoc javah javap jconsole jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic schemagen serialver wsgen wsimport xjc'
jre_var='ControlPanel java java_vm javaws jcontrol keytool pack200 rmid rmiregistry unpack200 orbd servertool tnameserv jexec policytool'
# browser_var="xulrunner-addons firefox iceape iceweasel mozilla midbrowser xulrunner"
browser_var="firefox iceweasel mozilla midbrowser"

# Java plugin path, you don't need to change this
plugin=$basedir/jre/lib/amd64/libnpjp2.so

# First, gzip man1 files to meet the 1.gz form
if [ -e $mandir ]; then
  for i in $mandir/*.1; do
    gzip $i
  done
fi

# Register jdk variables
for i in $jdk_var; do
  unset slave || true
  if [ -e $mandir/$i.1.gz ]; then
    slave="--slave /usr/share/man/man1/$i.1.gz $i.1.gz $mandir/$i.1.gz"
  fi
  #update-alternatives --remove $i $basedir/bin/$i
  update-alternatives --install /usr/bin/$i $i $basedir/bin/$i $priority $slave
done

# Register jre variables
for i in $jre_var; do
  unset slave || true
  if [ -e $mandir/$i.1.gz ]; then
    slave="--slave /usr/share/man/man1/$i.1.gz $i.1.gz $mandir/$i.1.gz"
  fi
  if [ $i = jexec ]; then
    # update-alternatives --remove jexec $basedir/jre/lib/jexec
    update-alternatives --install /usr/bin/jexec jexec $basedir/jre/lib/jexec $priority
  elif [ $i = java ]; then
    # update-alternatives --install /usr/bin/java java $basedir/jre/bin/java $priority $slave --slave /usr/bin/policytool policytool $basedir/jre/bin/policytool
    update-alternatives --install /usr/bin/java java $basedir/jre/bin/java $priority $slave
  else
    # update-alternatives --remove $i $basedir/jre/bin/$i
    update-alternatives --install /usr/bin/$i $i $basedir/jre/bin/$i $priority $slave
  fi
done

# Register plugins
for i in $browser_var; do
  if [ $i = xulrunner-addons ]; then
    browser=xulrunner-1.9
  else
    browser=$i
  fi
  mkdir -p /usr/lib/$i/plugins
  #update-alternatives --remove $browser-javaplugin.so $plugin
  update-alternatives --install /usr/lib/$i/plugins/libjavaplugin.so $browser-javaplugin.so $plugin $priority
done

然後執行 /usr/lib/jvm/debian-orcale-jdk6-alternatives.sh 將 Oracle JDK 6 安裝到系統裡。

接著執行這個指令列出系統有那些 JDK:

$ sudo update-java-alternatives -l

此時應該能看到稍早安裝的 OpenJDK 7,和剛才安裝的 Oracle JDK 6 了。

java-1.7.0-openjdk-amd64 1051 /usr/lib/jvm/java-1.7.0-openjdk-amd64
jdk1.6.0_45 63 /usr/lib/jvm/jdk1.6.0_45

接著,再執行這個指令將預設的 JDK 改成 Oracle JDK 6 就可以了。

$ sudo update-java-alternatives -s jdk1.6.0_45

Step 6.

接著參考《使用 GitHub 管理 VIM》這篇文章及 https://github.com/yuanfarn/vimrc/blob/master/README.md 的說明,建立 ~/.vimrc 及 ~/.vim 的內容。摘錄指令如下:

$ git clone https://github.com/yuanfarn/vimrc.git ~/.vim
$ cd ~/.vim
$ git submodule init
$ git submodule update
$ cd ~ && ln -s ~/.vim/vimrc ~/.vimrc
$ vim +BundleInstall

Step 7.

接著,我會執行以下指令設定 git 的幾項參數:

$ git config --global user.name "Shau-Hung Hsieh"
$ git config --global user.email "shauhung -AT- gmail.com"
$ git config --global color.diff auto
$ git config --global color.status auto
$ git config --global color.branch auto
$ git config --global color.log auto

Step 8.

Debian 預設的 Samba 設定裡,每個使用者透過 Samba 對自己的家目錄是「唯讀」的,因此我通常會修改 Samba 的 /etc/samba/smb.conf,讓使用者可以透過 Samba 寫入自己的家目錄。修改前後差異如下:

--- /etc/samba/smb.conf 2013-09-10 05:59:58.410688535 +0000
+++ /etc/samba/smb.conf 2013-09-10 06:02:28.674702041 +0000
@@ -99,7 +99,7 @@
 # in this server for every user accessing the server. See
 # /usr/share/doc/samba-doc/htmldocs/Samba3-HOWTO/ServerType.html
 # in the samba-doc package for details.
-#   security = user
+   security = user

 # You may wish to use password encryption.  See the section on
 # 'encrypt passwords' in the smb.conf(5) manpage before enabling.
@@ -247,11 +247,11 @@

 # By default, the home directories are exported read-only. Change the
 # next parameter to 'no' if you want to be able to write to them.
-   read only = yes
+   read only = no

 # File creation mask is set to 0700 for security reasons. If you want to
 # create files with group=rw permissions, set next parameter to 0775.
-   create mask = 0700
+   create mask = 0600

 # Directory creation mask is set to 0700 for security reasons. If you want to
 # create dirs. with group=rw permissions, set next parameter to 0775.

基本上,改動的內容以家目錄 (HOME) 的分享為主:

# "security = user" is always a good idea. This will require a Unix account
# in this server for every user accessing the server. See
# /usr/share/doc/samba-doc/htmldocs/Samba3-HOWTO/ServerType.html
# in the samba-doc package for details.
   security = user

[homes]
   comment = Home Directories
   browseable = no

# By default, the home directories are exported read-only. Change the
# next parameter to 'no' if you want to be able to write to them.
   read only = no

# File creation mask is set to 0700 for security reasons. If you want to
# create files with group=rw permissions, set next parameter to 0775.
   create mask = 0600

# Directory creation mask is set to 0700 for security reasons. If you want to
# create dirs. with group=rw permissions, set next parameter to 0775.
   directory mask = 0700

# By default, \\server\username shares can be connected to by anyone
# with access to the samba server.
# The following parameter makes sure that only "username" can connect
# to \\server\username
# This might need tweaking when using external authentication schemes
   valid users = %S

Step 9.

安裝到這裡,我已經擁有一個很好用的 Debian GNU/Linux 環境了。如同本文一開始所說的,我習慣以遠端登入的方式來操作,所以並沒有安裝 X 視窗 (X Window System)。不過,在我的腳本裡確實安裝了 X 視窗的基本套件 (xserver-xorg-core 與 xinit) 和幾個字型的套件。

如果需要在本機前面操作,且需要視窗介面時,目前 Debian GNU/Linux 已收錄了 Gnome 3,只要執行這個指令安裝基本的 Gnome 套件:

sudo apt-get install gnome-core

或是執行這個指令安裝更完整的 Gnome 桌面套件:

sudo apt-get install gnome

下圖就是安裝了 Gnome 的樣子:

如果只是偶爾需要在主機前使用少數圖形化的軟體,又不想安裝一整個的 Gnome 桌面,那麼還有一個更簡單的方式。在之前的腳本裡已經安裝了基本的 X 視窗套件 (xserver-xorg-core 與 xinit),因此只要再安裝像 FVWM 這類比較簡捷的視窗管理程式:

$ sudo apt-get install fvwm

然後,參考 https://wiki.debian.org/ 上的 XorgXinitrc 兩個詞目的說明,執行 Xorg -configure 指令建立 Xorg 的設定檔 /etc/X11/xorg.conf,接著在使用者的家目錄裡建立 ~/.xinitrc 這個檔案,內容如下:

# ~/.xinitrc
# This file is sourced when running startx and 
#    other programs which call xinit
# As in all Bash/sh scripts lines starting with 
#    '#' are comments

# Set background to your favorite pic
xsetbg -fullscreen /morphix/background.png &

# Start a XTerm
/usr/bin/X11/xterm &

# Start the systems window manager.
# See WindowManagers for other choices.
exec /etc/alternatives/x-window-manager

# To run Gnome instead of system default, place a 
# '#' before the previous line and uncomment the 
# next line.
# exec gnome-session

這麼一來,使用者可以在文字介面登入後,執行 startx 指令進入 X 視窗。就像下面這張圖一樣。

註:我實際在 KVM 及 VMware 下安裝 Debian GNU/Linux 與 FVWM,似乎可以不用理會 /etc/X11/xorg.conf 這個設定檔。

延伸閱讀:

  1. 在 Debian 上使用 ufw 設定防火牆
  2. 製作 Debian 的 USB 開機碟
  3. 升級至 Linux Mint 15 (Olivia)
  4. 在 Debian 使用 KVM / libvirt 的虛擬化系統
  5. 在 Linux Mint 14 安裝 Oracle JDK 6
  6. 在 OpenSUSE 12.2 安裝 Oracle JDK 6
  7. 如何安裝 Linux Mint 14
  8. 在 Debian GNU/Linux 安裝 Gnome
  9. 安裝 Debian GNU/Linux
  10. 在 Ubuntu 12.04 安裝 JDK 6 (最終版)
  11. 在 Ubuntu 12.04 安裝 JDK 6
  12. 讓 Ubuntu 12.04 的 ibus 倉頡五代輸入法預設使用繁體語系
  13. 在 OpenSUSE 12.1 安裝 Sun JDK 6
  14. Debian GNU/Linux 安裝記錄
  15. Ubuntu 10.10 安裝記錄
  16. Ubuntu 10.04 LTS 安裝記錄
  17. 在 Ubuntu 10.04 裡安裝 Sun JDK
  18. Ubuntu 9.10 安裝記錄
  19. Samba on Ubuntu 9.04
  20. Debian (testing) on VMware Workstation 6.0.5
  21. Debian 4.0 安裝記錄
  22. Ubuntu Linux 安裝筆記

張貼留言