arch linux 安装指南

别来无恙 提交于 2020-05-08 04:57:24

(如果不想折腾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 快捷键。可以使用 nanovivim 编辑配置文件。

键盘布局

控制台键盘布局 默认为us(美式键盘映射)。如果您正在使用非美式键盘布局,通过以下的命令选择相应的键盘映射表:

# loadkeys layout

layout 转换为您的键盘布局,如frukdvorakbe-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 (简体中文).

参阅 systemd-timesyncd

建立硬盘分区

先使用 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.genlocale.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 并提交您的本地化选项:

Tip: 将系统 locale 设置为 en_US.UTF-8,系统的 Log 就会用英文显示,这样更容易问题的判断和处理。用户可以设置自己的 locale,详情参阅 Locale#Per user [broken link: invalid section].
# echo LANG=en_US.UTF-8 > /etc/locale.conf
警告: 不推荐在此设置任何中文locale,或导致tty乱码。

另外,如果你需要修改键盘布局[broken link: invalid section], 并想让这个设置持续生效,编辑 vconsole.conf(5),例如:

/etc/vconsole.conf
KEYMAP=de-latin1

主机名

要设置 hostname,将其添加/etc/hostname, myhostname 是需要的主机名:

# echo myhostname > /etc/hostname

建议添加对应的信息hosts(5):

/etc/hosts
127.0.0.1	localhost
::1		localhost
127.0.1.1	myhostname.localdomain	myhostname

网络配置

对新安装的系统,需要再次设置网络。具体请参考 Network configuration (简体中文)

对于 无线网络配置安装 软件包 iw, wpa_supplicantdialog 以及需要的 固件软件包.

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)、窗口、工具栏、文件夹、壁纸、应用程序和拖放等功能。使用GNOMEKDELXDEXfce这类桌面环境,是最简单的配置方法. 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.

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!