strace Basics: See Exactly What a Linux Program Really Does
You run a program and it fails with a useless message: "permission denied", "connection failed", or nothing at all. The logs are empty. Reinstalling doesn't help. This is the moment for strace — a tool that shows you every single thing a program asks the Linux kernel to do. Every file it opens, every network connection it tries, every permission check that fails. When a program lies to you, strace tells you the truth.
What a System Call Is
Programs don't talk to hardware directly. When a program wants to open a file, read from the network, or start another process, it asks the kernel through a system call — open, read, write, connect, execve, and a few hundred more. strace sits between the program and the kernel and logs every one of those calls, with its arguments and its result. It's like watching a program's thoughts in real time.
Your First Trace
strace is usually pre-installed. Try it on a harmless command:
strace ls /tmp
You'll see dozens of lines like this:
openat(AT_FDCWD, "/tmp", O_RDONLY) = 3
getdents64(3, [...], 32768) = 240
close(3) = 0
Each line is one system call: the name, its arguments, and the return value after =. openat opened /tmp and got file descriptor 3. getdents64 read the directory listing. close closed it. That's all ls does, underneath the friendly output.
The Three Uses That Matter
1. Find the missing file. A program fails with a vague error? Trace it and grep for failures:
strace -e trace=openat -f myprogram 2>&1 | grep ENOENT
ENOENT means "no such file or directory". You will often see the program looking for a config file in the wrong place — /etc/myapp/config when the real file is at /usr/local/etc/myapp/config. Now you know exactly what to fix.
2. Find the permission problem. EACCES means permission denied. Trace the program, find the EACCES line, and it tells you the exact file and the exact operation that was blocked. No more guessing which chmod to run.
3. See what a suspicious program does. For security work, strace is a quick way to see what an unknown binary is up to — in your lab, on a machine you own. Run it and watch for red flags:
strace -f -e trace=network,openat ./suspicious-program
Look for connections to strange IP addresses, files opened in /etc/shadow or your SSH keys, or writes to unexpected places. A program that claims to be a calculator but opens your browser history is telling you everything you need to know.
Useful Flags
-f— follow child processes. Without it, you only see the parent.-e trace=openat,connect,execve— trace only the calls you care about, and cut the noise.-o output.txt— save the trace to a file instead of flooding your screen.-c— summary mode: counts every system call and shows total time. Great for finding performance bottlenecks.-p <pid>— attach to a program that's already running.
A Word of Warning
strace slows the program down and shows you everything — including passwords and secrets passed as arguments. Never run it on a program handling real credentials in production, and never share a raw trace file publicly. In your own lab, though, it's one of the most honest tools Linux gives you.
Next time a program fails silently, don't reinstall it. Trace it. The answer is usually one line down, in a system call that returned an error the program never bothered to tell you about.



Comments
Post a Comment