# Debian Guides

# Bootstrapping a Debian Machine

[![debian.org](https://img.shields.io/badge/debian.org-0288D1?style=flat)](https://www.debian.org/)
[![downloads](https://img.shields.io/badge/downloads-0f7d15?style=flat)](https://www.debian.org/distrib/netinst)
[![debian](https://img.shields.io/badge/debian-555555?style=flat&logo=github&logoColor=white)](https://github.com/debian)

Installation
-------------

#### Clean Up the Standard Installation

```shell
sudo apt purge debian-faq doc-debian installation-report tasksel
```

```shell
sudo apt --purge autoremove
```

#### Install Essentials

```shell
sudo apt install bash-completion bind9-dnsutils bzip2 curl ethtool htop iotop ntpsec-ntpdig screen sudo unzip vim wget
```


#### Install Security Enhancements

[![Enforcing Password Quality](https://img.shields.io/badge/Enforcing_Password_Quality-4f46e5?style=flat)](/books/debian-guides/page/enforcing-password-quality)
[![Verifying Installation Integrity](https://img.shields.io/badge/Verifying_Installation_Integrity-4f46e5?style=flat)](/books/debian-guides/page/verifying-installation-integrity)

```shell
sudo apt install debsums libpam-pwquality libpam-tmpdir
```

#### Install Virtual Machine Guest Agent

```shell
sudo apt install qemu-guest-agent
```



Configuration
--------------

#### Grant Password-less Sudo Access

>
> Since `sudo` is likely not yet available, the following commands must be run as `root`.
>

```shell
( umask 0337; tee "/etc/sudoers.d/sudo" >/dev/null <<'EOF'
%sudo ALL=(ALL) NOPASSWD: ALL
EOF
)
chmod 0440 "/etc/sudoers.d/sudo"
```

```shell
gpasswd --add [USERNAME] sudo
```

#### Set the Machine Hostname

```shell
sudo hostnamectl set-hostname [HOSTNAME]
```

```shell
sudo sed -i '/^127.0.1.1/d' "/etc/hosts"
sudo sed -i '1i127.0.1.1\t[HOSTNAME].[DOMAIN] [HOSTNAME]' "/etc/hosts"
```

#### Set the Machine Timezone

```shell
sudo timedatectl set-timezone [TIMEZONE]
```

##### Set the Machine Timezone to UTC

```shell
sudo timedatectl set-timezone Etc/UTC
```

#### Generate Machine Locales

```shell
sudo locale-gen
```

##### Generate the Slovak Locale

```shell
sudo sed -i 's/^[#[:space:]]*sk_SK\.UTF-8 UTF-8$/sk_SK.UTF-8 UTF-8/' "/etc/locale.gen"
sudo locale-gen
```

##### Generate the English Locale

```shell
sudo sed -i 's/^[#[:space:]]*en_US\.UTF-8 UTF-8$/en_US.UTF-8 UTF-8/' "/etc/locale.gen"
sudo locale-gen
```

#### Tweak the Kernel Tunable Variables

```shell
TODO: add kernel variables to /etc/sysctl.conf
```

Time Synchronization Using NTP
-------------------------------

[![The Chrony_Project](https://img.shields.io/badge/The_Chrony_Project-indigo?style=flat-square)](https://chrony-project.org/)
[![chrony/chrony](https://img.shields.io/badge/chrony/chrony-555555?style=flat-square&logo=gitlab)](https://gitlab.com/chrony/chrony)

> Debian uses `systemd-timesyncd` by default, which does not continuously synchronize the clock while the system is running.
> `chrony` provides continuous, periodical time synchronization via background daemon.

```shell
sudo apt install chrony
```

```shell
sudo systemctl restart chronyd
```

# Bootstrapping a Debian Workstation

[![Debian_Project](https://img.shields.io/badge/Debian_Project-indigo?style=flat-square)](https://www.debian.org/)
[![Download Debian](https://img.shields.io/badge/Download_Debian-forestgreen?style=flat-square)](https://www.debian.org/distrib/netinst)
[![Debian](https://img.shields.io/badge/Debian-555555?style=flat-square&logo=github)](https://github.com/debian)

Installation
-------------

[![Bootstrapping a Debian Machine](https://img.shields.io/badge/Bootstrapping_a_Debian_Machine-4F46E5?style=flat-square)](https://kb.havlas.me/books/debian-guides/page/bootstrapping-a-debian-machine)

### GNOME Desktop

[![GNOME_Project](https://img.shields.io/badge/GNOME_Project-indigo?style=flat-square)](https://www.gnome.org/)
[![GNOME](https://img.shields.io/badge/GNOME-555555?style=flat-square&logo=gitlab)](https://gitlab.gnome.org/GNOME)

#### Clean Up the Standard Installation

```shell
sudo apt purge gnome-clocks gnome-connections gnome-contacts gnome-maps gnome-music gnome-remote-desktop gnome-snapshot gnome-sound-recorder gnome-tour gnome-user-docs gnome-user-share gnome-weather \
    libreoffice* lynx rygel shotwell simple-scan yelp
```

```shell
sudo apt --purge autoremove
```

#### Install Desktop Goodies

```shell
sudo apt install bmap-tools chromium firefox-esr rsync tcpdump tpm2-tools wakeonlan whois
```

#### Install Development Tools

```shell
sudo apt install git jq make pngquant podman python3-venv qrencode uuid xxd yq
```

```shell
sudo apt install cockpit cockpit-podman
sudo mkdir -p "/etc/systemd/system/cockpit.socket.d"
sudo tee "/etc/systemd/system/cockpit.socket.d/10-localhost.conf" >/dev/null <<'EOF'
[Socket]
ListenStream=
ListenStream=127.0.0.1:9090
EOF
```

```shell
curl --proto '=https' --tlsv1.2 -sSf "https://just.systems/install.sh" | sudo bash -s -- --to "/usr/local/bin"
```

Configuration
--------------

#### Schedule Cron Jobs During Work Hours

```crontab
17 *	* * *	root	cd / && run-parts --report /etc/cron.hourly
25 14	* * *	root	test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.daily; }
47 14	* * 2	root	test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.weekly; }
52 14	1 * *	root	test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.monthly; }
```

# Installing NVIDIA Graphics Drivers

[![Official NVIDIA Drivers](https://img.shields.io/badge/Official_NVIDIA_Drivers-indigo?style=flat-square)](https://www.nvidia.com/en-us/drivers/)

Installation
-------------

```shell
sudo sed -i 's/main/main non-free contrib non-free-firmware/g' "/etc/apt/sources.list"
sudo apt update
```

```shell
sudo apt install linux-headers-$(uname --kernel-release) build-essential dkms nvidia-detect
```

```shell
nvidia-detect
```

```shell
sudo apt install nvidia-driver nvidia-kernel-dkms
```

# Debian Machine Maintenance

[![Debian_Project](https://img.shields.io/badge/Debian_Project-indigo?style=flat-square)](https://www.debian.org/)

Keeping the System Up to Date
------------------------------

```shell
sudo apt update
```

```shell
sudo apt upgrade
```

```shell
sudo apt autoremove
sudo apt autoclean
```

### Upgrade to a New Release

*TODO*: Write when Debian 14 releases.

Cleaning Up Orphaned Conf
-------------------------

```shell
sudo apt purge '~c'
```

Cleaning Up the APT Cache
--------------------------

```shell
sudo apt clean
```

# Networking Cheat Sheet

### Limitting NIC Auto-Negotiation

##### Cap Auto-Negotiation at 10 Mbps

```shell
sudo ethtool -s [NIC] autoneg on advertise 0x002
```

##### Cap Auto-Negotiation at 100 Mbps

```shell
sudo ethtool -s [NIC] autoneg on advertise 0x008
```

##### Cap Auto-Negotiation at 1000 Mbps

```shell
sudo ethtool -s [NIC] autoneg on advertise 0x3F
```

# Security Hardening

# Protecting GRUB with a Password

[![GRUB Project](https://img.shields.io/badge/GRUB_Project-indigo?style=flat-square)](https://www.gnu.org/software/grub/grub.html)

Installation
-------------

```shell
sudo vi "/usr/local/sbin/grub-set-password"
sudo chmod 0755 "/usr/local/sbin/grub-set-password"
```

```shell
#!/bin/sh
set -eu

AUTH_CFG="/boot/grub/auth.cfg"

if [ "$(id -u)" -ne 0 ]; then
    echo "This script must be run as root." >&2
    exit 1
fi

printf 'Enter password: '
stty -echo
read -r password1
stty echo
printf '\n'

printf 'Confirm password: '
stty -echo
read -r password2
stty echo
printf '\n'

if [ "$password1" != "$password2" ]; then
    echo "Passwords do not match, aborting." >&2
    exit 1
fi

# Empty password (confirmed twice) disables protection entirely.
if [ -z "$password1" ]; then
    if [ -f "$AUTH_CFG" ]; then
        rm -f "$AUTH_CFG"
        echo "Empty password entered — GRUB password protection disabled."
    else
        echo "Empty password entered — nothing to do."
    fi
    exit 0
fi

hash=$(printf '%s\n%s\n' "$password1" "$password1" \
    | grub-mkpasswd-pbkdf2 \
    | sed -n 's/^.*is \(grub\.pbkdf2\..*\)$/\1/p')

if [ -z "$hash" ]; then
    echo "Failed to generate password hash." >&2
    exit 1
fi

umask 077
cat > "$AUTH_CFG" <<EOF
GRUB_PASSWORD=$hash
EOF
chmod 0600 "$AUTH_CFG"

echo "GRUB password set. Run 'update-grub' to apply it."
```

```shell
sudo vi "/etc/grub.d/09_password"
sudo chmod 0755 "/etc/grub.d/09_password"
```

```shell
#!/bin/sh
exec tail -n +3 $0

if [ -f $prefix/auth.cfg ]; then
  source $prefix/auth.cfg
  if [ -n "${GRUB_PASSWORD}" ]; then
    set superusers="admin"
    password_pbkdf2 admin ${GRUB_PASSWORD}
    menuentry_id_option="--unrestricted $menuentry_id_option"
  fi
fi
```

Configuration
--------------

> Having submenu poses a security risk as it allows bypassing password protection on sub-items.

```shell file="/etc/default/grub"
GRUB_DISABLE_SUBMENU=true
```

Usage
------

```shell
sudo grub-set-password
```

```shell
sudo update-grub
```

# Enforcing Password Quality

Installation
-------------

```shell
sudo apt install libpam-pwquality
```

Configuration
--------------

> Passwords must be at least 8 characters long, include at least one uppercase letter, one
> lowercase letter, and one digit, this policy is enforced only for local users.

```shell
sudo sed -i 's/^[#[:space:]]*minlen[[:space:]]*=.*/minlen = 8/' "/etc/security/pwquality.conf"
sudo sed -i 's/^[#[:space:]]*dcredit[[:space:]]*=.*/dcredit = -1/' "/etc/security/pwquality.conf"
sudo sed -i 's/^[#[:space:]]*ucredit[[:space:]]*=.*/ucredit = -1/' "/etc/security/pwquality.conf"
sudo sed -i 's/^[#[:space:]]*lcredit[[:space:]]*=.*/lcredit = -1/' "/etc/security/pwquality.conf"
sudo sed -i 's/^[#[:space:]]*minclass[[:space:]]*=.*/minclass = 3/' "/etc/security/pwquality.conf"
sudo sed -i 's/^[#[:space:]]*enforcing[[:space:]]*=.*/enforcing = 1/' "/etc/security/pwquality.conf"
sudo sed -i 's/^[#[:space:]]*local_users_only[[:space:]]*$/local_users_only/' "/etc/security/pwquality.conf"
```

# Verifying Installation Integrity

Installation
-------------

```shell
sudo apt install debsums
```

Configuration
--------------

```shell
sudo sed -i 's/^[#[:space:]]*CRON_CHECK=.*/CRON_CHECK=weekly/' "/etc/default/debsums"
```

Usage
------

#### Run Scan Manually

```shell
sudo debsums --changed
```

# Tweaking a Debian Machine

# Tweaking GRUB

[![GRUB Project](https://img.shields.io/badge/GRUB_Project-indigo?style=flat-square)](https://www.gnu.org/software/grub/grub.html)

### Automatically Boot to Windows

```shell file="/etc/default/grub"
GRUB_DEFAULT="Windows Boot Manager (on /dev/nvme0n1p1)"
```

# Tweaking Bash

### Disable Bash History

```shell
sudo tee "/etc/bash.bashrc" >/dev/null <<'EOF'
unset HISTFILE
export HISTSIZE=0
export HISTFILESIZE=0
EOF
```

# How to Setup a...

# Using KeePassXC as the Secret Service

[![KeePassXC_Project](https://img.shields.io/badge/KeePassXC_Project-indigo?style=flat-square)](https://keepassxc.org/)
[![keepassxreboot/keepassxc](https://img.shields.io/badge/keepassxreboot/keepassxc-555555?style=flat-square&logo=github)](https://github.com/keepassxreboot/keepassxc)

Installation
-------------

```shell
sudo apt install keepassxc
```

#### Create a D-Bus Service That Implements the Secret Service API

```shell
mkdir -p "$HOME/.local/share/dbus-1/services"
```

```shell
tee "$HOME/.local/share/dbus-1/services/org.freedesktop.secrets.service" >/dev/null <<'EOF'
[D-BUS Service]
Name=org.freedesktop.secrets
Exec=/usr/bin/keepassxc
EOF
```

#### Disable the GNOME Keyring Daemon Service

```shell
systemctl --user mask gnome-keyring-daemon.socket
systemctl --user mask gnome-keyring-daemon.service 
sudo systemctl mask gnome-keyring-daemon.socket
sudo systemctl mask gnome-keyring-daemon.service
```

```shell
mkdir -p "$HOME/.config/autostart/"
cp "/etc/xdg/autostart/gnome-keyring-pkcs11.desktop" "$HOME/.config/autostart/"
cp "/etc/xdg/autostart/gnome-keyring-secrets.desktop" "$HOME/.config/autostart/"
cp "/etc/xdg/autostart/gnome-keyring-ssh.desktop" "$HOME/.config/autostart/"
echo "Hidden=true" >> "$HOME/.config/autostart/gnome-keyring-pkcs11.desktop"
echo "Hidden=true" >> "$HOME/.config/autostart/gnome-keyring-secrets.desktop"
echo "Hidden=true" >> "$HOME/.config/autostart/gnome-keyring-ssh.desktop"
```

> Until an appropriate solution is found, resort to removing "executable" permission from `gnome-keyring-daemon`.

```shell
sudo chmod -x /usr/bin/gnome-keyring-daemon
```

```shell
sudo tee "/etc/apt/apt.conf.d/99-disable-gnome-keyring-daemon" >/dev/null <<'EOF'
DPkg::Post-Invoke {
    "if [ -e /usr/bin/gnome-keyring-daemon ]; then chmod -x /usr/bin/gnome-keyring-daemon; fi";
};
EOF
```

#### Unset GNOME Keyring as the Chromium Password Store

```shell
sudo tee --append "/etc/chromium.d/default-flags" >/dev/null <<'EOF'

# Disable GNOME keyring as password store (using keepassxc)
export CHROMIUM_FLAGS="$CHROMIUM_FLAGS --password-store=basic"
EOF
```

Configuration
--------------

- General
  - Basic Settings
    - Startup
      - [x] Start only a single instance of KeePassXC
      - [x] Automatically launch KeePassXC at system startup
      - [x] Minimize window at application startup
      - [ ] Minimize window after unlocking database
      - [x] Remember previously used databases
        - [ ] Load previously open databases on startup
        - [x] Remember database key files and security dongles

Unlocking the Keyring Automatically
------------------------------------

#### Create a KeePassXC Database Protected by Keyfile

```shell
mkdir -p --mode=700 "$HOME/.secrets/databases"
mkdir -p --mode=700 "$HOME/.secrets/private"
keepassxc-cli db-create --decryption-time 1000 --set-key-file "$HOME/.secrets/private/SecretService.keyx" "$HOME/.secrets/databases/SecretService.kdbx"
```

##### Encrypt the Keyfile Using the TPM2-Sealed Key

```shell
sudo systemd-creds encrypt --name=KeePassXC-SecretService --with-key=tpm2 "$HOME/.secrets/SecretService.keyx" "$HOME/.secrets/SecretService.creds"
sudo chown $USER:$USER "$HOME/.secrets/SecretService.creds"
sudo chmod 0400 "$HOME/.secrets/SecretService.creds"
```

> Ensure you have a backup copy of the keyfile stored securely before shredding it — there is no recovery option if the TPM-sealed key becomes unusable (e.g. due to a firmware update).

```shell
shred "$HOME/.secrets/SecretService.keyx"
```

#### Authorize Credentials Decryption Using the TPM2-Sealed Key

```shell
sudo tee "/etc/polkit-1/rules.d/49-systemd-creds.rules" >/dev/null <<'EOF'
polkit.addRule(function(action, subject) {
    if (action.id == "io.systemd.credentials.decrypt" &&
        subject.local == true && subject.active == true &&
        subject.isInGroup ("tss")) {
            return polkit.Result.YES;
    }
});
EOF
```

```shell
sudo gpasswd --add <username> tss
```

#### Set Up Automatic Unlock on Login

```shell
sudo wget --quiet -O "/usr/local/libexec/keepassxc-watch.sh" "https://kb.havlas.me/attachments/16"
sudo chown root:root "/usr/local/libexec/keepassxc-watch.sh"
sudo chmod 0755 "/usr/local/libexec/keepassxc-watch.sh"
```

```shell
sudo tee "/etc/systemd/user/keepassxc-watch@.service" >/dev/null <<'EOF'
[Unit]
Description=Auto-unlock KeePassXC database '%i' via TPM2-sealed key
After=graphical-session.target
ConditionPathExists=%h/.secrets/%i.kdbx
ConditionPathExists=%h/.secrets/%i.creds

[Service]
ExecStart=/usr/local/libexec/keepassxc-watch.sh %i
Restart=on-failure
RestartSec=2

[Install]
WantedBy=graphical-session.target
EOF
```

```shell
sudo chown root:root "/etc/systemd/user/keepassxc-watch@.service"
sudo chmod 0644 "/etc/systemd/user/keepassxc-watch@.service"
sudo systemctl daemon-reload
```

```shell
systemctl --user enable --now keepassxc-watch@SecretService.service
```

##### SSH Keys

```shell
sudo apt install seahorse
sudo mkdir -p /usr/libexec/ssh
sudo ln -s ../seahorse/ssh-askpass /usr/libexec/ssh/ssh-askpass
```

```desktop file=".local/share/applications/ssh-askpass.desktop
[Desktop Entry]
Type=Application
Name=SSH ask-pass
Exec=/usr/libexec/ssh/ssh-askpass
StartupWMClass=ssh-askpass
NoDisplay=true
```

```shell
systemctl edit --user ssh-agent.service
```

```systemd
[Service]
#Environment=DISPLAY=:0
Environment=SSH_ASKPASS=/usr/libexec/ssh/ssh-askpass
```

```shell
systemctl --user daemon-reload
systemctl --user restart ssh-agent.service
```

# How to Setup a YubiKey Authentication

[![Yubico](https://img.shields.io/badge/Yubico-indigo?style=flat-square)](https://www.yubico.com/)
[![Yubico](https://img.shields.io/badge/Yubico-555555?style=flat-square&logo=github)](https://github.com/yubico)
[![documentation](https://img.shields.io/badge/documentation-forestgreen?style=flat-square)](https://www.yubico.com/works-with-yubikey/catalog/linux/)
[![Buy at Alza.sk](https://img.shields.io/badge/Shop-Alza.sk-royalblue?style=flat-square)](https://www.alza.sk/bezpecnostne-tokeny/yubico/18894404-v10060.htm)

Installation
-------------

```shell
sudo apt install fido2-tools libpam-u2f pcscd
```

```shell
sudo apt install yubikey-manager yubikey-personalization
```

Configuration
--------------

#### Set YubiKey PIN

```shell
ykman fido access change-pin
```

#### Disable YubiKey OTP Protocol

```shell
ykman config usb --disable OTP
```

Setup GNOME/PAM Authentication
-------------------------------

```txt filename="/etc/pam.d/common-yubico
auth       sufficient   pam_u2f.so cue [cue_prompt=🔑 Tap the security key now...] openasuser userpresence=1
```

```txt filename="/etc/pam.d/sudo"
@include common-yubico
```

```txt filename="/etc/pam.d/sudo-i"
@include common-yubico
```

```txt filename="/etc/pam.d/gdm-password"
auth       sufficient   pam_u2f.so nodetect cue [cue_prompt=🔑 Tap the security key now...] openasuser
```

```shell
mkdir -p ~/.config/Yubico
pamu2fcfg > ~/.config/Yubico/u2f_keys
```


Setup Full Disk Encryption
---------------------------

```shell
sudo apt install dracut dracut-core
```

```shell
sudo tee "/etc/dracut.conf.d/60hostonly.conf" >/dev/null <<'EOF'
hostonly=yes
hostonly_cmdline=yes
EOF
```

```shell
sudo dracut --regenerate-all --force
```

```shell
sudo systemd-cryptenroll [PARTITION] --fido2-device=auto --fido2-with-client-pin=no
```

# Troubleshooting

# Missing Disk Space?

#### Check Deleted but Still Opened Files

```shell
lsof | grep '(deleted)'
```