20 KiB
Installing Aptly on Debian
Table of Contents
Introduction
Welcome to the installation guide for Aptly on Debian in a Proxmox LXC container. Aptly is a powerful Debian repository management tool that can help you manage your custom APT repositories efficiently.
Why Aptly?
Aptly simplifies Debian package management and allows you to:
- Create local APT repositories.
- Mirror remote repositories.
- Take consistent snapshots of repositories.
- Publish snapshot repositories.
Prerequisites
Before you begin the installation process, ensure that your Debian system meets the following requirements:
- Debian GNU/Linux 9 (Stretch) or later
- Access to a terminal with sudo privileges
- Basic familiarity with the command line interface
- Stable internet connection to download necessary packages
What This Guide Covers
This guide will walk you through the installation of Aptly on Debian step by step, covering:
- Installation: Installing Aptly from the official repository or using a binary release.
- Configuration: Configuring Aptly to suit your environment and preferences.
- Setup: Setting up Aptly as a service and accessing it.
Placeholders
Replace the placeholders below with the appropriate values for your setup:
-
User Details
- Username:
<username>(e.g., admin) - Username - Hypervisor:
<username-hypervisor>(e.g., admin) - User full name:
<user-full-name>(e.g., John Doe) - User email address:
<user-email-address>(e.g., johndoe@example.com)
- Username:
-
Server Configuration
- Server IP address:
<server-ip-address>(e.g., 192.168.1.100) - Hostname - Intranet:
<hostname-intranet>(e.g., aptly-server.domain.com) - Hostname - Internet:
<hostname-internet>(e.g., aptly.domain.com) - Hostname - Hypervisor:
<hostname-hypervisor>(e.g., proxmox-hypervisor.domain.com) - Hostname - Hypervisor NAS:
<hostname-hypervisor-nas>(e.g., nas-server.domain.com) - Name - Hypervisor NAS:
<name-hypervisor-nas>(e.g., nas-server) - Container ID - Hypervisor:
<container-id-hypervisor>(e.g., 100)
- Server IP address:
-
SSH Keys
- SSH key - Proxmox:
<ssh-key-proxmox>(e.g., /home/user/.ssh/id_rsa.pub) - SSH key - Client:
<ssh-key-client>(e.g., /home/user/.ssh/client_id_rsa.pub)
- SSH key - Proxmox:
-
Networking
- Wireguard port:
<wireguard-port>(e.g., 51820)
- Wireguard port:
-
Repository Details
- Example repository name:
<example-repository-name>(e.g., myrepository) - Example package base:
<example-package-base>(e.g., mypackage_1.1.1) - Example package Debian version:
<example-package-debian-version>(e.g., mypackage_1.1.1-1) - GPG repository key id:
<gpg-repository-key-id>(e.g., ABCD1234EFGH5678)
- Example repository name:
-
Paths
- Path index:
<path-index>(e.g., /var/www/html) - Path images:
<path-images>(e.g., /var/www/static/) - Path errors:
<path-errors>(e.g., /var/www/errors)
- Path index:
Important Warnings and Security Practices
Before executing any commands in this documentation, please adhere to the following guidelines to ensure the security and integrity of the system:
- Execute Commands with Caution: Always review and understand a command before executing it. Misuse of commands can lead to data loss or system instability.
- Backup Command Execution: The backup command must be executed only by authorized users. Ensure that proper permissions are set to prevent unauthorized access to backup files.
- Regular Backups: Maintain regular backups of all critical data. It is advisable to use automated backup solutions and verify backup integrity periodically.
- System Updates: Regularly update the system and all installed packages to protect against vulnerabilities. Use the package manager responsibly to avoid potential conflicts.
- Monitor System Logs: Continuously monitor system logs for any unusual activity. Use logging tools to help identify potential security breaches or system failures.
- User Permissions: Ensure that user permissions are strictly managed. Limit access to sensitive commands and data to only those who need it to perform their job functions.
- Network Security: Implement proper network security measures, such as firewalls and intrusion detection systems, to protect against external threats.
- Data Encryption: Encrypt sensitive data at rest and in transit to prevent unauthorized access.
By following these practices, you will help maintain the security and stability of the system while minimizing the risk of data loss or compromise.
Useful Commands and Information
Documentation
Links
- Aptly Repository
Software on the Machine
- Operating System: Debian
- Web Server: Nginx
- Security: GnuPG, WireGuard, UFW
- Other: Git, sudo
Paths
- Aptly Configuration:
/home/aptly/.aptly.conf - Aptly Work Path:
/home/aptly/.aptly/
Proxmox Commands
List available Proxmox templates
ssh <username>@<hostname-hypervisor-nas> "ls /mnt/proxmox/template/cache/"
Create the container
ssh <username-hypervisor>@<hostname-hypervisor> "pct create <container-id-hypervisor> <name-hypervisor-nas>:vztmpl/debian-12-upgraded_12.5_amd64.tar.zst --hostname <hostname-intranet> --cores 2 --memory 2048 --swap 2048 --net0 name=net0,bridge=vmbr0,ip=dhcp,firewall=1 --rootfs <name-hypervisor-nas>:250 --unprivileged 1 --features nesting=1 --ssh-public-keys <ssh-key-proxmox> --start 1"
Backup
ssh <username-hypervisor>@<hostname-hypervisor> "vzdump <container-id-hypervisor> --compress zstd --mode stop --storage <name-hypervisor-nas> --note \"$(date +'%Y-%m-%d %H:%M') Backup fresh install\""
Set the state of the Proxmox HA Manager for Container
ssh <username-hypervisor>@<hostname-hypervisor> "ha-manager add ct:<container-id-hypervisor>"
ssh <username-hypervisor>@<hostname-hypervisor> "ha-manager remove ct:<container-id-hypervisor>"
Set the state and limits of the Proxmox Container in the HA Manager
ssh <username-hypervisor>@<hostname-hypervisor> "ha-manager set ct:<container-id-hypervisor> --state started --max_relocate 3 --max_restart 3"
ssh <username-hypervisor>@<hostname-hypervisor> "ha-manager set ct:<container-id-hypervisor> --state stopped"
ssh <username-hypervisor>@<hostname-hypervisor> "pct reboot <container-id-hypervisor>"
Destroy the Proxmox Container forcefully
ssh <username-hypervisor>@<hostname-hypervisor> "pct destroy <container-id-hypervisor> --force --purge"
Move the Proxmox Container to another host
ssh <username-hypervisor>@<hostname-hypervisor> "pct migrate <container-id-hypervisor> hv2"
SSH Connection
Connection with specific keys
ssh -i <ssh-key-client> root@<hostname-intranet>
ssh -i <ssh-key-client> root@<server-ip-address>
ssh -i <ssh-key-client> <username>@<hostname-intranet>
ssh -i <ssh-key-client> <username>@<server-ip-address>
Remove offending keys from known_hosts
ssh-keygen -f "/home/<username>/.ssh/known_hosts" -R "<hostname-intranet>"
ssh-keygen -f "/home/<username>/.ssh/known_hosts" -R "<server-ip-address>"
Copy SSH public key to remote host
ssh-copy-id -i <ssh-key-client> root@<server-ip-address>
ssh-copy-id -i <ssh-key-client> root@<hostname-intranet>
ssh-copy-id -i <ssh-key-client> <username>@<server-ip-address>
ssh-copy-id -i <ssh-key-client> <username>@<hostname-intranet>
Transfer SSH keys and files
mkdir -p /home/aptly/.ssh/
cp /home/<username>/.ssh/authorized_keys /home/aptly/.ssh/authorized_keys
chown -R aptly:aptly /home/aptly/.ssh/
Installation Procedure
-
Fresh Debian Installation
- Install a fresh Debian operating system on your new server following the standard installation procedure.
-
Backup before starting
ssh <username-hypervisor>@<hostname-hypervisor> "vzdump <container-id-hypervisor> --compress zstd --mode stop --storage <name-hypervisor-nas> --note \"$(date +'%Y-%m-%d %H:%M') Backup fresh install\"" -
Install Required Dependencies
Upgrade the base system
apt update apt upgrade -yInstall dependencies
apt install -y sudo gnupg wget nginx git wireguard-tools ufw -
Ensure Hostname
nano /etc/hosts # Add line: 127.0.1.1 <hostname-intranet> nano /etc/hostname # Set to: <hostname-intranet> hostnamectl set-hostname <hostname-intranet> -
Add Users
adduser --gecos "<user-full-name>,,,<user-email-address>" <username> usermod -aG sudo <username> adduser --system --shell /bin/bash --gecos 'Aptly repository' --group --disabled-password --home /home/aptly aptly -
Setup SSH Connectors
- Configure SSH connectors as per your setup script to establish secure connections to the server.
-
Test users, SSH, and sudo
- Transfer SSH keys for User
- Connect as User with SSH key
- Test sudo
sudo su -- Disconnect as root
-
Secure SSH
nano /etc/ssh/sshd_configPermitRootLogin no PasswordAuthentication no ChallengeResponseAuthentication noRestart SSH
systemctl restart ssh -
Configure Firewall
Open ports
ufw allow OpenSSH ufw allow http ufw allow https ufw allow <wireguard-port>/udpEnable firewall
ufw enable -
Add the Aptly Repository
Find the latest instructions at Aptly Installations.
Import the GPG key
mkdir -p /etc/apt/keyrings chmod 755 /etc/apt/keyrings wget -O /etc/apt/keyrings/aptly.asc https://www.aptly.info/pubkey.txtAdd the Aptly repository
echo "deb [signed-by=/etc/apt/keyrings/aptly.asc] http://repo.aptly.info/ squeeze main" | tee /etc/apt/sources.list.d/aptly.list -
Install Aptly
After adding the repository, update your package list and install Aptly:
apt update apt install -y aptly -
Configure Aptly
Initialize the Aptly Directory
aptly repo create -distribution=stable -component=main <example-repository-name>-stable aptly repo create -distribution=testing -component=main <example-repository-name>-testingAdding Packages
sudo -iu aptly mkdir /home/aptly/packages/scp <example-package-debian-version>_all.deb <example-package-debian-version>.dsc <example-package-base>.orig.tar.gz <example-package-debian-version>.debian.tar.xz <example-package-debian-version>_amd64.changes aptly@<hostname-intranet>:/home/aptly/packages/To add packages to your local repository:
aptly repo add <example-repository-name>-stable /home/aptly/packages/ aptly repo add <example-repository-name>-testing /home/aptly/packages/Generate and Transfer GPG Keys
gpg --full-generate-key gpg --list-keys gpg --export-secret-keys --armor <gpg-repository-key-id> > ~/<hostname-internet>.private-key.asc gpg --export --armor <gpg-repository-key-id> > ~/<hostname-internet>.public-key.ascscp ~/<hostname-internet>.private-key.asc aptly@<hostname-internet>:/home/aptly/ rm ~/<hostname-internet>.private-key.ascRegister keys with public keyservers
gpg --send-keys <gpg-repository-key-id> gpg --keyserver hkp://keyserver.ubuntu.com --send-keys <gpg-repository-key-id> gpg --keyserver hkp://pgp.mit.edu --send-keys <gpg-repository-key-id>Import GPG Key
sudo -iu aptly gpg --import /home/aptly/<hostname-internet>.private-key.asc gpg --edit-key <gpg-repository-key-id> # trust rm ~/<hostname-internet>.private-key.ascAdd Key to Aptly Config
Edit
~/.aptly.conf:{ "gpgKey": "<gpg-repository-key-id>" }Publishing the Repository
After adding packages, you can publish the repository:
aptly publish repo -architectures="amd64,i386" <example-repository-name>-stable aptly publish repo -architectures="amd64,i386" <example-repository-name>-testingThis will create a new directory with the published APT repository, which can be served via HTTP.
Snapshot the repositories
sudo -iu aptly # Get the current date in YYYY-MM-DD format current_date=$(date +%F) # Define your repository names repository_name="<example-repository-name>" # Create snapshots with automatic names aptly snapshot create "${repository_name}-stable-${current_date}" from repo "${repository_name}-stable" aptly snapshot create "${repository_name}-testing-${current_date}" from repo "${repository_name}-testing" -
nginx Configuration
-
Configure nginx to run as aptly
nano /etc/nginx/nginx.confuser aptly; -
Backup Default Files Backup the default index.html and configuration files.
mv /var/www/html /var/www/.html mv /etc/nginx/sites-available/default /etc/nginx/sites-available/.default rm /etc/nginx/sites-enabled/default -
Create source ssl certificate
mkdir -p /etc/nginx/ssl/ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/<hostname-internet>.key -out /etc/nginx/ssl/<hostname-internet>.crt -
Create Default Server Block for Unauthorized Access
nano /etc/nginx/sites-available/unauthorizedserver { listen 80 default_server; listen [::]:80 default_server; server_name _; error_page 403 /403.html; location = /403.html { root <path-errors>; internal; } error_page 404 /404.html; location = /404.html { root <path-errors>; internal; } error_page 500 /500.html; location = /500.html { root <path-errors>; internal; } location / { return 403; } } -
Site configuration
Edit the Nginx configuration file to point to the Aptly repository. Open the file:
nano /etc/nginx/sites-available/<hostname-internet>.confAdd the following configuration:
server { listen 80; listen [::]:80; server_name <hostname-internet>; # Redirect all HTTP traffic to HTTPS return 301 https://$host$request_uri; } server { listen 443 ssl; listen [::]:443 ssl; server_name <hostname-internet>; ssl_certificate /etc/nginx/ssl/<hostname-internet>.crt; # Path to the SSL certificate ssl_certificate_key /etc/nginx/ssl/<hostname-internet>.key; # Path to the SSL certificate key # Optional: Add HSTS for enhanced security add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; # Security headers for additional protection add_header X-Content-Type-Options nosniff; add_header X-Frame-Options SAMEORIGIN; add_header X-XSS-Protection "1; mode=block"; # GPG public key location location = /<hostname-internet>-public-key.asc { root /home/aptly/.aptly/public; # Directory containing the public key try_files /<hostname-internet>-public-key.asc =404; # Serve the public key or return 404 } location = /<hostname-internet>-public-key.gpg { root /home/aptly/.aptly/public; # Directory containing the GPG key try_files /<hostname-internet>-public-key.gpg =404; # Serve the GPG key or return 404 } # Debian repository locations with directory listing enabled location /dists/ { alias /home/aptly/.aptly/public/dists/; # Repository distribution files autoindex on; # Enable directory listing } location /pool/ { alias /home/aptly/.aptly/public/pool/; # Repository package files autoindex on; # Enable directory listing } # Default page served from the specified root directory location = / { root <path-index>; # Directory for the default page try_files /<hostname-internet>.html =404; # Serve the default page or return 404 } # Static images served without directory listing location /images/ { alias <path-images>; # Directory for static images autoindex off; # Disable directory listing } # Custom error pages for improved user experience error_page 403 /403.html; # Custom 403 error page location = /403.html { root <path-errors>; # Directory for error pages internal; # Prevent direct access } error_page 404 /404.html; # Custom 404 error page location = /404.html { root <path-errors>; # Directory for error pages internal; # Prevent direct access } error_page 500 /500.html; # Custom 500 error page location = /500.html { root <path-errors>; # Directory for error pages internal; # Prevent direct access } # Deny access to hidden files and specific file types location ~ /\. { deny all; # Deny access to hidden files } } -
Enabling the new VirtualHost
ln -s /etc/nginx/sites-available/unauthorized /etc/nginx/sites-enabled/ ln -s /etc/nginx/sites-available/<hostname-internet>.conf /etc/nginx/sites-enabled/ -
Test nginx configuration
nginx -t -
Reloading nginx nginx reload
systemctl reload nginxOptional nginx restart
systemctl restart nginx
Now, your Aptly repository is available at
http://<hostname-internet>. -
-
Back-up post installation
ssh <username-hypervisor>@<hostname-hypervisor> "ha-manager set ct:<container-id-hypervisor> --state stopped" ssh <username-hypervisor>@<hostname-hypervisor> "vzdump <container-id-hypervisor> --compress zstd --mode stop --storage <name-hypervisor-nas> --note \"$(date +'%Y-%m-%d %H:%M') Backup post installation\"" -
Start the server
ssh <username-hypervisor>@<hostname-hypervisor> "ha-manager set ct:<container-id-hypervisor> --state started --max_relocate 3 --max_restart 3"