Linux Internals

eBPF Explained: Safe Kernel-Level Observability for Linux Servers

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

When a Linux server gets slow, classic tools like top, vmstat and log files tell you that something is wrong, but rarely why. Which process is waiting on disk? Which TCP connections are retransmitting? How long does each open() call take? Answering these questions used to require kernel modules, recompiled kernels or heavy debuggers. eBPF changed that. It lets you run small, verified programs inside the running kernel and collect precise data with low overhead, without rebooting and without risking a crash. This article explains how eBPF works and how to start using it for observability on a VPS.

What eBPF actually is

BPF originally stood for Berkeley Packet Filter, a 1990s mechanism that let tools such as tcpdump filter packets inside the kernel. Extended BPF (eBPF), merged into Linux from version 3.18 onward and greatly expanded since, turned that filter into a general-purpose virtual machine inside the kernel. Today eBPF is used for tracing, networking, load balancing and security enforcement.

The key idea: you write a small program, the kernel checks it for safety, then attaches it to an event. Every time that event occurs, your program runs, records what it needs and returns.

How an eBPF program runs

  1. Write. Programs are written in restricted C, or in a high-level language such as bpftrace, and compiled to eBPF bytecode.
  2. Load. The bpf() system call loads the bytecode into the kernel. This needs root or specific capabilities such as CAP_BPF and CAP_PERFMON.
  3. Verify. The in-kernel verifier analyses every possible path. It rejects programs that could loop forever, read invalid memory or crash the kernel.
  4. Compile. A JIT compiler turns the bytecode into native machine code for speed.
  5. Attach. The program is attached to a hook: a kernel function, a tracepoint, a network interface, or a function inside a user-space binary.
  6. Share data. Results go into eBPF maps (key-value structures such as hash tables, arrays and histograms) or ring buffers, which user-space tools read and display.

Common hook types

HookWhat it observesExample
kprobe / kretprobeEntry and return of kernel functionsTime spent in vfs_read
TracepointStable, documented kernel eventssyscalls:sys_enter_openat
uprobeFunctions in user programs and librariesQueries inside a database binary
XDP / tcPackets at the network driver or traffic control layerFast filtering and counting
perf eventsCPU sampling at a fixed frequencyBuilding CPU flame graphs

Tracepoints are preferred when available because they stay stable across kernel versions, while kprobes depend on internal function names that may change.

Why eBPF suits observability

  • Low overhead. Data is filtered and aggregated inside the kernel, so only summaries such as counts and histograms cross into user space, not millions of raw events.
  • Safe. The verifier makes a buggy eBPF program far less dangerous than a buggy kernel module.
  • No restarts. You attach to a running system and detach when you are done.
  • Full stack. One tool can follow a request from a system call through the filesystem down to the block device.

Getting started on a VPS

Most current distributions ship kernels with eBPF and BTF (BPF Type Format) support. On Ubuntu or Debian:

uname -r                                  # kernel version
ls /sys/kernel/btf/vmlinux                # BTF present?
sudo apt install bpftrace bpfcc-tools

On Ubuntu and Debian the BCC tools are installed with a -bpfcc suffix, for example execsnoop-bpfcc. On AlmaLinux and Rocky, install bpftrace and bcc-tools; the tools then live in /usr/share/bcc/tools/.

Useful ready-made tools

sudo execsnoop-bpfcc        # every new process with its arguments (find cron or script storms)
sudo opensnoop-bpfcc        # files being opened, by which process
sudo biolatency-bpfcc       # histogram of block I/O latency
sudo tcpconnect-bpfcc       # outgoing TCP connections
sudo tcpretrans-bpfcc       # TCP retransmissions as they happen
sudo runqlat-bpfcc          # how long tasks wait for a CPU

runqlat is especially helpful on a VPS: long run-queue delays combined with high steal time in top point to CPU contention rather than a bug in your application.

bpftrace one-liners

bpftrace offers an awk-like language for quick questions:

# Count system calls by process name; press Ctrl+C to print results
sudo bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }'

# Show which processes open which files
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_openat { printf("%s %s\n", comm, str(args.filename)); }'

# Histogram of read() sizes returned by nginx
sudo bpftrace -e 'tracepoint:syscalls:sys_exit_read /comm == "nginx"/ { @bytes = hist(args.ret); }'

Older bpftrace releases write args->filename instead of args.filename; check bpftrace --version if a one-liner fails to parse.

Limits and good practice

  • Run tracing tools for short sessions on production. Overhead is low but not zero, and probing very hot functions such as every scheduler switch can be noticeable.
  • Some managed or container environments block the bpf() syscall. On a full KVM-based VPS with root access, you control your own kernel and can use eBPF normally.
  • The verifier limits program complexity, so heavy logic belongs in user space.
  • Prefer tracepoints over kprobes for scripts you plan to keep.

eBPF also powers well-known projects such as Cilium for Kubernetes networking, Falco for runtime security and Pixie for application monitoring. Learning the basic tools first makes those systems much easier to understand. To see how eBPF data connects to wider performance issues, read our article on server I/O, and if you need a sandbox with root access, check the VPS plans on our pricing page.

FAQ

Is eBPF safe to run on a production server?

Generally yes. The verifier blocks programs that could crash the kernel, and the standard BCC and bpftrace tools are widely used in production. The main risk is performance overhead when tracing extremely frequent events, so start with summarising tools and limit how long they run.

Do I need to recompile my kernel to use eBPF?

No. Distribution kernels from recent Ubuntu, Debian, AlmaLinux and Rocky releases include eBPF and BTF support. You only need to install the user-space tools such as bpftrace or bcc-tools and run them as root.

What is the difference between BCC and bpftrace?

BCC is a toolkit of ready-made tools plus Python and C libraries for building complex programs. bpftrace is a compact scripting language for quick one-liners and short scripts. Many engineers use BCC tools for routine checks and bpftrace when they need a custom question answered fast.

Related guides