Go’s deployment story is mostly the absence of a story — one statically-linked binary, no runtime to install, no venv to manage. The work is on the operating side: building reproducibly, dropping the binary somewhere systemd can find it, and writing a unit file that gives the process the sandboxing it should have had from day one. This article does that for a typical Go HTTP service on Ubuntu.
How to verify
Check the current Go install and any existing service:
go version 2>/dev/null || echo "go not installed"
ls -la /usr/local/go/bin/go 2>/dev/null
which go
systemctl status myapp --no-pager | head 2>/dev/null
file /usr/local/bin/myapp 2>/dev/null
file myapp on a Go binary should report “statically linked” (or “statically linked (with debug_info)”) — if it reports “dynamically linked”, CGO is enabled and the binary depends on system libraries that may differ between build and run hosts.
What’s happening
The Go runtime is bundled into the binary, so the only thing that has to be installed on the build machine is the Go toolchain itself. Ubuntu’s golang-go package lags real releases by months — for production builds use the official tarball or a tool like g or goenv.
A Go binary with CGO_ENABLED=0 and -tags=netgo,osusergo is fully static — no glibc dependency. That binary will run on any Linux of the same architecture, including distroless or scratch containers. With CGO enabled (default), Go links against libc, which means the build host’s glibc version must be older or equal to the run host’s, or you get version GLIBC_X.YY not found at startup.
systemd on the run side is what handles supervision, log capture (via journald), and sandboxing. The unit’s [Service] section is where you constrain the process — ProtectSystem, NoNewPrivileges, PrivateTmp, and friends turn an ordinary process into one that cannot trivially escalate even if compromised.
The procedure
-
Install Go from the official tarball. Skip
apt install golang-gounless your application explicitly targets the distro’s old version:GO_VERSION=1.23.4 curl -sLO https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz sudo rm -rf /usr/local/go sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz sudo ln -sf /usr/local/go/bin/go /usr/local/bin/go go version -
Build a static binary for the production host. Build on the same architecture, with module mode and a build cache local to the user:
cd /var/src/myapp CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ go build -trimpath -ldflags="-s -w -X main.version=$(git rev-parse --short HEAD)" \ -o /tmp/myapp ./cmd/server file /tmp/myapp-trimpathremoves local filesystem paths from the binary (reproducibility, fewer leaks).-s -wstrip the symbol table and DWARF (smaller binary, no stack traces with line numbers — keep them off if you ship to Sentry or similar). -
Drop the binary into
/usr/local/binand create a dedicated service user:sudo install -m 0755 /tmp/myapp /usr/local/bin/myapp sudo adduser --system --no-create-home --group myapp sudo mkdir -p /var/lib/myapp /var/log/myapp sudo chown myapp:myapp /var/lib/myapp /var/log/myapp -
Write a systemd unit with real sandboxing.
/etc/systemd/system/myapp.service:[Unit] Description=MyApp HTTP service After=network.target [Service] User=myapp Group=myapp ExecStart=/usr/local/bin/myapp -listen=127.0.0.1:8080 -data=/var/lib/myapp WorkingDirectory=/var/lib/myapp Restart=on-failure RestartSec=2s # sandboxing NoNewPrivileges=yes ProtectSystem=strict ProtectHome=yes PrivateTmp=yes PrivateDevices=yes ProtectKernelModules=yes ProtectKernelTunables=yes ProtectControlGroups=yes RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX RestrictNamespaces=yes LockPersonality=yes MemoryDenyWriteExecute=yes SystemCallArchitectures=native ReadWritePaths=/var/lib/myapp /var/log/myapp # resource limits LimitNOFILE=65535 MemoryMax=1G [Install] WantedBy=multi-user.target -
Enable, start, watch the logs:
sudo systemctl daemon-reload sudo systemctl enable --now myapp journalctl -u myapp -f
Operational notes
MemoryDenyWriteExecute=yesblocks any process that wants to allocate executable memory — that breaks JIT compilers but not Go (Go does not JIT). Useful constraint that catches the typical post-exploitation move.Restart=on-failureonly restarts on non-zero exit. If the binary exits 0 in an unexpected case (flag.Parseerror printing to stderr and returning), useRestart=alwayswith a lowRestartSecand watch for log noise.- Static binaries are sensitive to
nsswitch.conffor DNS resolution. With-tags=netgoGo uses its pure-Go resolver and reads/etc/resolv.confdirectly — no glibc DNS. That matters in chrooted or distroless environments. - For graceful shutdown, the binary should trap SIGTERM and drain in-flight requests. systemd sends SIGTERM, waits
TimeoutStopSec(default 90 s), then SIGKILLs — without trap handling you lose the in-flight requests. - If the service binds to ports below 1024, prefer
CAP_NET_BIND_SERVICEover running as root:AmbientCapabilities=CAP_NET_BIND_SERVICEin the unit. Saves you a reverse proxy if you really want the service on 443 directly.
For multi-platform builds (ARM64 for Graviton, etc.), see Cross-compiling Go binaries. The combination of static binary + sandboxed systemd unit is the default Stack Harbor uses for Go workloads under managed operations — minimal attack surface, deterministic deploys, and journald captures everything for free.