hetzner-arch-luks/README.md

341 lines
7.0 KiB
Markdown
Raw Normal View History

2020-10-05 14:09:08 +02:00
# Arch Linux with LUKS and btrfs on a Hetzner server
2020-04-17 21:16:06 +02:00
## Software
This guide shows how to set up the following software composition:
* [Arch Linux](https://www.archlinux.de/)
* [btrfs](https://en.wikipedia.org/wiki/Btrfs)
* [LUKS](https://wiki.archlinux.org/index.php/Dm-crypt)
## Requirements
Written for a [Dedicated](https://de.wikipedia.org/wiki/Server#Dedizierte_Server) [Hetzner](https://www.hetzner.com/) server with the following hardware specifications:
2020-04-17 21:20:16 +02:00
```
2020-04-17 21:16:06 +02:00
CPU1: Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz (Cores 8)
Memory: 15973 MB
Disk /dev/sda: 3000 GB (=> 2794 GiB)
Disk /dev/sdb: 3000 GB (=> 2794 GiB)
Total capacity 5589 GiB with 2 Disks
2020-04-17 21:20:16 +02:00
```
2020-04-15 12:32:29 +02:00
2020-04-17 21:02:07 +02:00
## Legend
2020-04-17 21:47:18 +02:00
The following symbols show in which environment the code is executed:
2020-04-17 21:02:07 +02:00
* :computer: Client
2020-04-19 13:12:15 +02:00
* :ambulance: [Hetzner Rescue System](https://wiki.hetzner.de/index.php/Hetzner_Rescue-System/en)
2020-04-17 21:02:07 +02:00
* :ghost: Chroot from Rescue System into Arch
2020-04-19 13:12:15 +02:00
* :minidisc: Arch OS
2020-04-17 21:02:07 +02:00
2020-04-15 12:32:29 +02:00
## Guide
2020-04-15 13:40:10 +02:00
### 1. Configure and Install Image
2020-04-17 20:43:52 +02:00
#### 1.1 Login to Hetzner Rescue System
2020-04-17 21:02:07 +02:00
:computer: :
2020-04-15 13:40:10 +02:00
```bash
ssh root@your_server_ip
```
2020-04-17 20:43:52 +02:00
#### 1.2 Create the /autosetup
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-15 13:40:10 +02:00
```bash
nano /autosetup
```
2020-04-17 21:02:07 +02:00
Save the following content into this file:
2020-04-15 12:32:29 +02:00
2020-04-17 21:02:07 +02:00
```
2020-04-15 12:32:29 +02:00
## Hetzner Online GmbH - installimage - config
DRIVE1 /dev/sda
DRIVE2 /dev/sdb
## SOFTWARE RAID:
## activate software RAID? < 0 | 1 >
SWRAID 1
## Choose the level for the software RAID < 0 | 1 | 10 >
SWRAIDLEVEL 1
## BOOTLOADER:
BOOTLOADER grub
## HOSTNAME:
HOSTNAME hetzner-arch-luks
#Adapt the hostname to your needs
## PARTITIONS / FILESYSTEMS:
PART /boot btrfs 512M
PART lvm vg0 all
LV vg0 swap swap swap 8G
LV vg0 root / btrfs 10G
## OPERATING SYSTEM IMAGE:
IMAGE /root/.oldroot/nfs/install/../images/archlinux-latest-64-minimal.tar.gz
```
2020-04-17 20:43:52 +02:00
#### 1.3 Install Image
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-15 13:40:10 +02:00
```bash
installimage
```
2020-04-17 20:43:52 +02:00
#### 1.4 Restart
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-15 13:40:10 +02:00
```bash
reboot
```
### 2. Setup System
2020-04-17 20:43:52 +02:00
#### 2.1 Login to server
2020-04-17 21:02:07 +02:00
:computer: :
2020-04-15 13:40:10 +02:00
```bash
2020-04-17 20:43:52 +02:00
ssh-keygen -f "$HOME/.ssh/known_hosts" -R your_server_ip
2020-04-15 13:40:10 +02:00
ssh root@your_server_ip
```
2020-04-17 20:43:52 +02:00
#### 2.2 Update the system
2020-04-17 21:02:07 +02:00
:minidisc: :
2020-04-15 13:40:10 +02:00
```bash
pacman -Syyu
```
2020-04-17 20:43:52 +02:00
#### 2.3 Install administration tools:
2020-04-17 21:02:07 +02:00
:minidisc: :
2020-04-15 13:40:10 +02:00
```bash
2020-04-17 18:52:26 +02:00
pacman -S nano
2020-04-15 13:40:10 +02:00
```
2020-04-17 20:43:52 +02:00
### 3. Prepare System for Unlocking via SSH
2020-04-19 13:12:15 +02:00
#### 3.1 Install software
2020-04-17 21:02:07 +02:00
:minidisc: :
2020-04-15 13:40:10 +02:00
```bash
2020-04-17 18:52:26 +02:00
pacman -S busybox mkinitcpio-dropbear mkinitcpio-utils mkinitcpio-netconf
2020-04-19 13:12:15 +02:00
```
2020-04-19 13:30:11 +02:00
#### 3.2 Copy authorized keys to dropbear
2020-10-05 14:09:08 +02:00
:minidisc: :
2020-04-19 13:12:15 +02:00
```bash
2020-04-16 22:34:01 +02:00
cp -v ~/.ssh/authorized_keys /etc/dropbear/root_key
2020-04-15 13:40:10 +02:00
```
2020-05-15 09:38:17 +02:00
```bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
systemctl enable sshd
```
2020-04-19 13:30:11 +02:00
2020-04-27 19:13:29 +02:00
#### 3.3 Regenerate OpenSSH keys
:minidisc: :
2020-04-27 17:52:49 +02:00
```bash
rm /etc/ssh/ssh_host_*
ssh-keygen -A -m PEM
```
2020-04-27 19:13:29 +02:00
#### 3.4 Import SSH-keys to dropbear
:minidisc: :
2020-04-27 17:52:49 +02:00
```bash
dropbearconvert openssh dropbear /etc/ssh/ssh_host_rsa_key /etc/dropbear/dropbear_rsa_host_key
```
2020-04-27 19:13:29 +02:00
#### 3.5 Modify /etc/mkinitcpio.conf
2020-04-17 21:02:07 +02:00
:minidisc: :
```bash
nano /etc/mkinitcpio.conf
```
##### Replace
2020-04-19 13:30:11 +02:00
**Old:**
2020-04-16 20:14:12 +02:00
```
HOOKS=(base udev autodetect modconf block mdadm_udev lvm2 filesystems keyboard fsck)
```
2020-04-19 13:30:11 +02:00
**New:**
2020-04-16 20:14:12 +02:00
```
2020-04-17 18:52:26 +02:00
HOOKS=(base udev autodetect modconf block mdadm_udev lvm2 netconf dropbear encryptssh filesystems keyboard fsck)
2020-04-16 20:14:12 +02:00
```
### 4. Activate Encryption
2020-04-17 21:02:07 +02:00
#### 4.1 Activate Rescue System
2020-04-16 20:14:12 +02:00
Activate the rescue system https://robot.your-server.de/server
2020-04-17 20:43:52 +02:00
#### 4.2 Reboot
2020-04-17 21:02:07 +02:00
:minidisc: :
2020-04-16 20:14:12 +02:00
```bash
reboot
```
2020-04-17 20:43:52 +02:00
#### 4.3 Login to the rescue system
2020-04-17 21:02:07 +02:00
:computer: :
2020-04-16 20:14:12 +02:00
```bash
2020-04-17 20:43:52 +02:00
ssh-keygen -f "$HOME/.ssh/known_hosts" -R your_server_ip
2020-04-16 20:14:12 +02:00
ssh root@your_server_ip
```
2020-04-15 13:40:10 +02:00
2020-04-17 20:43:52 +02:00
#### 4.4 Mount the "system"
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-16 20:14:12 +02:00
```bash
vgscan -v
vgchange -a y
mount /dev/mapper/vg0-root /mnt
```
2020-04-17 20:43:52 +02:00
#### 4.5 Copy "system"
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-16 20:14:12 +02:00
```bash
echo 0 >/proc/sys/dev/raid/speed_limit_max
mkdir /oldroot
2020-04-16 22:34:01 +02:00
cp -va /mnt/. /oldroot/.
2020-04-16 20:14:12 +02:00
echo 200000 >/proc/sys/dev/raid/speed_limit_max
```
2020-04-17 20:43:52 +02:00
#### 4.6 Unmount the "system"
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-16 20:14:12 +02:00
```bash
umount /mnt
```
2020-04-15 13:40:10 +02:00
2020-04-17 20:43:52 +02:00
#### 4.7 Delete decrypted LVM-Volume-Group
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-16 20:14:12 +02:00
```bash
2020-04-16 20:27:35 +02:00
vgremove vg0
2020-04-16 20:14:12 +02:00
```
2020-04-17 20:43:52 +02:00
#### 4.8 Check drive state
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-16 20:14:12 +02:00
```bash
cat /proc/mdstat
```
2020-04-17 20:43:52 +02:00
#### 4.9 Encrypt MD1 by executing
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-16 20:14:12 +02:00
```bash
cryptsetup --cipher aes-xts-plain64 --key-size 256 --hash sha256 --iter-time=10000 luksFormat /dev/md1
2020-04-16 22:34:01 +02:00
cryptsetup luksOpen /dev/md1 cryptroot
2020-04-16 20:14:12 +02:00
pvcreate /dev/mapper/cryptroot
vgcreate vg0 /dev/mapper/cryptroot
lvcreate -n swap -L8G vg0
lvcreate -n root -L10G vg0
mkfs.btrfs /dev/vg0/root
mkswap /dev/vg0/swap
```
2020-04-17 20:43:52 +02:00
#### 4.10 Mount encrypted
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-16 20:14:12 +02:00
```bash
mount /dev/vg0/root /mnt
```
2020-04-17 20:43:52 +02:00
#### 4.12 Copy "system"
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-16 20:14:12 +02:00
```bash
echo 0 >/proc/sys/dev/raid/speed_limit_max
2020-04-16 22:34:01 +02:00
cp -av /oldroot/. /mnt/.
2020-04-16 20:14:12 +02:00
echo 200000 >/proc/sys/dev/raid/speed_limit_max
```
2020-04-17 20:43:52 +02:00
#### 4.13 Integrate Finale Installation
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-16 20:14:12 +02:00
```bash
mount /dev/md0 /mnt/boot
mount --bind /dev /mnt/dev
mount --bind /sys /mnt/sys
mount --bind /proc /mnt/proc
chroot /mnt
```
#### 4.14
2020-04-17 21:02:07 +02:00
:ghost: :
2020-04-16 20:14:12 +02:00
```bash
echo "cryptroot /dev/md1 none luks" >> /etc/crypttab
```
2020-04-17 21:02:07 +02:00
2020-04-17 20:43:52 +02:00
#### 4.15 Create an initial ramdisk
2020-04-17 21:02:07 +02:00
:ghost: :
2020-04-16 22:34:01 +02:00
```bash
2020-04-16 22:48:59 +02:00
mkinitcpio -p linux
2020-04-16 22:34:01 +02:00
```
2020-04-17 19:21:08 +02:00
2020-04-17 20:43:52 +02:00
### 5 Grub
#### 5.1 Install Grub
2020-04-17 21:02:07 +02:00
:ghost: :
2020-04-17 18:52:26 +02:00
```bash
pacman -S grub
```
2020-04-17 20:43:52 +02:00
#### 5.2 Configure /etc/default/grub
2020-04-19 13:12:15 +02:00
2020-04-17 21:02:07 +02:00
:ghost: :
2020-04-19 13:12:15 +02:00
```bash
nano /etc/default/grub
```
2020-04-17 21:02:07 +02:00
2020-04-19 13:12:15 +02:00
Change the following parameters:
2020-04-17 18:52:26 +02:00
```bash
2020-10-04 21:06:22 +02:00
GRUB_CMDLINE_LINUX="cryptdevice=/dev/md1:cryptroot ip=dhcp"
2020-10-15 13:56:40 +02:00
GRUB_ENABLE_CRYPTODISK=y
2020-04-17 18:52:26 +02:00
```
2020-04-17 21:47:18 +02:00
:information_source: Further [information](https://wiki.archlinux.org/index.php/Dm-crypt/Encrypting_an_entire_system#Configuring_GRUB).
2020-04-17 20:43:52 +02:00
#### 5.3 Make and Install on Hard-drives
2020-04-17 21:02:07 +02:00
:ghost: :
2020-04-17 18:52:26 +02:00
```bash
grub-mkconfig -o /boot/grub/grub.cfg
grub-install /dev/sda
grub-install /dev/sdb
```
2020-04-16 20:14:12 +02:00
2020-04-17 20:43:52 +02:00
#### 5.4 Restart System
2020-04-17 21:02:07 +02:00
:ghost: :ambulance: :
2020-04-16 20:14:12 +02:00
```bash
exit
umount /mnt/boot /mnt/proc /mnt/sys /mnt/dev
umount /mnt
sync
reboot
```
2020-04-17 20:43:52 +02:00
### 6. Encryption Procedure
#### 6.1 Decrypt server
2020-04-17 21:02:07 +02:00
:computer: :
2020-04-17 19:21:08 +02:00
```bash
ssh -o UserKnownHostsFile=/dev/null root@your_server_ip
cryptroot-unlock
exit
```
2020-04-17 20:43:52 +02:00
#### 6.2 Login to server
2020-04-17 21:02:07 +02:00
:computer: :
2020-04-17 19:21:08 +02:00
```bash
ssh-keygen -f "$HOME/.ssh/known_hosts" -R your_server_ip
ssh root@your_server_ip
```
2020-10-14 14:47:51 +02:00
#### 7. Expand filesystem
:computer: :
```bash
lvresize -l +100%FREE /dev/vg0/root
btrfs filesystem resize max /
```
## 8. Debugging
### 8.1 Login to System from Rescue System
2020-04-17 21:02:07 +02:00
:ambulance: :
2020-04-17 20:43:52 +02:00
```bash
cryptsetup luksOpen /dev/md1 cryptroot
mount /dev/vg0/root /mnt
mount /dev/md0 /mnt/boot
mount --bind /dev /mnt/dev
mount --bind /sys /mnt/sys
mount --bind /proc /mnt/proc
chroot /mnt
```
2020-10-14 14:47:51 +02:00
### 8.2 Logout from chroot environment
2020-04-19 13:30:11 +02:00
:ghost: :ambulance: :
2020-04-17 21:47:18 +02:00
```bash
exit
umount /mnt/boot /mnt/proc /mnt/sys /mnt/dev
umount /mnt
sync
reboot
```
2020-04-17 20:43:52 +02:00
2020-10-14 14:47:51 +02:00
### 8.3 Regenerate GRUB and Arch
2020-04-19 13:12:15 +02:00
:ghost: :
```bash
mkinitcpio -p linux
grub-mkconfig -o /boot/grub/grub.cfg
grub-install /dev/sda
grub-install /dev/sdb
```
2020-04-15 12:32:29 +02:00
## Sources
The code is adapted from the following guides:
* http://daemons-point.com/blog/2019/10/20/hetzner-verschluesselt/
* https://www.howtoforge.com/using-the-btrfs-filesystem-with-raid1-with-ubuntu-12.10-on-a-hetzner-server
2020-04-17 19:21:08 +02:00
* https://code.trafficking.agency/arch-linux-remote-unlock-root-volume-with-mdraid-and-dmcrypt.html
2020-04-16 20:14:12 +02:00
* https://wiki.archlinux.org/index.php/Dm-crypt/Specialties#Remote_unlocking_(hooks:_netconf,_dropbear,_tinyssh,_ppp)
2020-04-27 17:52:49 +02:00
* https://gist.github.com/pezz/5310082