owned this note
owned this note
Published
Linked with GitHub
---
tags: raspberry
---
# Arch Linux in Raspberry Pi
[TOC]
:::danger
Support for ARMv6 has [ended](https://archlinuxarm.org/forum/viewtopic.php?f=3&t=15721), for raspberry Pi zero we need to install raspbian.
:::
## Install base system
Official [instructions](https://archlinuxarm.org/platforms/armv6/raspberry-pi)
### Partition, format and mount sdcard
Replace **sdX** in the following instructions with the device name for the SD card as it appears on your computer.
1. Start _fdisk_ to partition the SD card:
~~~bash
sudo fdisk /dev/sdX
~~~
2. At the fdisk prompt, delete old partitions and create a new one:
* Type **o**. This will clear out any partitions on the drive.
* Type **p** to list partitions. There should be no partitions left.
* Type **n**, then **p** for primary, **1** for the first partition on the drive, press **ENTER** to accept the default first sector, then type **+200M** for the last sector.
* Type **t**, then **c** to set the first partition to type W95 FAT32 (LBA).
* Type **n**, then **p** for primary, **2** for the second partition on the drive, and then press **ENTER twice** to accept the default first and last sector.
* Type **p** to list partitions, you should see something like this:

* Write the partition table and exit by typing **w**.
3. Create and mount the FAT filesystem:
~~~
mkfs.vfat /dev/sdX1
mkdir boot
sudo mount /dev/sdX1 boot
~~~
4. Create and mount the ext4 filesystem:
~~~
mkfs.ext4 /dev/sdX2
mkdir root
sudo mount /dev/sdX2 root
~~~
### Copy system files
1. Download and extract the root filesystem (as root, not via sudo):
Find the proper image depending on your raspberry pi [model](https://archlinuxarm.org/platforms).
For raspberry 3 and 4 you can use a 64 bit image: [ArchLinuxARM-rpi-aarch64-latest.tar.gz](http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-aarch64-latest.tar.gz)
~~For Raspberry Pi zero you should use: [ArchLinuxARM-rpi-latest.tar.gz]http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-latest.tar.gz)~~ Support for this CPU has ended 😢
~~~
wget http://os.archlinuxarm.org/os/put-your-image-version-here
sudo su
bsdtar -xpf your-image-version-file -C root
sync
~~~
2. Move boot files to the first partition:
~~~
mv root/boot/* boot
sync
~~~
(Don't worry about the _failed to preserve ownership: Operation not permited_ error, is harmless)
3. Setup WiFi (optional)
Create a new file: `root/etc/systemd/network/wlan0.network` and add this content:
~~~
[Match]
Name=wlan0
[Network]
DHCP=yes
~~~
Now setup your WiFi SSID and password with (remember to input your credentials!!):
~~~
wpa_passphrase "SSID" "Password" > root/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
~~~
And enable the service with:
~~~
ln -s root/usr/lib/systemd/system/wpa_supplicant@.service root/etc/systemd/system/multi-user.target.wants/wpa_supplicant@wlan0.service
~~~
4. Unmount the two partitions:
~~~
umount boot root
~~~
### Boot the new system and init package manager
1. Insert the SD card into the Raspberry Pi, connect ethernet (if available), and apply 5V power.
2. Use the serial console or SSH to the IP address given to the board by your router. Login as the default user alarm with the password alarm. The default root password is root.
3. Initialize the pacman keyring and populate the Arch Linux ARM package signing keys:
~~~
su -
pacman-key --init
pacman-key --populate archlinuxarm
~~~
### Extras
* Change root password (the password is _root_)
~~~
su -
passwd
~~~
* Update system
~~~
pacman -Syu
~~~
* Install sudo
~~~
pacman -S sudo
~~~
* Create new user
~~~
USERNAME=juanito
useradd $USERNAME
mkdir /home/$USERNAME
chown $USERNAME:$USERNAME /home/$USERNAME
passwd $USERNAME
~~~
To give this user sudo privileges run `sudo visudo` and under the line `root ALL=(ALL) ALL` add the line `USERNAME ALL=(ALL) ALL` and save the file.
* Remove user _alarm_
Log in as the new created user and delete _alarm_ user with `sudo userdel alarm` and his home folder (be sure you don't have anything you want to keep in there) `sudo rm -rf /home/alarm`
* Change hostname
~~~
sudo vi /etc/hostname
~~~
* Set the timezone
~~~
timedatectl list-timezones
sudo timedatectl set-timezone Zone/SubZone
~~~
ej. `sudo timedatectl set-timezone Europe/Madrid`
* Setup [console keyboard languaje](https://wiki.archlinux.org/index.php/Linux_console/Keyboard_configuration):
Edit/create the file `/etc/vconsole.conf` and add/replace the line `KEYMAP=es` with the languaje of your choice.
* Change locale (ref [here](https://wiki.archlinux.org/title/Locale))
Generate them if they haven't been generated yet. Uncomment the one you want in the following file:
~~~
sudo vi /etc/locale.gen
~~~
For instance `en_US.UTF-8`
And then generate with:
~~~
sudo locale-gen
~~~
Then set them with:
~~~
sudo localectl set-locale LANG=en_US.UTF-8
~~~
* Console font
To setup a nicer and bigger console font you can install terminus-font package with `sudo pacman -S terminus-font` and the add a line to `/etc/vconsole.conf` with the content `FONT=ter-h28b`
* Install [Paru](https://github.com/morganamilo/paru) AUR package manager
~~~bash
sudo pacman -S --needed base-devel git
git clone https://aur.archlinux.org/paru.git
cd paru
makepkg -si
~~~
Go for a :coffee:
## Locate a Pi on your network
When the setup is finished you need to login to your Pi via ssh, to find out the IP address there are different options:
1. Connect your Pi to a monitor and keyboard, login locally and check ip address:
~~~
ip a show dev wlan0
~~~
2. If you have access to your router you can use the web interface to check wich IP was assigned to the Pi.
3. Check which MAC address are connected to your network and filter the Raspberry Pi foundation MAC address ranges:
||
|---|
|B8:27:EB:xx:xx:xx|
|DC:A6:32:xx:xx:xx|
|E4:5F:01:xx:xx:xx|
For this you can use the **_arp_** command (in linux), first to update the arp cache you can do a ping scan with **_nmap_**:
~~~
nmap 192.168.0.1/24
arp -na | grep -e B8:27:EB -e DC:A6:32 -e E4:5F:01
~~~
Or just put everything in a bash script:
~~~
#!/bin/sh
MY_IP_RANGE=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}') && nmap -sn $MY_IP_RANGE && IP=$(arp -na | grep b8:27:eb | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{
1,3}\.[0-9]{1,3}')
echo $IP
~~~
There is also a tool called arp-scan (must use it with sudo) that does every thing in one step:
~~~
sudo arp-scan -l
~~~
## Install MQTT broker
~~~bash
sudo pacman -S mosquitto
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
~~~
Edit `/etc/mosquitto/mosquitto.conf` and add/uncoment:
~~~
listener 1883
allow_anonymous true
~~~
and restart mosquitto service: `sudo systemctl restart mosquitto`
## Install Node-Red
If you have [yay](https://github.com/Jguer/yay) installed just do
~~~bash
yay -S nodejs-node-red
~~~
if you prefer not to install yay you can do it manually:
~~~bash
git clone https://aur.archlinux.org/nodejs-node-red.git
cd node-js-red
makepkg -si
~~~
Enable and start the service
~~~bash
sudo systemctl enable nodejs-node-red
sudo systemctl start nodejs-node-red
~~~
Now you should found node-red interface in http://yourserver:1880
## Setup WiFi Acces Point
Install the [create_ap](https://github.com/lakinduakash/linux-wifi-hotspot/tree/master/src/scripts#generic) script.
~~~
sudo pacman -S create_ap
~~~
Test it with
~~~
sudo create_ap wlan0 eth0 mySSID myPassphrase
~~~
If it works properly you can setup a persistent systemd service.
Edit `/etc/create_ap.conf`.
Enable and start create_ap service
~~~
sudo systemctl enable create_ap
sudo systemctl start create_ap
~~~