虚拟机安装Arch Linux

2022/6/27 5:20:16

本文主要是介绍虚拟机安装Arch Linux,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本次介绍在Hyper-V虚拟机上安装Arch Linux的方法,其它虚拟机平台类似。

1. 创建和启动虚拟机

1.1 下载ArchLinux安装镜像

先登录官网下载页面:[https://archlinux.org/download/].

然后根据喜好选择具体的下载链接,比如我选择通过国内网易163下载镜像文件:

[http://mirrors.163.com/archlinux/iso/2022.06.01/archlinux-2022.06.01-x86_64.iso].

 

1.2 创建虚拟机

 

 

新建一个虚拟机,选择Generation 2的UEFI方式启动,接着设置内存2048M,存储50G,配置网卡(安装过程需要联网下载文件),Installation Options选项中选择加载步骤1.1中下载的ISO文件。

点击完成后,就创建了一个虚拟机,选中虚拟机,鼠标右键选择"Settings -> Security",禁用"Enable Secure Boot"功能,保存,启动虚拟机。

 

 

 

1.3 测试网络

启动完成之后,就进入了控制台界面,使用ping命令测试网络是否正常。

$ ping www.baidu.com -c 4

PING www.wshifen.com (45.113.192.102) 56(84) bytes of data.
64 bytes from 45.113.192.102 (45.113.192.102): icmp_seq=1 ttl=53 time=131 ms
64 bytes from 45.113.192.102 (45.113.192.102): icmp_seq=2 ttl=53 time=94.0 ms
64 bytes from 45.113.192.102 (45.113.192.102): icmp_seq=3 ttl=53 time=91.1 ms
64 bytes from 45.113.192.102 (45.113.192.102): icmp_seq=4 ttl=53 time=93.1 ms

--- www.wshifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 91.123/102.347/131.195/16.687 ms

 

2. 分区

2.1 查看分区

$ fdisk -l
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/loop0: 688.21 MiB, 721641472 bytes, 1409456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

 

2.2 创建分区

50G的存储空间,这里分为3个部分,如下表:

No. Area Caps Fromat
1 boot 512M FAT32
2 swap 4096M -
3 / 45.5G EXT4
## 分区.
$ fdisk /dev/sda

Welcome to fdisk (util-linux 2.38).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x9842a669.

## 查看帮助.
$ Command (m for help): m

Help:

  GPT
   M   enter protective/hybrid MBR

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

## 创建GPT分区表.
$ Command (m for help): g

Created a new GPT disklabel (GUID: D28F168D-BC76-E244-B6AC-FE77F9960EA1).

## 增加第1个新分区.
$ Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-104857566, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-104857566, default 104855551): +512M

Created a new partition 1 of type 'Linux filesystem' and of size 512 MiB.

## 增加第2个新分区.
$ Command (m for help): n
Partition number (2-128, default 2): 2
First sector (1050624-104857566, default 1050624):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-104857566, default 104855551): +4096M

Created a new partition 2 of type 'Linux filesystem' and of size 4 GiB.

## 增加第3个新分区.
$ Command (m for help): n
Partition number (3-128, default 3): 3
First sector (9439232-104857566, default 9439232):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (9439232-104857566, default 104855551):

Created a new partition 3 of type 'Linux filesystem' and of size 45.5 GiB.

## 改变第一个分区类型为EFI.
$ Command (m for help): t
Partition number (1-3, default 3): 1
Partition type or alias (type L to list all): 1

Changed type of partition 'Linux filesystem' to 'EFI System'.

## 查看分区情况.
$ Command (m for help): p

Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: D28F168D-BC76-E244-B6AC-FE77F9960EA1

Device       Start       End  Sectors  Size Type
/dev/sda1     2048   1050623  1048576  512M EFI System
/dev/sda2  1050624   9439231  8388608    4G Linux filesystem
/dev/sda3  9439232 104855551 95416320 45.5G Linux filesystem

## 写入分区表并退出.
$ Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

 

3. 格式化和挂载

3.1 格式化

## /dev/sda1格式化为FAT32格式.
$ mkfs.fat -F32 /dev/sda1
mkfs.fat 4.2 (2021-01-31)

## /dev/sda2格式化为SWAP.
$ mkswap /dev/sda2
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=ac0de06e-33cd-4533-b8f3-31268a41c606

## /dev/sda3格式化为EXT4格式.
$ mkfs.ext4 /dev/sda3
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done
Creating filesystem with 11927040 4k blocks and 2981888 inodes
Filesystem UUID: de318dc5-0d09-459c-9a59-2904219d58d2
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done

 

3.2 挂载硬盘

$ mount /dev/sda3 /mnt
$ mkdir /mnt/boot
$ mount /dev/sd1 /mnt/boot
$ swapon /dev/sda2
$ lsblk -a

 

4. 安装系统

4.1 添加国内源

$ vim /etc/pacman.d/mirrorlist

在原有的Server基础上,增加两个:

Server = http://mirrors.163.com/archlinux/$repo/os/$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch

保存退出,更新源:

$ pacman -Syy
:: Synchronizing package databases...
 core                           156.4 KiB  23.1 KiB/s 00:07 [################################] 100%
 extra                         1717.5 KiB   198 KiB/s 00:09 [################################] 100%
 community                        6.7 MiB   226 KiB/s 00:30 [################################] 100%

 

4.2 安装系统

$ pacstrap /mnt base linux linux-firmware linux-headers base-devel

根据网速的不同,此过程需要一段不等的时间,我安装的这个版本需要下载558.64 MiB,如果中途出现断网等导致下载的情况,重新执行命令即可.

 

4.3 生成fstab

$ genfstab -U /mnt >> /mnt/etc/fstab

 

5. 系统配置

5.1 进入系统

$ arch-chroot /mnt

 

5.2 增加国内源

$ vim /etc/pacman.d/mirrorlist

增加两个国内源,其它源根据喜好自行添加:

Server = http://mirrors.163.com/archlinux/$repo/os/$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch

 

5.3 网络配置

安装网络工具:

$ pacman -S iw wpa_supplicant dialog net-tools networkmanager dhcpcd

安装完成后,进行配置网络:

$ systemctl enable NetworkManager
$ systemctl enable dhcpcd

 

5.4 用户配置

设置root密码:

$ passwd
New password: 
Retype new password: 
passwd: password updated successfully

添加新用户,示例添加一个名字叫test的新用户:

$ useradd -d /home/test -g root -m -s /bin/bash test
$ passed test
New password: 
Retype new password: 
passwd: password updated successfully

安装sudo:

$ pacman -S sudo vim
$ visudo

执行上述命令后,会自动打开/etc/sudoers,定位并编辑如下内容:

##
## User privilege specification
##
root ALL=(ALL:ALL) ALL

## 增加新建的test用户
test ALL=(ALL:ALL) ALL

## Uncomment to allow members of group wheel to execute any command
## 去掉注释
%wheel ALL=(ALL:ALL) ALL

 

5.5 安装Grub和efibootmgr

$ pacman -S grub efibootmgr
$ grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=ArchLinuxGrub
$ grub-mkconfig -o /boot/grub/grub.cfg

 

5.6 安装基础开发工具

$ pacman -S gcc make bc flex bison git openssh axel unzip bzip2 texinfo binutils patch python3

激活sshd:

$ systemctl enable sshd

 

5.7 完成安装

[root@archiso /]# exit
$ umount -R /mnt
$ poweroff

 

6. 启动系统

在Hyper-V此虚拟机中设置,移除安装镜像*.iso, CPU核心数默认是1个,可以适当增加,之后,启动虚拟机.

 

 启动后的效果:

 



这篇关于虚拟机安装Arch Linux的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程