Skip to content

Running .NET 8 applications on Linux with systemd

Install the .NET runtime on Ubuntu, publish a self-contained binary, and supervise it as a sandboxed systemd service.

ASP.NET Core on Linux is unremarkable now — dotnet publish, drop the artifact on a Linux host, point a systemd unit at the binary, and Kestrel handles the request lifecycle. The interesting choices are around whether to publish framework-dependent or self-contained, how to put Nginx in front, and which systemd sandboxing flags to enable. This article walks a .NET 8 ASP.NET Core service onto Ubuntu 24.04.

How to verify

Check the .NET install and any existing service:

dotnet --info 2>/dev/null | head
systemctl status myapp --no-pager | head 2>/dev/null
ls /opt/myapp/ 2>/dev/null
sudo ss -lntp | grep dotnet
journalctl -u myapp -n 20 --no-pager 2>/dev/null

If dotnet --info reports SDK installed on a production host, that is probably overkill — runtime-only (aspnetcore-runtime-8.0) is enough to run published artifacts and reduces the install footprint.

What’s happening

A .NET application can be published in two shapes. Framework-dependent produces a small artifact that requires the .NET runtime to be installed on the host. Self-contained bundles the runtime into the artifact (~80-100 MiB extra) so the host only needs glibc and no .NET package at all. Self-contained is good for distroless deployment or strict version pinning; framework-dependent is good when many .NET apps share a host and the runtime upgrade should apply to all of them.

ASP.NET Core’s web server is Kestrel — a high-performance, async HTTP server that ships with the framework. Kestrel can listen directly on port 80/443 but the production pattern is Nginx in front for TLS, logging, and rate limiting. Kestrel listens on 127.0.0.1:5000 (or a UNIX socket) and Nginx proxies in.

systemd integration on Linux is straightforward — Type=notify works because the framework sends READY=1 to systemd when the host is up. That gives you proper boot ordering: After=postgresql.service actually waits for ASP.NET Core to be ready, not just for the process to be spawned.

The procedure

  1. Install the runtime. Microsoft’s package feed is the cleanest source:

    wget https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb
    sudo dpkg -i packages-microsoft-prod.deb
    rm packages-microsoft-prod.deb
    sudo apt update
    sudo apt install -y aspnetcore-runtime-8.0
    dotnet --info
  2. Publish the application on the build host. Framework-dependent for a host that has the runtime:

    dotnet publish ./src/MyApp -c Release \
      --runtime linux-x64 --no-self-contained \
      -o ./publish

    Self-contained — runtime bundled — for an air-gapped or distroless host:

    dotnet publish ./src/MyApp -c Release \
      --runtime linux-x64 --self-contained true \
      -p:PublishSingleFile=true -p:PublishTrimmed=true \
      -o ./publish

    PublishTrimmed=true strips unused assemblies — verify the app still starts; trimming can break reflection-heavy code.

  3. Copy the published output to the host. A dedicated service user owns it:

    sudo adduser --system --no-create-home --group myapp
    sudo mkdir -p /opt/myapp /var/log/myapp
    sudo rsync -a publish/ /opt/myapp/
    sudo chown -R myapp:myapp /opt/myapp /var/log/myapp
  4. systemd unit at /etc/systemd/system/myapp.service:

    [Unit]
    Description=MyApp ASP.NET Core
    After=network.target
    
    [Service]
    Type=notify
    User=myapp
    Group=myapp
    WorkingDirectory=/opt/myapp
    ExecStart=/usr/bin/dotnet /opt/myapp/MyApp.dll
    Environment=ASPNETCORE_URLS=http://127.0.0.1:5000
    Environment=ASPNETCORE_ENVIRONMENT=Production
    Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
    Restart=on-failure
    RestartSec=5s
    
    # sandboxing
    NoNewPrivileges=yes
    ProtectSystem=strict
    ProtectHome=yes
    PrivateTmp=yes
    ProtectKernelModules=yes
    ProtectControlGroups=yes
    ReadWritePaths=/var/log/myapp
    
    MemoryMax=1G
    
    [Install]
    WantedBy=multi-user.target

    For self-contained publishes, ExecStart=/opt/myapp/MyApp (the single-file binary) replaces the dotnet ...dll form.

  5. Enable, start, point Nginx at Kestrel:

    sudo systemctl daemon-reload
    sudo systemctl enable --now myapp
    journalctl -u myapp -f

    Nginx server block:

    server {
      listen 443 ssl http2;
      server_name app.example.com;
      # ssl_certificate ... ssl_certificate_key ...
    
      location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
      }
    }

    Configure services.Configure<ForwardedHeadersOptions> in Program.cs so the app trusts X-Forwarded-For from the local Nginx.

Operational notes

  • Type=notify requires the SDK package Microsoft.Extensions.Hosting.Systemd and a .UseSystemd() call in Program.cs. Without it, systemd never sees READY=1 and falls back to Type=simple semantics.
  • dotnet publish honors the <TieredCompilation>true</TieredCompilation> default — first requests JIT at a low tier, hot methods tier up. The first ~30 seconds after start are slower than steady state; warm the app before swapping traffic.
  • Trimmed self-contained builds save ~50 MiB but can break code using Type.GetType("MyApp.Plugin", ...) or Assembly.Load. Test trimming in a staging environment that mirrors production paths.
  • MemoryMax= is a cgroup hard limit. .NET 8’s GC reads the cgroup memory and sizes the heap accordingly — set it consciously to avoid OOM-kill mid-request.
  • For multi-architecture targets (ARM64 on AWS Graviton), set --runtime linux-arm64 in the publish command. .NET 8 has first-class ARM64 support; performance is competitive with x64 at a notably lower cost.

For TLS terminations and reverse-proxy details that Kestrel sits behind, see Nginx reverse-proxy setup. Stack Harbor’s managed operations practice runs .NET workloads on Linux as the default — the Windows-only era is long over, and the Linux operating story is simpler in every dimension.