(如果不想折騰arch linux,推薦直接使用 manjaro: https://manjaro.org/ )
1、安裝准備
Arch Linux 能在任何內存空間不小於 512MB 的 x86_64 兼容機上運行。用 base 組內的軟件包進行的基本安裝將占用小於 800MB 的存儲空間。由於安裝過程中需要從遠程存儲庫獲取軟件包,機器將需要一個有效的互聯網連接。
根據 Category:Getting and installing Arch 中所述,下載並引導安裝介質。啟動完成后將會自動以 root 身份登錄虛擬控制台並進入zsh命令提示符。類似 systemctl(1) 的常規命令都可以用 Tab
自動補全。
如果你想切換至其它的虛擬終端來干點別的事, 例如使用 ELinks 來查看本篇指南,使用 Alt+arrow
快捷鍵。可以使用 nano,vi 或 vim 編輯配置文件。
鍵盤布局
控制台鍵盤布局 默認為us
(美式鍵盤映射)。如果您正在使用非美式鍵盤布局,通過以下的命令選擇相應的鍵盤映射表:
# loadkeys layout
將 layout 轉換為您的鍵盤布局,如fr
,uk
,dvorak
或be-latin1
。這里有國家的二位字母編碼表。使用命令 ls /usr/share/kbd/keymaps/**/*.map.gz
列出所有可用的鍵盤布局。
Console fonts 位於 /usr/share/kbd/consolefonts/
, 設置方式請參考 setfont(8).
驗證啟動模式
如果以在 UEFI 主板上啟用 UEFI 模式, Archiso 將會使用 systemd-boot 來啟動 Arch Linux。可以列出 efivars 目錄以驗證啟動模式:
# ls /sys/firmware/efi/efivars
如果目錄不存在,系統可能以 BIOS 或 CSM 模式啟動,詳見您的主板手冊。
連接到因特網
守護進程 dhcpcd 已被默認啟用來探測有線設備, 並會嘗試連接。如需驗證網絡是否正常, 可以使用 ping:
# ping -c 3 archlinux.org
若發現網絡不通,利用 systemctl stop dhcpcd@
,TAB
停用 dhcpcd 進程,詳情請查看 網絡配置文檔.
對於無線連接,iw(8), wpa_supplicant(8) 和 netctl 等工具已被提供. 詳情查看無線網絡配置.
更新系統時間
用 systemd-timesyncd 確保系統時間是正確的:
# timedatectl set-ntp true
用 timedatectl status
檢查服務狀態.詳情閱讀 Time (簡體中文).
建立硬盤分區
先使用 lsblk 查看硬盤設備。
然后使用 gdisk 來進行分區,從前到后依次分為: boot, swap, /, home 四個分區, 分別為行如 sda1, sda2, sda3, sda4。
詳細使用方式:
# gdisk /dev/sda
enter n
and accept the default for partition number and first sector. This will set you up with your second partition and start it right after the first.
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-125829086, default = 1050624) or {+-}size{KMGTP}:
For the last sector, enter +1M
to create a 1 Megabyte partition.
Last sector (1050624-125829086, default = 125829086) or {+-}size{KMGTP}: +1M
For Hex code or GUID, enter EF02
to create a BIOS partition for GRUB.
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): EF02
Changed type of partition to 'BIOS boot partition'
Root Partition
Enter n
to create a new partition. And accept all defaults to create a ‘Linux filesystem’ partition at the end of the partition table that uses the remaining available space.
Command (? for help): n
Partition number (3-128, default 3):
First sector (34-125829086, default = 1052672) or {+-}size{KMGTP}:
Last sector (1052672-125829086, default = 125829086) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Enter p
to see the partition table。
Finally, enter w
to write the results, and then q
to exit gdisk.
格式化分區
當分區配置好了, 這些分區應立即被格式化並使用一個合適的文件系統. 例如,如果你想將/dev/sda1
格式化成ext4
, 使用這個命令:
# mkfs.ext4 /dev/sdaX
其中X 分別為1 2 3 4。詳情參見 文件系統 和 swap (簡體中文)。
掛載分區
首先將根分區掛載[broken link: invalid section]到 /mnt
,例如:
# mount /dev/sda3 /mnt
如果使用多個分區,還需要為其他分區創建目錄並掛載它們(/mnt/boot
、/mnt/home
、……)。
# mkdir /mnt/boot # mount /dev/sda1 /mnt/boot
# mkdir /mnt/home # mount /dev/sda4 /mnt/home
genfstab 將會自動檢測掛載的文件系統和 swap 分區。
交換分區
建立交換分區,分別輸入如下命令:
# mkswap /dev/sda2
# swapon /dev/sda2
2、安裝
選擇鏡像
編輯 /etc/pacman.d/mirrorlist
,選擇您的首選 mirror. 這個 mirror 列表也將通過 pacstrap
被復制並保存在到系統中,所以請確保設置正確。
中國區用戶推薦 163 或者 uestc 的鏡像源。
安裝基本系統
如果您想通過 AUR (簡體中文) 或者 ABS (簡體中文) 編譯安裝軟件包,需要裝上 base-devel:
# pacstrap /mnt base base-devel
使用 -i
選項時會在實際安裝前進行確認。此章節會給您安裝好最基本的 Arch 系統,其它軟件以后會用 pacman (簡體中文) 安裝得到。第一個 initramfs 會在新系統的啟動路徑生成和安裝,請確保 ==> Image creation successful
.
3、配置系統
Fstab
用以下命令生成 fstab 文件 (用 -U
或 -L
選項設置UUID 或卷標):
# genfstab -U /mnt >> /mnt/etc/fstab
強烈建議 在執行完以上命令后,后檢查一下生成的 /mnt/etc/fstab
文件是否正確。
安裝引導程序
There are several commonly used boot loaders out there. Most people use either GRUB or Syslinux. You can read up on the selection, but for a relatively basic setup like the one I’ve been describing here, it matters relatively little which you choose. I use Syslinux, and so that’s the boot loader I’ll discuss here, though you can find instructions for installing GRUB on the Arch Wiki. So, Syslinux…
Just like we installed the rest of our system onto our virtual PC’s hard drive, we can now do the same with Syslinux using the following command:
# pacstrap /mnt syslinux gptfdisk
The first part of this command installs the Syslinux software onto the virtual PC, though we’ll still need to configure it before it will do its job. The second part of the command installs another program, gptfdisk, that will be needed to perform this configuration.
As mentioned, we’ve still got some work left to do before we’ve finished installing the boot loader, but we’ve now completed the first step.
Chroot
Change root 到新安裝的系統:
# arch-chroot /mnt
時區
設置 時區:
# ln -sf /usr/share/zoneinfo/zone/subzone /etc/localtime
例如:
# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
建議設置時間標准 為 UTC,並調整 時間漂移,輸入如下命令:
# hwclock --systohc --utc
Locale
本地化的程序與庫若要本地化文本,都依賴 Locale, 后者明確規定地域、貨幣、時區日期的格式、字符排列方式和其他本地化標准等等。在下面兩個文件設置:locale.gen
與 locale.conf
.
/etc/locale.gen
是一個僅包含注釋文檔的文本文件。指定您需要的本地化類型,只需移除對應行前面的注釋符號(#
)即可,建議選擇帶UTF-8
的項:
# nano /etc/locale.gen
en_US.UTF-8 UTF-8 zh_CN.UTF-8 UTF-8 zh_TW.UTF-8 UTF-8
接着執行locale-gen
以生成locale訊息:
# locale-gen
/etc/locale.gen
生成指定的本地化文件,每次 glibc 更新之后也會運行 locale-gen
。
創建 locale.conf
並提交您的本地化選項:
en_US.UTF-8
,系統的 Log 就會用英文顯示,這樣更容易問題的判斷和處理。用戶可以設置自己的 locale,詳情參閱
Locale#Per user
[broken link: invalid section].
# echo LANG=en_US.UTF-8 > /etc/locale.conf
另外,如果你需要修改鍵盤布局[broken link: invalid section], 並想讓這個設置持續生效,編輯 vconsole.conf(5),例如:
/etc/vconsole.conf
KEYMAP=de-latin1
主機名
要設置 hostname,將其添加 到 /etc/hostname
, myhostname 是需要的主機名:
# echo myhostname > /etc/hostname
/etc/hosts
127.0.0.1 localhost ::1 localhost 127.0.1.1 myhostname.localdomain myhostname
網絡配置
對新安裝的系統,需要再次設置網絡。具體請參考 Network configuration (簡體中文) 和
對於 無線網絡配置,安裝 軟件包 iw, wpa_supplicant,dialog 以及需要的 固件軟件包.
Initramfs
如果修改了 mkinitcpio.conf,用以下命令創建一個初始 RAM disk:
# mkinitcpio -p linux
Root 密碼
設置 root 密碼:
# passwd
Finishing the Boot Loader Installation
Now, while we’re still logged into our hard drive’s Arch Linux installation, let’s configure the boot loader. Start by running the following command, which executes a configuration script that will (mostly) get Syslinux ready to go. The script depends on you having installed not only Syslinux, but gptfdisk above.
# syslinux-install_update -i -a -m
cd cdcdWhat’s this script doing? It’s a script specific to Arch Linux that actually takes the place of a number of separate configuration scripts and commands that you’d otherwise need to run to get a working boot loader. The options in the command give an idea a to what’s going on here. According to the Arch Wiki, the ‘-i’ option means “install the files”—put all the support files in the right place on your boot partition, so that Syslinux can find them. The ‘-a’ option tells the system that the partition to which Syslinux is installed is the active boot partition—in other words it does something called “set the boot flag,” which means to basically tell the computer’s lizard brain which partition to boot when it starts up. And ‘-m’ means “install the master boot record boot code,” which is a fancy way of saying that it’s going to place some instructions for how to booting up the machine properly at the very beginning of our hard disk, so it’s the first thing that the machine’s hardware sees when it boots.
Once this script has run, navigate to the /boot/syslinux directory…
# cd /boot/syslinux
…then run the following commands to replace some of the files that the configuration script put in this directory that aren’t quite right for our system:
# cp /usr/lib/syslinux/bios/menu.c32 .
# cp /usr/lib/syslinux/bios/vesamenu.c32 .
# cp /usr/lib/syslinux/bios/chain.c32 .
# cp /usr/lib/syslinux/bios/hdt.c32 .
# cp /usr/lib/syslinux/bios/reboot.c32 .
# cp /usr/lib/syslinux/bios/poweroff.c32 .
# cp /usr/lib/syslinux/bios/libutil.c32 .
# cp /usr/lib/syslinux/bios/libcom32.c32 .
Next, we’ll run another handy script called ‘mkinitcpio’, which is going to set up the initial ramdisk (memory) environment for our virtual PC. This serves as the computer’s memory when it first boots up, before it finds the rest of its brain. If you want to customize this environment, you can read up on mkinitcpio and modify ‘/etc/mkinitcpio.conf’. But if you’re not doing anything fancy, you can skip making any modifications and simply run the command:
# mkinitcpio -p linux
In English, this translates to “Make an initial ramdisk environment (mkinitcpio) using presets (-p) suitable for Linux”.
We’re now very nearly finished installing Arch Linux. There are still a couple loose ends to be tied up, which will be covered in the next step.
Step Thirteen: Finishing Up the Installation
We’ve got a couple loose ends to tie up as we back out of the system we’ve just installed and prepare to reboot our virtual PC. First, now that we’re preparing to boot into a fully functional system for the first time, we’d like a bit of security. It’d be best if, in the future, no one could log in and make the sorts of changes we’ve just made without first entering a password. To set a password for your system’s “root” user—the all powerful super user—enter the following command:
# passwd
You’ll be prompted to enter and re-enter a password. Make sure to remember it, as you’ll need it in both the near and foreseeable future.
We’re now done making changes from within our virtual PC’s Arch Linux installation. The next step will be to transport back to working from the Arch Linux Install CD. To do so, type the command:
# exit
What you want is to see the original colorful command prompt beginning with ‘root@archiso’ that you first saw when you booted Arch from the CD, and absent the square brackets we’ve been seeing as part of the prompt since we first entered the ‘arch-chroot’ command. When you’re back to this prompt, it will indicate that you’re once again working from the Arch Linux installation CD, rather the copy of Arch on your hard disk.
Once you’ve returned to the CD’s command prompt, it’s time to unmount all the partitions we mounted earlier. If you’re using the partitioning scheme I’ve been demonstrating, the commands will be as follows (you can alter these as needed for your own partition scheme).
First, change to the top-level directory of the installation CD environment, so that you’re not browsing in any of the folders that are linked to the partitions you’re trying to unmount:
# cd /
Next, go ahead and unmount the partitions. Note that you should unmount the root directory, mounted at /mnt, last since other partitions are mounted within it:
# umount /mnt/boot
# umount /mnt/home
# swapoff /dev/sda2
# umount /mnt
Once this has been done, we’ve only one last command to run before rebooting into our new system. Before rebooting, run this command from the installer disk’s command prompt, which will set the /boot partition’s BIOS flag to “bootable.” I believe that the Syslinux install script above may actually have done this already, but it’s a pretty vital step, so it can’t hurt to do it again and make assurance doubly sure. To reiterate what’s going on with this, we basically want to tell the computer’s lizard brain which partition to boot—and allow your new system to boot on restart:
# sgdisk /dev/sda --attributes=1:set:2
For context, sgdisk is a disk utility program similar to gdisk, which we used earlier, but which can take instructions directly from the command line, rather than by entering into an interactive mode (i.e., engaging in a Q&A with the user). The command, in English reads “Using sgdisk, examine the device found at /dev/sda (which is our hard drive). Give partition 1 the attribute of being bootable (‘2’ means ‘legacy BIOS bootable’).” The reason we didn’t simply do this earlier in gdisk is that gdisk doesn’t appear to offer this option—probably because under GPT, we’re not supposed to have to set a boot flag (one of its advantages). Unfortunately, not all hardware is smart enough to realize this, so we have to set it anyhow.
Rebooting Into Your New Arch Linux Machine
And that’s it! Your virtual PC is now completely set up and Arch Linux has been installed and configured. It’s time to reboot into your new system! To do so, just enter the command:
# reboot
Some Housecleaning that Will Make the System More Usable
First, when we booted from the CD, Arch automatically connected to the internet for us in anticipation of our needing it to install the system. But this time around, we have to tell the system to connect to the internet. We’ll automate this momentarily, but for now, to connect to the internet run the command:
# dhcpcd
One beautiful thing about using virtualization software like VirtualBox is that it saves us having to configure wireless internet from the command line, which is never very fun. Our virtual computer drafts off our real computer’s internet connection (wireless or otherwise), while thinking its connected to the ‘net via an ethernet cable.
Next, it’s more secure and more convenient to create yourself a user account, rather than continuing to use your Linux install as ‘root’. To create a user account for yourself, run the following command, replacing both instances of ‘josh’ with whatever user name you’d like:
# useradd
--
home-dir /home/josh--
create-home josh
Note that the username you enter here should be a simple login ID, not your full name; later on you can associate this ID with your full name and other personal information. As for what the command does, it obviously creates the user account ‘josh’ (or whatever user ID you’ve chosen). Aside from that, the command we’ve entered has a couple extra flags that add bells and whistles. The flag ‘–create-home’ tells the system that you’re creating a home directory for the user, and the ‘–home-dir’ flag tells the system the path to that directory ( ‘/home/josh’).
Next give your new user account its own password with the following command (again, replacing ‘josh’ with whatever user ID you created for yourself):
# passwd josh
This command is largely self-explanatory. It’s the same command we ran above to give a password to the root account, but this time we’re specifying that we want to create a password for a particular account apart from root (i.e., ‘josh’).
Though you now have a new user account for yourself that’s separate from ‘root’, you’ll occasionally want to perform administrative tasks without having to log out of your personal account and log back in as the root user. To do this, you can use the ‘sudo’ command, which means “perform my command as the root user.”
Before the ‘sudo’ command will do what we want, though, we need to tell it that your new user account is one that should have access to administrative privileges. To do this, we’ll need to edit a configuration file called ‘sudoers’, located in the /etc directory. Again, since this configuration file is just a text file, we’ll use nano to edit it:
# nano /etc/sudoers
Once the file opens, find the lines reading:
##
## User privilege specification
##
root ALL=(ALL) ALL
Below the line beginning with ‘root’ shown above, add an identical line, but this time replacing ‘root’ with the new username you just created. For example, if I created the login ID ‘josh’ above, the new line I would add would look like this:
josh ALL=(ALL) ALL
Once this line has been added, exit to the command prompt by typing Control-X, answering ‘y’ for ‘yes’ when you’re asked whether you’d like to save your changes, and then hitting ‘Return’ to confirm the suggested filename.
Once back on the command line, enter the command…
# exit
…to log out of the root user account and log back in with your new user ID and password.
Next, from here on out, we’d also like Arch Linux to automatically start an internet connection on login, so we don’t have to keep typing ‘dhcpcd’ or ‘sudo dhcpcd’ to get online. To automate this process, we’ll need to know the name that Arch is using to identify our virtual PC’s ethernet card. To find this, enter the following command:
$ ip link show
(Note that in this command I denote the prompt with a dollar sign ($) instead of a pound sign (#)—this is a common way of denoting that the person running the command is not expected to be logged in as the root user.)
The above command will spit out a list of network devices on your virtual PC. Briefly, ‘ip’ is a command for working with all sorts of stuff to do with networking on your computer. The option ‘link’ specifies that the thing you want to manipulate with the ‘ip’ command is a network interface (a ‘link’). And ‘show’ means “show me a list of my network interfaces.”
The thing you want from this output is the name of your network card, which will be a single word begins with ‘enp’. Copy down this word in its entirety, as you’ll use it again in the next command we give to the system.
To have our system automatically connect to the internet enter this command, replacing ‘enp0s3’ with whatever the name was you copied down in the previous instruction (provide your password when prompted):
$ sudo systemctl enable dhcpcd@enp0s3.service
Some context for the above command: As mentioned previously, ‘sudo’ is used to execute an administrative task without using the root account. ‘Systemctl’ is a command that works with Arch Linux’s default “service manager,” which is called “systemd.” Systemd is the tool that’s behind the scenes with regard to much of our system’s configuration. With the ‘systemctl’ command, we’re telling systemd to enable an internet connection for us automatically using the ‘dhcpcd’ command via our ‘eth0’ device, which is the virtual PC’s wired ethernet card.
Now, in the future, whenever your virtual machine boots, it will automatically connect to the internet.
Installing X and Guest Additions
X 窗口管理系統(X11或者X) 是基於網絡的顯示協議,提供了窗口功能,包含建立圖形用戶界面(GUI)的標准工具和協議。Xorg是X窗口系統11版本的開源實現,提供圖形用戶界面, 安裝和配置請閱讀Xorg。
Xorg只提供圖形環境的基本框架,完整的用戶體驗還需要其他組件。 桌面環境(DE): 在X之上並與其共同運作,提供完整的功能和動態圖形界面。桌面環境通常提供圖標、小程序(applets)、窗口、工具欄、文件夾、壁紙、應用程序和拖放等功能。使用GNOME、KDE、LXDE、Xfce這類桌面環境,是最簡單的配置方法. Category:Desktop environments 包含了各種桌面環境。
Arch Linux should be working wonderfully at this point. But chances are, you don’t want to live your entire computing life on the command line. You’d like a slick GUI desktop environment, with beautiful icons, where you can your favorite programs and—oh, I don’t know—use a mouse. You’d probably also like it if you could use your new Arch Linux installation in full screen mode, such that it was more like working with your native OS. All this is possible, of course, and we’ll get to it now.
To begin with, we need to install a software suite called “X”, which is a “window system”—software that supports draggable, droppable windows and the like—that serves as the underpinning of most of Linux’s GUI desktop environments. We’ll start by installing the basic X packages.
Arch Linux has a handy package manager, called ‘pacman’ that automates the process of installing, uninstalling, and updating software on your computer. You’ll be using it frequently in the future. The command ‘pacman -S somesoftware’ tells pacman to install ‘somesoftware’ on your machine. We can use ‘pacman -S’ to install all the software we’re looking to add (note that we need to prefix the command with ‘sudo’, since installing software is an administrative task):
$ sudo pacman -S xorg-server xorg-xinit xorg-apps
Provide your password if asked, and follow the prompts pacman provides to install the software. Next, I used to recommend installing another package, called ‘mesa’, which helps with the slick 3D graphics common in newer desktop environments. However, ‘mesa’ now gets installed automatically as part of the above step, so there’s no need to install it separately.
Normally at this stage, we’d install the hardware drivers for our computer’s graphics card, but since our PC is virtual we’re instead going to install VirtualBox’s “Guest Additions”—a collection of software packages that will enable our virtual machine to take advantage of full screen graphics and hardware acceleration. The Guest Additions also more generally enable our Arch Linux guest OS to integrate more fully with the native host OS running VirtualBox.
To install guest additions, run the following command:
$ sudo pacman -S virtualbox-guest-modules-arch virtualbox-guest-utils
At this point you’re likely getting used to working with both sudo and pacman, so I’ll stop with the spiel about entering your password if asked and following the prompts.
Once Guest Additions are installed, we need to make sure that the various components of this software are loaded automatically each time the system boots. To do this, we’ll use nano to create a new configuration file called ‘virtualbox.conf’. This file will go in the ‘/etc/modules-load.d’ directory, which contains files that need to be loaded when Arch boots up. Since adding a file to this directory requires administrative permissions, we’ll need to precede our command with ‘sudo’ once again:
$ sudo nano /etc/modules-load.d/virtualbox.conf
When nano brings up the blank file, add these three lines to it:
vboxguest
vboxsf
vboxvideo
As usual, when you’re finished entering text in nano, type Control-X to exit to the command line, and answer ‘y’ for yes when you’re asked whether you want to save your work, and then hit ‘Return’ to accept the filename.
At this point, reboot your machine for the Guest Additions to take effect.
$ sudo reboot
Upon rebooting, your system should automatically connect to the internet, and Guest Additions should load up as part of the boot process.
By installing Guest Additions, we’re working toward better integration between our host and guest OSes. One example is that we can sync our Arch Linux install’s clock with that of the host system by entering the following command. This will enable synching on the next boot:
$ sudo systemctl enable vboxservice.service
But enough with the clock. At this point, we want to see if X is working, since we need it to be functional before we can install and use a GUI desktop.
To this end, we’ll install a few software packages that will help us test X:
$ sudo pacman -S xorg-twm xorg-xclock xterm
After these software packages have been installed, we can test that Xorg is installed correctly by running the command:
# startx
This will launch a test environment that relies on X. At this point, a few things should happen. Your guest OS screen should resize, your mouse should work, and you should see some draggable windows. This all means that both Guest Additions and X are working properly.
Note that the each of the draggable windows in the test environment should include a command prompt. At one of these prompts, type ‘exit’ to quit out of the test environment and back to the ordinary command line interface.