Skip to content

Instantly share code, notes, and snippets.

@bouroo
Last active May 12, 2025 04:16
Show Gist options
  • Save bouroo/b750d3fa357362772082407cfa21fb4a to your computer and use it in GitHub Desktop.
Save bouroo/b750d3fa357362772082407cfa21fb4a to your computer and use it in GitHub Desktop.
Init arch linux pacman
#!/usr/bin/env bash
# Exit immediately if a command exits with a non-zero status.
# Treat unset variables as an error.
# Exit immediately if a command in a pipeline fails.
set -euo pipefail
# Enable and start systemd-networkd and systemd-resolved
echo "Enabling and starting systemd services..."
systemctl enable --now systemd-networkd
systemctl enable --now systemd-resolved
echo "Systemd services enabled and started."
# --- Pacman Configuration ---
# Define mirrorlist URL and target file
MIRRORLIST_URL="https://www.archlinux.org/mirrorlist/?country=TH&country=SG&country=IN&country=JP&protocol=https&ip_version=4"
MIRRORLIST_FILE="/etc/pacman.d/mirrorlist"
MIRRORLIST_BACKUP_FILE="/etc/pacman.d/mirrorlist.backup"
echo "Initializing Pacman mirrorlist..."
# Download and process mirrorlist: uncomment Server lines and remove other commented lines
curl -s -L "$MIRRORLIST_URL" | sed -e 's/^#Server/Server/' -e '/^#/d' > "$MIRRORLIST_FILE"
echo "Mirrorlist updated."
echo "Initializing and populating Pacman keyring..."
# Initialize Pacman keyring
pacman-key --init
# Populate Arch Linux keys
pacman-key --populate archlinux
echo "Keyring initialized."
echo "Synchronizing and updating system..."
# Sync and update the system with no confirmation
pacman -Syyu --noconfirm
echo "System updated."
# Define common packages to install
COMMON_PACKAGES="sudo htop iftop mtr tmux dkms lz4 bash-completion base-devel pacman-contrib"
echo "Installing common packages: $COMMON_PACKAGES..."
# Install common packages with no confirmation
pacman -S --noconfirm $COMMON_PACKAGES
echo "Common packages installed."
echo "Ranking mirrors by speed..."
# Backup current mirrorlist before ranking
cp "$MIRRORLIST_FILE" "$MIRRORLIST_BACKUP_FILE"
# Rank the top 5 mirrors and update the mirrorlist
rankmirrors -n 5 "$MIRRORLIST_BACKUP_FILE" > "$MIRRORLIST_FILE"
echo "Mirrorlist ranked."
# --- User Configuration ---
# Define new user details
NEW_USER="admin"
SUDOERS_DROPIN_FILE="/etc/sudoers.d/10-wheel-nopasswd"
echo "Creating new user '$NEW_USER'..."
# Create new user with home directory
useradd -m "$NEW_USER"
echo "User '$NEW_USER' created."
echo "Setting password for user '$NEW_USER'."
# Prompt for and set the user's password (requires manual input during script execution)
passwd "$NEW_USER"
echo "Adding user '$NEW_USER' to 'wheel' group..."
# Add user to the wheel group
usermod -aG wheel "$NEW_USER"
echo "User '$NEW_USER' added to 'wheel' group."
echo "Configuring sudo for 'wheel' group (NOPASSWD)..."
# Create a sudoers drop-in file for passwordless sudo for the wheel group
# This is safer than directly editing /etc/sudoers
echo '%wheel ALL=(ALL) NOPASSWD: ALL' > "$SUDOERS_DROPIN_FILE"
# Set appropriate permissions for the sudoers file
chmod 0440 "$SUDOERS_DROPIN_FILE"
echo "Sudo configured for 'wheel' group."
echo "Script finished successfully."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment