Talk is cheap, show me the code, Linux Torvald.
This is Linux world. In silent nights you can hear the Windows machines rebooting.
Let’s create a series of scripts to install, maintain, and automate some tasks in ArchLinux.
It is sometimes a good idea or convenient to install ArchLinux using SSH.
#!/bin/bash
# Set up the keyboard layout: loadkeys es
echo "KEYMAP=es" >> /etc/vconsole.conf
Previously, you should verify boot mode (UEFI). If it doesn’t exist, you are either sitting on old hardware or you have UEFI disabled in the BIOS: ls /sys/firmware/efi/efivars
Besides, check your Internet connection: ping -s 5 justtothepoint.com
If it does not work: ip link shows your network device configuration:
lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
enp5s0: mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 7c:10:c9:42:f2:b7 brd ff:ff:ff:ff:ff:ff 3: wlo1: mtu 1500 qdisc noqueue state DOWN mode DORMANT group default qlen 1000 link/ether 82:1f:72:58:c2:1a brd ff:ff:ff:ff:ff:ff permaddr 14:85:7f:b9:6f:b8 altname wlp0s20f3
You may want to try to bring up the interface: ip link set dev enp5s0 up. Next, run dhcpd to provide DHCP service to your machine: dhcpcd enp5s0. Finally, check your Internet connection again: ping -s 5 justtothepoint.com
# Set up your timezone.
timedatectl set-timezone Europe/Madrid
# Enable and start network time synchronization.
sudo timedatectl set-ntp true
NTP stands for Network Time Protocol. It is used to synchronize computers clocks automatically over the Internet and help ensure that all system clocks in servers and networking equipment use accurate time.
# Set the hardware clock to the local system clock.
sudo hwclock --systohc
Safety isn’t expensive, it’s priceless.
Partitioning is difficult and inherently dangerous. Please backup your data before this operation.
sgdisk is a command-line utility for manipulating partition tables under Linux.
# The order lsblk lists all block-level devices available on your system.
disk=/dev/nvme0n1
# Wipe out the partition table (it deletes all GPT and MBR entries and creates a new GPT -GUID Partition Table-) and any data on it!
sgdisk --zap-all $disk
# Partition the drive:
sgdisk -og $disk # Convert an MBR (Master Boot Record) or BSD disklabel disk to a GPT disk.
We are creating four partitions: sgdisk -n,
It creates the following partitions: /boot/ -1M-, / (root) -30G-, swap -2G-, and /home -the remaining disk space.
sgdisk -n 1:2048:+1M -c 1:"BIOS Boot Partition" -t 1:ef02 $disk
sgdisk -n 2:0:+30G -c 2:"Linux /" -t 2:8304 $disk
sgdisk -n 3:0:+2G -c 3:"[SWAP]" -t 3:8200 $disk
sgdisk -n 4:0:0 -c 4:"Linux /home" -t 4:8302 $disk
# Format the partitions. mkfs stands for “make file system.”
mkfs.vfat /dev/nvme0n1p1
mkfs.ext4 /dev/nvme0n1p2
mkfs.ext4 /dev/nvme0n1p4
# mkswap sets up a Linux swap area on the third partition of our disk.
mkswap /dev/nvme0n1p3
# swapon enables it for swapping.
swapon /dev/nvme0n1p3
# Mount the root, home, and the EFI boot filesystems.
mount /dev/nvme0n1p2 /mnt
mkdir -p /mnt/{boot/efi,home}
mount /dev/nvme0n1p1 /mnt/boot/efi
mount /dev/nvme0n1p4 /mnt/home
#!/bin/bash
# The archlinux-keyring package contains the latest keys.
pacman -S archlinux-keyring
# Install Arch Linux:
pacstrap /mnt base base-devel linux-lts linux-firmware git vim intel-ucode base-devel linux-lts-headers
# pacstrap is designed to create a new system installation from scratch.
# Generate /etc/fstab. genfstab generates the /etc/fstab file by autodetecting all the current mounts below a given mountpoint (/mnt)
genfstab -U /mnt >> /mnt/etc/fstab
This is my /etc/fstab:
#/dev/nvme0n1p2
UUID=70b8e993-17b3-4991-af26-c2a96290425f / ext4 rw,relatime 0 1
# UUID is an identifier used in partitions to uniquely identify partitions in GNU/Linux.
#/dev/nvme0n1p1
UUID=9619-B821 /boot/efi vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2
#/dev/nvme0n1p4
UUID=98213662-0927-49fc-8768-d870d9de3870 /home ext4 defaults,rw,nofail,noatime 0 0
#/dev/nvme0n1p3
UUID=2fcbaaa5-3f66-4e85-b98b-c94f29d80f6f none swap defaults 0 0
# Enter or switch to the new system.
arch-chroot /mnt /bin/bash
# Set the timezone and update the system clock:
ln -sf /usr/share/zoneinfo/Europe/Madrid /etc/localtime
# Set the hardware clock to the current system time (systohc stands for “system to hardware clock").
hwclock --systohc --utc
# Set up the locale, i.e., the language, numbering, date, and currency formats for your system.
sed --in-place=.bak 's/^#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen.
sed –in-place edits text on the file /etc/locale.gen. The syntax of the s (substitute) command is ‘s/regexp/replacement/flags’.
A regular expression is a sequence of characters that forms a search pattern. Metacharacters are characters with a special meaning: ^ (Starts with), so we are searching for #en_US.UTF-8 UTF-8 and replacing it by en_US.UTF-8 UTF-8 (UTF-8 American/English locale).
locale-gen is a program that reads the file /etc/locale.gen and generates localisation files
locale-gen
# The /etc/locale.conf file configures system-wide locale settings.
echo "LANG=en_US.UTF-8" >> /etc/locale.conf
# Set up the keyboard layout: loadkeys es
echo "KEYMAP=es" >> /etc/vconsole.conf
# Network configuration.
echo "myarch" >> /etc/hostname # It sets the hostname as myarch.
echo "127.0.0.1 localhost" >> /etc/hosts # /etc/hosts translates hostnames to IP-addresses.
echo "::1 localhost" >> /etc/hosts
echo "127.0.1.1 myarch.localdomain arch" >> /etc/hosts
/etc/hosts translates hostnames to IP-addresses and it was used for all computers connected to the internet before DNS was created.
The idea is simple, a file with an address and a host name for each line. Nowadays, the name resolution is defined in /etc/nsswitch.conf. It usually contains this entry, hosts: file dns. It means try in /etc/hosts, and if it fails, try DNS.
echo "Setting root password"
passwd
# Create a new sudo (Super User Do) user and avoid typing his password constantly
read -r -p "Enter a new user name:" username
# Create a new user and add him or her to the wheel group.
useradd -m -G wheel -s /bin/bash $username
passwd $username
# _Members of the wheel group are automatically granted sudo privileges_: %wheel ALL=(ALL). NOPASSWD: ALL means that the system requires no password when these members invoke the sudo command.
echo '%wheel ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
echo 'Defaults insults' >> /etc/sudoers # Sudo insults you if you enter an incorrect password. Obviously, this step is super important for the whole installation.
# Install the boot manager:
pacman -Sy efibootmgr networkmanager network-manager-applet dialog wpa\_supplicant linux-headers xdg-utils xdg-user-dirs pulseaudio alsa-utils pavucontrol
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB
grub-install’s arguments:
‐‐ efi-directory=/boot/efi It specifies where the EFI System is mounted.
‐‐ bootloader-id=GRUB It is the bootloader identifier.
‐‐ target=x86_64-efi It installs GRUB for x86_64 systems.
After the installation, the configuration file /boot/grub/grub.cfg needs to be generated by using grub-mkconfig:
grub-mkconfig -o /boot/grub/grub.cfg
# Start the service NetworkManager. It manages your network devices and connections: Ethernet, Wifi, etc. It attempts to keep your network connectivity active all the time.
systemctl enable NetworkManager
# Exit the new system and unmount the partitions.
exit
umount -R /mnt
echo All Done. Type "reboot" to enjoy!
If you want to set the GRUB’s time to zero: sudo vim /etc/default/grub: GRUB_TIMEOUT=0
You may also want to update your CPU microcode on Arch Linux: sudo pacman -Syy intel-ucode. sudo grub-mkconfig will automatically detect the microcode update and configure GRUB appropriately.
After installing the microcode package or set the GRUB’s time to zero, regenerate the GRUB config to activate loading the microcode update by running sudo grub-mkconfig -o /boot/grub/grub.cfg