Kafka
Ubuntu
System Startup
Automation
Server Configuration

How to automatically start Kafka upon system startup in Ubuntu?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

On modern Ubuntu systems, the right way to start Kafka automatically at boot is to run it as a systemd service. That gives you proper startup ordering, restart behavior, logs in the journal, and a consistent way to manage the broker with systemctl. The exact configuration depends on whether your Kafka cluster uses KRaft mode or the older ZooKeeper mode, but the service-management pattern is the same.

Create a Dedicated Service User

Kafka should usually run as its own non-root user.

bash
sudo useradd --system --home /opt/kafka --shell /usr/sbin/nologin kafka
sudo chown -R kafka:kafka /opt/kafka

Adjust /opt/kafka to match your installation path.

Running Kafka as a dedicated user reduces risk and makes service ownership clearer.

Write a systemd Unit File

Create /etc/systemd/system/kafka.service:

ini
1[Unit]
2Description=Apache Kafka Server
3After=network-online.target
4Wants=network-online.target
5
6[Service]
7Type=simple
8User=kafka
9Group=kafka
10WorkingDirectory=/opt/kafka
11ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/kraft/server.properties
12ExecStop=/opt/kafka/bin/kafka-server-stop.sh
13Restart=on-failure
14RestartSec=5
15SuccessExitStatus=143
16LimitNOFILE=100000
17
18[Install]
19WantedBy=multi-user.target

This example assumes a KRaft configuration file at /opt/kafka/config/kraft/server.properties. If your installation uses a different path, change it accordingly.

Important fields:

  • 'User and Group run Kafka under the dedicated account'
  • 'WorkingDirectory keeps relative paths predictable'
  • 'Restart=on-failure restarts the broker after abnormal exit'
  • 'SuccessExitStatus=143 treats normal Java termination as clean'

If You Still Use ZooKeeper

Older Kafka deployments rely on ZooKeeper. In that case, Kafka should start after ZooKeeper and usually declare that dependency explicitly.

ini
1[Unit]
2Description=Apache Kafka Server
3Requires=zookeeper.service
4After=zookeeper.service network-online.target
5Wants=network-online.target

Everything else can stay similar, but the broker config file will be the ZooKeeper-era server.properties rather than a KRaft controller-broker config.

If you are on a current Kafka installation, KRaft is the direction to prefer.

Enable and Start the Service

After writing the unit file:

bash
sudo systemctl daemon-reload
sudo systemctl enable kafka
sudo systemctl start kafka

Check status:

bash
sudo systemctl status kafka

View logs:

bash
journalctl -u kafka -f

At this point the broker should start on boot and be manageable through standard systemd commands.

Test the Boot Path, Not Just the Manual Start

A service that starts manually is not always a service that survives reboot conditions correctly. After enabling it, reboot a test host or VM and verify:

  • Kafka starts automatically
  • storage directories are mounted before startup
  • network is ready
  • the configured listeners bind correctly

On real systems, boot-time bugs often come from dependency ordering rather than Kafka itself.

Practical Hardening

You can extend the unit file with environment variables if needed:

ini
Environment="KAFKA_HEAP_OPTS=-Xms1G -Xmx1G"
Environment="KAFKA_JVM_PERFORMANCE_OPTS=-XX:+UseG1GC"

These are often better placed in a separate environment file when operations teams need to tune them without editing the unit directly.

Also make sure log and data directories are writable by the Kafka user. A surprising number of startup failures are just permission mistakes.

Common Pitfalls

The most common problem is following old guides that assume ZooKeeper even when the installed Kafka version is using KRaft. Use the correct config file and dependency model for your cluster.

Another issue is putting the wrong path in ExecStart. If the service points at a missing script or the wrong properties file, systemd will fail instantly.

Some setups also rely on shell environment such as JAVA_HOME, which is not always present when systemd launches the process. If Java is not on the service path, set the environment explicitly.

Finally, do not run Kafka as root just because it is easier during setup. Fix ownership and permissions instead.

Summary

  • Use a systemd unit to start Kafka automatically on Ubuntu boot.
  • Run Kafka as a dedicated non-root user.
  • Point ExecStart at the correct Kafka script and config file for KRaft or ZooKeeper mode.
  • Enable the service with systemctl enable kafka and verify it after a reboot.
  • Check logs with journalctl and watch for permission, path, and dependency issues.

Course illustration
Course illustration

All Rights Reserved.