Introduction
A hostel warden has a master key for every room. A student has a key only to their own room. If the student's key is stolen, one room is at risk; if the warden's is, the whole building is. That is the idea behind the principle of least privilege: every person, program and account gets only the access its job needs, and nothing more, so that any one stolen key opens as little as possible.
It sounds obvious, yet most real systems drift the other way: accounts collect permissions over the years, and nobody takes them back.
How it works
Least privilege applies to people and to software:
- People: a student can submit assignments but not change grades; a teacher can grade their own courses, not every course.
- Programs and service accounts: a backup job needs to read files, not delete them; a web app's database account needs to read and write its tables, not drop the database.
- Time: administrator rights can be granted for one task and removed afterwards (just-in-time access), instead of being kept forever.
Related ideas:
- Separation of duties: no single person can complete a risky action alone. The person who requests a payment is not the one who approves it.
- Role-based access control (RBAC): permissions are given to roles (student, teacher, admin) and people get roles, which makes reviews manageable.
- Regular access reviews: every few months, compare what each account has with what it uses, and remove the rest.
Least privilege limits the blast radius of every other failure. If an attacker phishes one password, injects SQL into one app, or takes over one service, they get only what that account could do.
Example
The program compares each account's granted permissions with what its job needs, and lists what to remove.
// Least privilege: compare what each account has with what its job needs, and list what to take away.
const accounts = [
{ name: "lab-printer", granted: ["print", "read-files", "admin"], needs: ["print"] },
{ name: "rahim (student)", granted: ["read-courses", "submit"], needs: ["read-courses", "submit"] },
{ name: "backup-job", granted: ["read-files", "write-files", "delete-files"], needs: ["read-files"] },
];
let removed = 0;
for (const a of accounts) {
const extra = a.granted.filter((p) => !a.needs.includes(p));
removed += extra.length;
console.log(a.name.padEnd(16) + (extra.length ? "remove: " + extra.join(", ") : "ok"));
}
console.log("permissions removed: " + removed);Line by line
- Each account has
granted(what it has) andneeds(what its job requires). filterkeeps every granted permission that is not inneeds: the excess.- The lab printer was given
read-filesand evenadmin; a printer needs only to print. A hacked printer with admin rights is a door into the whole network. - Rahim's student account has exactly what it needs.
- The backup job needs only to read;
write-filesanddelete-fileswould let ransomware that took it over destroy the very backups meant to save you.
Expected result
lab-printer remove: read-files, admin
rahim (student) ok
backup-job remove: write-files, delete-files
permissions removed: 4
In the real world
- Cloud providers let you write fine-grained policies ("this function may read this one storage bucket"); broad "allow everything" policies are a common cause of cloud breaches.
- Operating systems separate normal users from administrators; doing daily work as an administrator means any malware you run gets administrator rights too.
- Many organisations require a second person to approve production changes or large payments: separation of duties in practice.
- Deleting accounts of people who have left (offboarding) is one of the most often forgotten access reviews.
Common mistakes
- Giving admin rights "to make it work" and never taking them back.
- Sharing one powerful account among several people, so nobody knows who did what.
- Forgetting service accounts and devices, which often have more rights than any person.
- Never reviewing access, so permissions only ever grow.
Best practices
- Start every new account or role with no rights, and add only what is needed.
- Grant administrator access for a task and a time, not permanently.
- Review access regularly, and remove accounts as soon as someone leaves.
- Split risky actions between two people, and log who did what.
Summary
The principle of least privilege gives every person, program and account only the access its job needs, so a stolen password or a compromised service can reach as little as possible. Separation of duties, role-based access control, time-limited admin rights and regular access reviews put it into practice. It does not stop attacks by itself; it shrinks the damage of every one that gets through.
Quick check
Key takeaways
- Every account gets only what its job needs; start from nothing and add.
- Service accounts and devices need least privilege as much as people.
- Separation of duties stops one person, or one stolen account, from finishing a risky action alone.
- Review access regularly; permissions only grow unless someone removes them.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.