在thinkpad x280上安装arch linux
到手一台2018年的ThinkPad x280,装的还是win11,深感性能之捉急,决定重装为Arch办公用。记录一下安装过程。 使用工具:装了Xshell、有SSH服务的Win10设备,U盘,公用wifi。
制作Live USB
下载Ventoy,解压安装到事先准备好的8GB U盘里。然后到镜像站[https://archlinux.org/download/]下载Arch的安装映像(ISO文件),复制到Ventoy盘。
启动Arch Live环境
Arch Linux 安装映像不支持 UEFI 安全启动(Secure Boot)功能,所以需要事先禁用,否则引导进不去。插上U盘,启动x280机器,狂按F1 进入Setup界面,选择Secure-Secure Boot,改属性为disable。按F10保存退出。狂按F12 选择USB HDD boot,成功进入Arch Live 环境。
连接局域网
命令行安装时,手动输入代码相当麻烦,所以选择使用另一台windows设备SSH上去,就可以复制粘贴代码了,相当省事。首先查看Live环境的网络设备名称:
iwctl device list
设备名为wlan0。由于现在不能输入中文,而且公司只有中文WiFi,只能把WiFi的中文名做个十六进制转义了:
SSID=$'\xE5\x88\x...'
iwctl station wlan0 connect "$SSID"
ping测试连通性,显示正常:
ping ping.archlinux.org
SSH设置和连接
因为 Arch Live中没有密码,而SSH默认禁用空密码连接,需要设置 root 密码,一两个字符就行:
passwd
输入两次密码后成功设置。编辑SSH配置文件:
vim /etc/ssh/sshd_config
取消#注释并修改属性为:
PermitRootLogin yes
PasswordAuthentication yes
重启SSH服务,并确认状态为active:
systemctl restart sshd
systemctl status sshd
查看本机局域网IP:
ip addr
显示wlan0 inet 192.168.1.111。打开另一台设备的SSH终端进行连接。我使用的是Xshell 8,主机填写192.168.1.111,端口默认22。输入用户名root和密码,显示连接成功。现在可以复制代码了!
系统安装
分区调整
lsblk
cfdisk /dev/nvme0n1
在图形化界面中调整分区。删除原有分区,设置1G的EFI分区,剩余空间划为另一个分区,保存退出。没有swap分区,因为后面会设置swap文件。现在分区长这样:
root@archiso ~ # lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 1012.6M 1 loop /run/archiso/airootfs
sda 8:0 1 7.5G 0 disk
├─sda1 8:1 1 7.5G 0 part
│ ├─ventoy 253:0 0 1.5G 1 dm
│ └─sda1 253:1 0 7.5G 0 dm
└─sda2 8:2 1 32M 0 part
sdb 8:16 1 0B 0 disk
nvme0n1 259:0 0 238.5G 0 disk
├─nvme0n1p1 259:4 0 1G 0 part
└─nvme0n1p2 259:5 0 237.5G 0 part
格式化
分别对EFI和根分区格式化。这次选择ext4文件系统,不用btrfs了。btrfs稳定性还是差了一点,系统快照可以交给rsync来做。
mkfs.fat -F32 /dev/nvme0n1p1
mkfs.ext4 /dev/nvme0n1p2
挂载分区
注意不要挂到/mnt/efi,因为后面systemd-boot工具默认在/boot里
mkdir -p /mnt/boot
mount --mkdir /dev/nvme0n1p1 /mnt/boot
mount /dev/nvme0n1p2 /mnt
修改镜像
vim /etc/pacman.d/mirrorlist
将清华源置顶,否则下载速度感人:
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch
更新:
pacman -Syy
安装基础系统
下载一堆核心包和工具。由于这台X280是Intel i3,所以安装intel-ucode:
pacstrap -K /mnt base linux linux-firmware intel-ucode networkmanager sudo vim git base-devel
生成fstab
genfstab -U /mnt > /mnt/etc/fstab
检查是否对应正确:
root@archiso ~ # cat /mnt/etc/fstab
# Static information about the filesystems.
# See fstab(5) for details.
# <file system> <dir> <type> <options> <dump> <pass>
# /dev/nvme0n1p2
UUID=146f0dc9-3f51-4639-9259-9c727fdcd5bc / ext4 rw,relatime 0 1
# /dev/nvme0n1p1
UUID=3EF9-8F60 /boot vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2
系统设置
时区
时区改到魔都:
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
hwclock --systohc
语言和Locale
vim /etc/locale.gen
找到en_GB UTF8 UTF8、zh_CN UTF8 UTF8、zh_HK UTF8 UTF8、zh_TW UTF8 UTF8,取消注释,生成locale文件,并设置LANG为英式英语(为了避免美式英语某些成谜的英制单位):
locale-gen
echo 'LANG=en_GB.UTF-8' > /etc/locale.conf
网络设置
修改host,这里主机名改为了x280:
echo x280 > /etc/hostname
cat > /etc/hosts <<'EOF'
127.0.0.1 localhost
::1 localhost
127.0.1.1 x280.localdomain x280
EOF
systemctl enable NetworkManager
用户
修改root密码:
passwd
创建wheel组的普通用户,懒得起名了,所以这里用户名叫arch
useradd -m -G wheel arch
passwd arch
EDITOR=vim visudo
给sudo权限,编辑掉前面的注释符号
# %wheel ALL=(ALL:ALL) ALL
Bootloader(关键)
选择用systemd-boot,参考[https://wiki.archlinuxcn.org/wiki/Systemd-boot]。
启动选单配置:
bootctl install
cat > /boot/loader/loader.conf <<'EOF'
default arch.conf
timeout 0
editor no
EOF
新建从卷启动 Arch 的启动选项文件。查看启动分区UUID
blkid -s UUID -o value /dev/nvme0n1p2
得到146f0dc9-3f51-4639-9259-9c727fdcd5bc,所以
cat > /boot/loader/entries/arch.conf <<EOF
title Arch Linux
linux /vmlinuz-linux
initrd /intel-ucode.img
initrd /initramfs-linux.img
options root=UUID=146f0dc9-3f51-4639-9259-9c727fdcd5bc rw
EOF
检查:
[root@archiso /]# bootctl list
Not booted with EFI or running in a container, skipping EFI variable modifications.
type: Boot Loader Specification Type #1 (.conf)
title: Arch Linux (default) (not reported/new)
id: arch.conf
source: /boot//loader/
entries/arch.conf (on the EFI System Partition)
linux: /boot//vmlinuz-linux
initrd: /boot//intel-ucode.img
/boot//initramfs-linux.img
options: root=UUID=146f0dc9-3f51-4639-9259-9c727fdcd5bc rw
[root@archiso /]# ls -lh /boot
total 52M
drwxr-xr-x 5 root root 4.0K Aug 16 01:58 EFI
-rwxr-xr-x 1 root root 21M Aug 16 01:41 initramfs-linux.img
-rwxr-xr-x 1 root root 15M Aug 13 06:04 intel-ucode.img
drwxr-xr-x 4 root root 4.0K Aug 16 01:58 loader
-rwxr-xr-x 1 root root 17M Aug 16 01:41 vmlinuz-linux
初次启动
退出chroot并关机:
exit
umount -R /mnt
poweroff
记得把U盘拔下来。现在重新开机,自动进入Arch。命令行不断滚动,直到出现tty命令行和登录界面。恭喜你成功安装了Arch! 但是现在系统里什么都木有,只有缺乏美感的黑白命令行。所以还有事情要做。
联网
现在没有SSH了, 检查网络服务是否在运行(一般来说上面已经设置好了):
systemctl status NetworkManager
安装并启动openssh,修改监听端口Port属性(取消注释,不要用默认的22,否则容易被爆破):
sudo pacman -S openssh
sudo systemctl enable --now sshd
sudo vim /etc/ssh/sshd_config
检查,重启SSH服务:
sudo sshd -t
sudo systemctl restart sshd
现在可以远程SSH登录了,Ctrl+C堂堂复活!
配置防火墙以及公钥登录
SSH其实比较危险,现在的Arch上也没有大数字杀毒软件,所以要手动配置一下防火墙:
sudo pacman -S ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 监听端口号/tcp
sudo ufw enable
sudo systemctl enable --now ufw
sudo ufw status
现在改成只允许公钥登录。先在客户端电脑上生成一对密钥,具体参考[https://learn.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_keymanagement]。然后把公钥传到Arch上:
Get-Content "$env:USERPROFILE\.ssh\id_ed25519.pub" | ssh -p 监听端口 普通用户名@IP地址 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
切回Arch(SSH)。重新配置:
sudo vim /etc/ssh/sshd_config
取消相应属性的注释,修改为:
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
MaxAuthTries 6
检查,重启SSH服务,登出:
sudo sshd -t
sudo systemctl restart sshd
exit
Xshell修改用户密钥为刚才生成的私钥文件,再次尝试SSH登录Arch,如果不需要密码就是成功了。 现在安装fail2ban,进一步ban掉扫端口的恶意ip。
sudo pacman -S fail2ban
sudo vim /etc/fail2ban/jail.local
写入配置
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
backend = systemd
[sshd]
enabled = true
port = 23333
启动服务,检查
sudo systemctl enable --now fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshd
时间同步和蓝牙
sudo timedatectl set-ntp true
timedatectl status
sudo pacman -S bluez bluez-utils bluedevil
sudo systemctl enable --now bluetooth
开启SSD TRIM
装了东芝的256G SSD,手动开一下TRIM提高寿命:
sudo systemctl enable --now fstrim.timer
systemctl status fstrim.timer
安装图形界面及常用组件
喜欢Windows风格就装KDE Plasma,喜欢Mac OS就装GNOME。
sudo pacman -S plasma kde-applications konsole dolphin ark kate spectacle plasma-nm
一路回车。然后安装启动sddm:
sudo pacman -S sddm
sudo systemctl enable sddm
接着安装常用命令行工具:
sudo pacman -S \
git \
base-devel \
wget \
curl \
unzip \
zip \
7zip \
rsync \
htop \
btop \
fastfetch \
man-db \
man-pages \
texinfo \
bash-completion
中文字体
sudo pacman -S \
noto-fonts \
noto-fonts-cjk \
noto-fonts-emoji
KDE中文输入法:
sudo pacman -S fcitx5 fcitx5-im fcitx5-chinese-addons
创建配置文件:
mkdir -p ~/.config/environment.d
cat > ~/.config/environment.d/fcitx5.conf <<'EOF'
GTK_IM_MODULE=fcitx
QT_IM_MODULE=fcitx
XMODIFIERS=@im=fcitx
INPUT_METHOD=fcitx
EOF
进入图形界面
重新启动
sudo reboot
现在SSH结束了使命。Arch重启后可以看到蓝色的图形界面了。左上角Session选择Wayland。接下来可以自行配置、安装软件了!
在System Settings里修改Input Method 为Pinyin即可使用中文输入法。
AUR库
因为某些原因,makepkg连接github经常timeout,不妨直接走局域网转发:在局域网另一台设备上启动某著名科学软件,默认监听7897端口;然后修改临时环境变量,设置Arch的系统代理为:
export http_proxy=http://192.168.x.xxx:7897
export https_proxy=http://192.168.x.xxx:7897
export HTTP_PROXY=http://192.168.x.xxx:7897
export HTTPS_PROXY=http://192.168.x.xxx:7897
测试:
curl -I https://github.com
然后新建builds文件夹安放临时文件,并安装yay:
mkdir -p ~/builds
cd ~/builds
sudo pacman -S --needed base-devel git
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si
一路按Y。现在可以用yay安装AUR库的程序了。