D.3. 透過 Unix/Linux 系統來安裝 Debian GNU/Linux

本節說明了如何透過現有的 Unix 或 Linux 系統來安裝 Debian GNU/Linux,而非使用本手冊後面要談到的基於 ncurses,由選單驅動的安裝軟體。這篇交叉安裝(cross-install) HOWTO 是應使用者的要求而撰寫的。他們原來是 Redhat、Mandriva 和 SUSE 的使用者,現轉而使用 Debian GNU/Linux。在本節中,我們假設您對於輸入 *nix 指令以及在檔案系統中穿梭移動並不生疏。同時,在本節中,$ 符號表示這個指令是在使用者當前使用的系統中輸入的,而 # 則表示這個指令是在 Debian chroot 環境中輸入的。

您一旦根據自己的需求設定好了新的 Debian 系統,就可以將您的原來系統中的使用者資料(如果有的話)全部遷移到 Debian 裡去。然後就能繼續爽了。一言以蔽之,這是一次不關機的 Debian GNU/Linux 安裝。同時,這也是處理那些因為硬體原因不能透過任何開機或者安裝媒介完成安裝的權宜之計。

[注意] 注意

As this is a mostly manual procedure, you should bear in mind that you will need to do a lot of basic configuration of the system yourself, which will also require more knowledge of Debian and of Linux in general than performing a regular installation. You cannot expect this procedure to result in a system that is identical to a system from a regular installation. You should also keep in mind that this procedure only gives the basic steps to set up a system. Additional installation and/or configuration steps may be needed.

D.3.1. 準備開始

With your current *nix partitioning tools, repartition the hard drive as needed, creating at least one filesystem plus swap. You need around 1012MB of space available for a console only install, or about 2539MB if you plan to install X (more if you intend to install desktop environments like GNOME or KDE Plasma).

在您的分割區上建立檔案系統。舉例來說,如果要在 /dev/sda6 分割區(以後的例子中,將把它作為 root 分割區) 建立一個 ext3 格式的檔案系統:

# mke2fs -j /dev/sda6

如果想建立的是 ext2 格式的檔案系統,就要去掉 -j

初始化並啟用置換分割區(請把分割區編號替換成您希望用作置換分割區的分割區編號):

# mkswap /dev/sda5
# sync
# swapon /dev/sda5

把一個分割區掛載(mount)到 /mnt/debinst(這是安裝的位置,將來作為您新系統的根(/)檔案系統)。掛載點的名稱是任意的,後面的步驟將會用到它。

# mkdir /mnt/debinst
# mount /dev/sda6 /mnt/debinst

[注意] 注意

If you want to have parts of the filesystem (e.g. /usr) mounted on separate partitions, you will need to create and mount these directories manually before proceeding with the next stage.

D.3.2. 安裝 debootstrap

The utility used by the Debian installer, and recognized as the official way to install a Debian base system, is debootstrap. It uses wget and ar, but otherwise depends only on /bin/sh and basic Unix/Linux tools[22]. Install wget and ar if they aren't already on your current system, then download and install debootstrap.

Or, you can use the following procedure to install it manually. Make a work folder for extracting the .deb into:

# mkdir work
# cd work

The debootstrap binary is located in the Debian archive (be sure to select the proper file for your architecture). Download the debootstrap .deb from the pool, copy the package to the work folder, and extract the files from it. You will need to have root privileges to install the files.

# ar -x debootstrap_0.X.X_all.deb
# cd /
# zcat /full-path-to-work/work/data.tar.gz | tar xv

D.3.3. 執行 debootstrap

debootstrap can download the needed files directly from the archive when you run it. You can substitute any Debian archive mirror for http.us.debian.org/debian in the command example below, preferably a mirror close to you network-wise. Mirrors are listed at http://www.debian.org/mirror/list.

If you have a trixie Debian GNU/Linux installation image mounted at /cdrom, you could substitute a file URL instead of the http URL: file:/cdrom/debian/

Substitute one of the following for ARCH in the debootstrap command: amd64, arm64, armel, armhf, i386, mips64el, mipsel, ppc64el, s390x.

# /usr/sbin/debootstrap --arch ARCH trixie \
     /mnt/debinst http://ftp.us.debian.org/debian

If the target architecture is different than the host, you should add the --foreign option.

D.3.4. 基本系統的設定

Now you've got a real Debian system, though rather lean, on disk. chroot into it:

# LANG=C.UTF-8 chroot /mnt/debinst /bin/bash

If the target architecture is different from the host, you will need to first copy qemu-user-static to the new host:

# cp /usr/bin/qemu-ARCH-static /mnt/debinst/usr/bin
# LANG=C.UTF-8 chroot /mnt/debinst qemu-ARCH-static /bin/bash

After chrooting you may need to set the terminal definition to be compatible with the Debian base system, for example:

# export TERM=xterm-color

Depending on the value of TERM, you may have to install the ncurses-term package to get support for it.

If the target architecture is different from the host, you need to finish the multi-stage boot strap:

/debootstrap/debootstrap --second-stage

D.3.4.1. Create device files

At this point /dev/ only contains very basic device files. For the next steps of the installation additional device files may be needed. There are different ways to go about this and which method you should use depends on the host system you are using for the installation, on whether you intend to use a modular kernel or not, and on whether you intend to use dynamic (e.g. using udev) or static device files for the new system.

A few of the available options are:

  • install the makedev package, and create a default set of static device files using (after chrooting)

    # apt install makedev
    # mount none /proc -t proc
    # cd /dev
    # MAKEDEV generic
    

  • manually create only specific device files using MAKEDEV

  • bind mount /dev from your host system on top of /dev in the target system; note that the postinst scripts of some packages may try to create device files, so this option should only be used with care

D.3.4.2. 分割區的掛載

You need to create /etc/fstab.

# editor /etc/fstab

Here is a sample you can modify to suit:

# /etc/fstab: static file system information.
#
# file system    mount point   type    options                  dump pass
/dev/XXX         /             ext3    defaults                 0    1
/dev/XXX         /boot         ext3    ro,nosuid,nodev          0    2

/dev/XXX         none          swap    sw                       0    0
proc             /proc         proc    defaults                 0    0

/dev/cdrom       /media/cdrom  iso9660 noauto,ro,user,exec      0    0

/dev/XXX         /tmp          ext3    rw,nosuid,nodev          0    2
/dev/XXX         /var          ext3    rw,nosuid,nodev          0    2
/dev/XXX         /usr          ext3    rw,nodev                 0    2
/dev/XXX         /home         ext3    rw,nosuid,nodev          0    2

Use mount -a to mount all the file systems you have specified in your /etc/fstab, or, to mount file systems individually, use:

# mount /path   # e.g.: mount /usr

Current Debian systems have mountpoints for removable media under /media, but keep compatibility symlinks in /. Create these as as needed, for example:

# cd /media
# mkdir cdrom0
# ln -s cdrom0 cdrom
# cd /
# ln -s media/cdrom

You can mount the proc file system multiple times and to arbitrary locations, though /proc is customary. If you didn't use mount -a, be sure to mount proc before continuing:

# mount -t proc proc /proc

The command ls /proc should now show a non-empty directory. Should this fail, you may be able to mount proc from outside the chroot:

# mount -t proc proc /mnt/debinst/proc

D.3.4.3. Setting Timezone

Setting the third line of the file /etc/adjtime to UTC or LOCAL determines whether the system will interpret the hardware clock as being set to UTC respective local time. The following command allows you to set that.

# editor /etc/adjtime

Here is a sample:

0.0 0 0.0
0
UTC

The following command allows you to choose your timezone.

# dpkg-reconfigure tzdata

D.3.4.4. 網路的設定

To configure networking, edit /etc/network/interfaces, /etc/resolv.conf, /etc/hostname and /etc/hosts.

# editor /etc/network/interfaces

Here are some simple examples from /usr/share/doc/ifupdown/examples:

######################################################################
# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
# See the interfaces(5) manpage for information on what options are
# available.
######################################################################

# The loopback interface isn't really required any longer, but can be used
# if needed.
#
# auto lo
# iface lo inet loopback

# To use dhcp:
#
# auto eth0
# iface eth0 inet dhcp

# An example static IP setup: (network, broadcast and gateway are optional)
#
# auto eth0
# iface eth0 inet static
#     address 192.168.0.42
#     network 192.168.0.0
#     netmask 255.255.255.0
#     broadcast 192.168.0.255
#     gateway 192.168.0.1

Enter your nameserver(s) and search directives in /etc/resolv.conf:

# editor /etc/resolv.conf

A simple example /etc/resolv.conf:

search example.com
nameserver 10.1.1.36
nameserver 192.168.9.100

Enter your system's host name (2 to 63 characters):

# echo DebianHostName > /etc/hostname

And a basic /etc/hosts with IPv6 support:

127.0.0.1 localhost
127.0.1.1 DebianHostName

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

If you have multiple network cards, you should arrange the names of driver modules in the /etc/modules file into the desired order. Then during boot, each card will be associated with the interface name (eth0, eth1, etc.) that you expect.

D.3.4.5. Configure Apt

Debootstrap will have created a very basic /etc/apt/sources.list that will allow installing additional packages. However, you may want to add some additional sources, for example for source packages and security updates:

deb-src http://ftp.us.debian.org/debian trixie main

deb http://security.debian.org/ trixie-security main
deb-src http://security.debian.org/ trixie-security main

Make sure to run apt update after you have made changes to the sources list.

D.3.4.6. Configure Locales and Keyboard

To configure your locale settings to use a language other than English, install the locales support package and configure it. Currently the use of UTF-8 locales is recommended.

# apt install locales
# dpkg-reconfigure locales

To configure your keyboard (if needed):

# apt install console-setup
# dpkg-reconfigure keyboard-configuration 

Note that the keyboard cannot be set while in the chroot, but will be configured for the next reboot.

D.3.5. 核心的安裝

If you intend to boot this system, you probably want a Linux kernel and a boot loader. Identify available pre-packaged kernels with:

# apt search linux-image

Then install the kernel package of your choice using its package name.

# apt install linux-image-arch-etc

D.3.6. Boot Loader 的設定

To make your Debian GNU/Linux system bootable, set up your boot loader to load the installed kernel with your new root partition. Note that debootstrap does not install a boot loader, but you can use apt inside your Debian chroot to do so.

Check info grub for instructions on setting up the bootloader. If you are keeping the system you used to install Debian, just add an entry for the Debian install to your existing grub2 grub.cfg.

Installing and setting up grub2 is as easy as:

# apt install grub-pc
# grub-install /dev/sda
# update-grub

The second command will install grub2 (in this case in the MBR of sda). The last command will create a sane and working /boot/grub/grub.cfg.

Note that this assumes that a /dev/sda device file has been created. There are alternative methods to install grub2, but those are outside the scope of this appendix.

D.3.7. Remote access: Installing SSH and setting up access

In case you can login to the system via console, you can skip this section. If the system should be accessible via the network later on, you need to install SSH and set up access.

# apt install ssh

Root login with password is disabled by default, so setting up access can be done by setting a password and re-enable root login with password:

# passwd
# editor /etc/ssh/sshd_config

This is the option to be enabled:

PermitRootLogin yes

Access can also be set up by adding an ssh key to the root account:

# mkdir /root/.ssh
# cat << EOF > /root/.ssh/authorized_keys
ssh-rsa ....
EOF

Lastly, access can be set up by adding a non-root user and setting a password:

# adduser joe
# passwd joe

D.3.8. Finishing touches

As mentioned earlier, the installed system will be very basic. If you would like to make the system a bit more mature, there is an easy method to install all packages with standard priority:

# tasksel install standard

Of course, you can also just use apt to install packages individually.

After the installation there will be a lot of downloaded packages in /var/cache/apt/archives/. You can free up some diskspace by running:

# apt clean



[22] These include the GNU core utilities and commands like sed, grep, tar and gzip.