A fresh VM boots on an isolated virtual network, you run apt update, and it hangs on Could not connect to archive.ubuntu.com. The VM works. It just has no route out, by design.
Here is a pattern for that situation: a small proxy VM as the only way out, and the client proxy settings delivered through contextualization instead of being set by hand.
1. The layout
- A private virtual network, for example
10.10.0.0/24, with no NAT to the outside. - One gateway VM with a leg on that network and a leg with internet access. It runs Squid on port 3128.
- All other VMs reach the internet only through
http://10.10.0.1:3128.
2. Squid with an allowlist
Minimal /etc/squid/squid.conf on the gateway VM:
http_port 3128
acl vmnet src 10.10.0.0/24
acl allowed_sites dstdomain .debian.org .ubuntu.com .fedoraproject.org
acl Safe_ports port 80 443
acl SSL_ports port 443
acl CONNECT method CONNECT
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow vmnet allowed_sites
http_access deny all
This allowlist will be too short for you. Many mirrors redirect to CDN or vendor domains that are not in that list. Expect denials the first time. Find out what was blocked:
grep TCP_DENIED /var/log/squid/access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head
Add the domains you actually need, then reload with squid -k reconfigure. Build the list from your own logs, not from a blog post.
3. Push the proxy settings with contextualization
Put the proxy address in the VM template as a custom context variable. Add the setup script as START_SCRIPT_BASE64, which avoids nested quote escaping.
Save this as proxy-setup.sh:
#!/bin/sh
set -eu
[ -n "${PROXY_URL:-}" ] || exit 0
cat > /etc/profile.d/proxy.sh <<EOF
export http_proxy=$PROXY_URL
export https_proxy=$PROXY_URL
export no_proxy=localhost,127.0.0.1
EOF
# Debian / Ubuntu
if [ -d /etc/apt/apt.conf.d ]; then
printf 'Acquire::http::Proxy "%s";\nAcquire::https::Proxy "%s";\n' "$PROXY_URL" "$PROXY_URL" > /etc/apt/apt.conf.d/90proxy
fi
# RHEL family / Fedora
if [ -f /etc/dnf/dnf.conf ] && ! grep -q '^proxy=' /etc/dnf/dnf.conf; then
echo "proxy=$PROXY_URL" >> /etc/dnf/dnf.conf
fi
Encode it on the frontend:
base64 -w0 proxy-setup.sh
Paste the output into the template:
CONTEXT = [
NETWORK = "YES",
SSH_PUBLIC_KEY = "$USER[SSH_PUBLIC_KEY]",
PROXY_URL = "http://10.10.0.1:3128",
START_SCRIPT_BASE64 = "<output of base64 -w0 proxy-setup.sh>"
]
The context variables should be visible to the script as environment variables. If PROXY_URL comes up empty on your image, check what the guest received:
cat /var/run/one-context/one_env
Then test from the guest:
apt-get update # Debian / Ubuntu
dnf makecache # RHEL family / Fedora
4. Make the proxy the only way out
Proxy settings are advisory. A VM can ignore them. To enforce the rule, restrict outbound traffic at the network level so that only the proxy IP on TCP 3128 is reachable. OpenNebula Security Groups can do this on virtual networks, but support depends on your version and network driver. Check the documentation for your release before you rely on them.
5. Mistakes I would avoid
- Allowing
CONNECTto any port. Keep it to 443. - Leaving Squid open to every source. Always scope
srcto the VM network. - Forgetting
no_proxyfor internal services such as your registry or monitoring endpoint. - Treating the allowlist as finished. Review
TCP_DENIEDentries regularly.
Disclosure
I am writing this on behalf of vpsttt, which offers proxy and VPS products. Nothing above depends on them. The setup uses plain Squid.
Which do you prefer for egress control on OpenNebula: a proxy VM like this, NAT with Security Groups, or something else? And which OpenNebula version are you running it on?