You’ve been asked to stand up a private container registry. The team ran Harbor’s install.sh against a self-signed cert two years ago and moved on. Today, the cert expired four months back so every docker pull warns about TLS, the embedded PostgreSQL was never backed up because no one knew where it lived, the registry volume passed 80% of the data disk last week and started 502-ing during pushes, image scanning was never enabled, and the only signed images in the system are the ones from the public mirror.
What’s happening
A Harbor deployment that is actually production-ready needs more than install.sh. The hostname must be a real FQDN with a valid Let’s Encrypt cert (modern clients refuse plain HTTP for v2 push/pull); Trivy is enabled at install so every push gets a CVE scan; Notary v1 is wired in if you want signed image policies; the embedded PostgreSQL and Redis listen only on the Compose internal network; the data volume sits on a path the VM-level snapshot covers; the GC schedule is on or the registry grows until the disk fills; and the admin password from install is rotated immediately into the secrets manager. Harbor is itself a Compose stack — the host needs unattended-upgrades and log rotation accordingly.
How to verify
After install, the listeners should look like this:
ss -lntp | grep -E ':(80|443|4443)\b'
# 0.0.0.0:80 nginx-inside-harbor (HTTP -> HTTPS redirect)
# 0.0.0.0:443 nginx-inside-harbor (TLS)
# 0.0.0.0:4443 notary-server (image signing)
The v2 API should answer 200 after auth:
curl -fsS -u admin:CHANGEME https://harbor.example.com/v2/_catalog
And a docker login plus a push of any small image should round-trip cleanly:
docker login harbor.example.com
docker pull alpine:3.20 && docker tag alpine:3.20 harbor.example.com/library/alpine:3.20
docker push harbor.example.com/library/alpine:3.20
After the push, the Harbor UI for that image should show a Trivy scan result within a minute or two; if it stays “Not Scanned” indefinitely, the Trivy adapter isn’t running.
The fix
Save the script below as /root/setup-harbor-ubuntu22.sh, then run it. It runs on a fresh Ubuntu 22.04 host: apt update, docker engine + compose plugin, unattended-upgrades, sysctl/FD tuning, SSH hardening, a data path under /data, certbot standalone for the FQDN, a verified download of the Harbor offline installer, a generated harbor.yml with Trivy and Notary enabled, and a wait loop for the v2 endpoint before declaring success.
#!/usr/bin/env bash
set -euo pipefail
### Ubuntu 22.04 Harbor clean host installer
### Harbor offline installer with Trivy and Notary enabled
### Keeps root SSH key login enabled
### Disables SSH password login
### Enables unattended-upgrades for security
### Let's Encrypt via standalone certbot (port 80 free at install time)
### No local backups; assumes VM-level backup of /var/lib/harbor and /data
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
: "${HARBOR_DOMAIN:?Set HARBOR_DOMAIN (e.g. harbor.example.com)}"
: "${LE_EMAIL:?Set LE_EMAIL}"
HARBOR_VERSION="${HARBOR_VERSION:-v2.11.1}"
HARBOR_ADMIN_PASSWORD="$(openssl rand -base64 24)"
HARBOR_DB_PASSWORD="$(openssl rand -hex 24)"
HARBOR_DATA_VOLUME="/data"
HARBOR_INSTALL_DIR="/opt/harbor"
echo
echo "========================================"
echo "Harbor Ubuntu 22.04 Setup"
echo "Domain: ${HARBOR_DOMAIN}"
echo "Version: ${HARBOR_VERSION}"
echo "========================================"
echo
export DEBIAN_FRONTEND=noninteractive
echo "==> Updating base OS"
apt-get update
apt-get -y upgrade
apt-get -y install \
ca-certificates curl gnupg lsb-release \
vim chrony fail2ban unattended-upgrades \
iproute2 lsof tcpdump dnsutils tar gzip unzip jq
echo "==> Enabling time sync"
systemctl enable --now chrony
timedatectl set-ntp true
echo "==> Applying sysctl tuning"
cat > /etc/sysctl.d/99-harbor.conf <<'SYSCTL'
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
net.core.somaxconn = 4096
net.core.netdev_max_backlog = 250000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.ip_local_port_range = 10240 65535
kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
fs.file-max = 2097152
vm.overcommit_memory = 1
vm.max_map_count = 262144
SYSCTL
modprobe tcp_bbr || true
echo tcp_bbr > /etc/modules-load.d/bbr.conf
sysctl --system
echo "==> Setting file limits"
cat > /etc/security/limits.d/99-harbor.conf <<'LIMITS'
* soft nofile 64000
* hard nofile 64000
root soft nofile 64000
root hard nofile 64000
LIMITS
echo "==> Hardening SSH"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S)
set_sshd_option() {
local key="$1"
local value="$2"
if grep -qE "^[#[:space:]]*${key}[[:space:]]+" /etc/ssh/sshd_config; then
sed -i "s|^[#[:space:]]*${key}[[:space:]].*|${key} ${value}|" /etc/ssh/sshd_config
else
echo "${key} ${value}" >> /etc/ssh/sshd_config
fi
}
set_sshd_option PermitRootLogin prohibit-password
set_sshd_option PasswordAuthentication no
set_sshd_option PubkeyAuthentication yes
set_sshd_option KbdInteractiveAuthentication no
set_sshd_option X11Forwarding no
set_sshd_option ClientAliveInterval 300
set_sshd_option ClientAliveCountMax 2
set_sshd_option MaxAuthTries 4
sshd -t
systemctl restart ssh
echo "==> Configuring fail2ban"
mkdir -p /etc/fail2ban/jail.d
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN
systemctl enable --now fail2ban
echo "==> Enabling unattended-upgrades for security"
cat > /etc/apt/apt.conf.d/20auto-upgrades <<'AUTOUPG'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::AutocleanInterval "7";
AUTOUPG
echo "==> Installing Docker engine and compose plugin"
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" \
> /etc/apt/sources.list.d/docker.list
apt-get update
apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
cat > /etc/docker/daemon.json <<'DOCKERJSON'
{
"log-driver": "json-file",
"log-opts": { "max-size": "100m", "max-file": "5" },
"live-restore": true,
"default-ulimits": { "nofile": { "Name": "nofile", "Hard": 64000, "Soft": 64000 } }
}
DOCKERJSON
systemctl enable --now docker
systemctl restart docker
echo "==> Preparing Harbor data dir at ${HARBOR_DATA_VOLUME}"
install -d -o root -g root -m 0755 "${HARBOR_DATA_VOLUME}"
echo "==> Installing certbot and obtaining cert (standalone, port 80 must be free)"
apt-get -y install certbot
certbot certonly --standalone \
-d "${HARBOR_DOMAIN}" --email "${LE_EMAIL}" \
--agree-tos --no-eff-email --non-interactive \
--preferred-challenges http
CERT_LIVE_DIR="/etc/letsencrypt/live/${HARBOR_DOMAIN}"
if [[ ! -f "${CERT_LIVE_DIR}/fullchain.pem" ]]; then
echo "Let's Encrypt did not deliver a cert at ${CERT_LIVE_DIR}"
exit 1
fi
systemctl enable --now certbot.timer
echo "==> Downloading Harbor ${HARBOR_VERSION} offline installer"
install -d -m 0755 "${HARBOR_INSTALL_DIR}"
cd "${HARBOR_INSTALL_DIR}"
TARBALL="harbor-offline-installer-${HARBOR_VERSION}.tgz"
curl -fsSL -O "https://github.com/goharbor/harbor/releases/download/${HARBOR_VERSION}/${TARBALL}"
curl -fsSL -O "https://github.com/goharbor/harbor/releases/download/${HARBOR_VERSION}/${TARBALL}.asc"
# Verify signature with the Harbor signing key
curl -fsSL https://raw.githubusercontent.com/goharbor/harbor/main/KEYS | \
gpg --import - 2>/dev/null || true
gpg --batch --verify "${TARBALL}.asc" "${TARBALL}" || \
echo "WARNING: GPG verification did not complete; proceed only if you trust the download path."
tar xzf "${TARBALL}"
cd harbor
echo "==> Writing harbor.yml"
cat > harbor.yml <<HARBORYML
hostname: ${HARBOR_DOMAIN}
http:
port: 80
https:
port: 443
certificate: ${CERT_LIVE_DIR}/fullchain.pem
private_key: ${CERT_LIVE_DIR}/privkey.pem
external_url: https://${HARBOR_DOMAIN}
harbor_admin_password: ${HARBOR_ADMIN_PASSWORD}
database:
password: ${HARBOR_DB_PASSWORD}
max_idle_conns: 50
max_open_conns: 500
data_volume: ${HARBOR_DATA_VOLUME}
trivy:
ignore_unfixed: false
skip_update: false
insecure: false
jobservice:
max_job_workers: 10
notification:
webhook_job_max_retry: 3
log:
level: info
local:
rotate_count: 50
rotate_size: 200M
location: /var/log/harbor
_version: 2.11.0
proxy:
http_proxy:
https_proxy:
no_proxy:
components:
- core
- jobservice
- trivy
upload_purging:
enabled: true
age: 168h
interval: 24h
dryrun: false
HARBORYML
chmod 600 harbor.yml
echo "==> Running Harbor installer with Trivy and Notary"
./install.sh --with-trivy --with-notary
echo "==> Waiting for Harbor v2 API"
for i in {1..120}; do
if curl -fsS -k "https://127.0.0.1/api/v2.0/health" >/dev/null 2>&1; then
break
fi
sleep 2
done
if ! curl -fsS -k "https://127.0.0.1/api/v2.0/health" >/dev/null 2>&1; then
echo "Harbor v2 API never answered"
( cd "${HARBOR_INSTALL_DIR}/harbor" && docker compose ps )
exit 1
fi
echo "==> Installing systemd unit for Harbor"
cat > /etc/systemd/system/harbor.service <<UNIT
[Unit]
Description=Harbor (Compose)
Requires=docker.service
After=docker.service network-online.target
Wants=network-online.target
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=${HARBOR_INSTALL_DIR}/harbor
ExecStart=/usr/bin/docker compose up -d
ExecStop=/usr/bin/docker compose down
LimitNOFILE=64000
[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable harbor
echo "==> Adding certbot deploy hook to re-copy cert into Harbor on renewal"
mkdir -p /etc/letsencrypt/renewal-hooks/deploy
cat > /etc/letsencrypt/renewal-hooks/deploy/harbor.sh <<HOOK
#!/usr/bin/env bash
set -euo pipefail
cd ${HARBOR_INSTALL_DIR}/harbor
docker compose down
./install.sh --with-trivy --with-notary
HOOK
chmod 700 /etc/letsencrypt/renewal-hooks/deploy/harbor.sh
echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Kernel:"
uname -r
echo
echo "Services:"
systemctl is-active docker harbor chrony fail2ban || true
echo
echo "Listeners:"
ss -lntp | grep -E ':(80|443|4443)\b' || true
echo
echo "Harbor compose state:"
( cd "${HARBOR_INSTALL_DIR}/harbor" && docker compose ps )
echo
echo "Web UI:"
echo "https://${HARBOR_DOMAIN}/"
echo
echo "Admin credentials (rotate immediately after first login):"
echo " user: admin"
echo " password: ${HARBOR_ADMIN_PASSWORD}"
echo
echo "Save the admin password and DB password (also in /opt/harbor/harbor/harbor.yml) to your secrets manager,"
echo "then chmod 600 the file and consider rotating both via the UI before the first push."
Make it executable and run it:
chmod 700 /root/setup-harbor-ubuntu22.sh
HARBOR_DOMAIN=harbor.example.com LE_EMAIL=ops@example.com /root/setup-harbor-ubuntu22.sh
The script generates a random admin password and prints it once at the end — capture it into your secrets manager, log in, rotate it through the UI, then enable a GC schedule under Administration -> Garbage Collection (weekly is fine for most teams) and a vulnerability policy under Configuration -> Vulnerability. Host-level backups are out of scope: /data and /opt/harbor/harbor/harbor.yml are the stateful paths, and the VM is assumed to have snapshot/backup at the cloud layer.
The container registry is the bottom of the supply chain — pair Harbor with the deploy pipeline that pushes to it on a Stack Harbor-managed environment: Cloud and PaaS hosting →.