How Does Linux Find Your Commands? (PATH Will Blow Your Mind)

Linux PATH Explained: The Hacker Secret You Use EVERY Day (and Never Noticed)

Grab your coffee, because we're about to unlock something you've already used a thousand times without knowing it. Have you ever typed ls in a Linux terminal and wondered, "How does my computer know what that even means?" It's not magic. Your terminal (the shell) has to FIND that program somewhere on your system before it can run it. Behind the scenes, Linux uses environment variables, which are like little sticky notes the shell reads to know how to behave. One note holds your username. Another holds your home folder. And one of them is the boss of them all: PATH.

Here's the deal. PATH is a list of folders where Linux goes hunting for programs every time you hit Enter on a command. The folders are separated by colons (:), and the shell checks them in order, left to right, until it finds a match. Think of it like a video game: you press the button, and the game scans Level 1, then Level 2, then Level 3 until it finds the item you asked for. Without PATH, you'd have to type the full address of every program, like /bin/ls instead of just ls. Nobody has time for that. Let's actually see it with your own eyes. Open your terminal and type this:

echo $PATH

Boom! You'll see something like /usr/local/bin:/usr/bin:/bin:/usr/sbin. That's your search list. When you type ls, the shell checks /usr/local/bin first (nope), then /usr/bin (nope), then /bin (FOUND IT!) and runs /bin/ls. The $ sign tells echo to print the value inside the variable, not the word "PATH". Want to see all your environment variables? Run env, or filter it down with env | grep PATH. Now here's where it gets fun: what if YOU build a cool tool and want to run it from anywhere? You add your folder to PATH like this:

export PATH=$PATH:/home/arul/mytools

That one line keeps your old PATH and adds your own folder at the end. Check it with echo $PATH and you'll see your folder in the list. But watch out: this only lasts until you close the terminal. To make it permanent, add that same line to a hidden file like .bash_profile in your home folder (hidden files start with a dot, so use ls -a to spot them). Pro tip: put a colon between folders and no spaces, or things will break. And if your script isn't in PATH but sits in your current folder, you can still run it with ./myscript.sh, where the dot literally means "this folder right here."

Now for the part cybersecurity nerds love. PATH isn't just convenient, it's also an attack surface. Imagine a sneaky attacker drops a fake program named ls into a folder that sits at the FRONT of your PATH. Next time you type ls, the shell finds the fake one first and runs it. That trick is called PATH hijacking, and it's a classic in CTFs and privilege escalation challenges. This is exactly why you should never put writable or untrusted folders at the start of your PATH, and why Linux doesn't search your current directory by default. (Old MS-DOS did check the current folder first, which is the kind of thing that makes security people sweat.) Understanding PATH makes you a better defender AND a sharper hacker. Want to go deeper? Check out the official GNU Bash Manual and then go break (legally!) some CTF boxes. Now close this tab, open your terminal, and go mess with your own PATH. You've got this!

Comments

Popular posts from this blog

n8n – CVE-2025-68613: Critical RCE Vulnerability

Identicon

Unusual Traffic, Unexpected Chaos: The Truth Behind the Cloudflare Outage