Linux File Permissions Explained: chmod, chown and Staying Secure


Run ls -l in any Linux terminal and you’ll see strings like -rwxr-xr-x next to every file. That gibberish is Linux’s security model — and misconfiguring it is one of the most common ways servers get compromised. Let’s decode it.

Why Permissions Matter

Linux was built as a multi-user system. Permissions decide who can read, write or execute every file. On a server, getting this wrong means a compromised web app can rewrite system files — game over.

Reading Permissions

Take -rwxr-xr-x:

  • First character: - means file, d means directory.
  • Next 9 characters split into three groups of three: owner, group, others.
  • Each group: r (read), w (write), x (execute). A - means that permission is denied.

So rwxr-xr-x = owner can do everything, group and others can read and execute but not modify.

The Number System

Each permission has a value: r=4, w=2, x=1. Add them per group:

  • 755 = rwxr-xr-x — standard for scripts and directories.
  • 644 = rw-r–r– — standard for regular files.
  • 600 = rw——- — private files like SSH keys. Only the owner can even read them.

Changing Permissions with chmod

chmod 755 script.sh # numeric: owner full, rest read+execute
chmod u+x script.sh # symbolic: add execute for the user/owner
chmod go-w file.txt # symbolic: remove write for group and others

Changing Ownership with chown

sudo chown anony:anony file.txt # change owner and group
sudo chgrp www-data file.txt # change group only

The One Mistake: chmod 777

777 gives everyone full read, write and execute access. Beginners use it to “fix” permission errors — and hand every user and process on the system the keys to that file. On a server, a 777 web directory is practically an invitation. Never use 777. Figure out the correct owner and use 755/644 instead.

Principle of Least Privilege

Give every file the minimum permissions it needs to work — nothing more. SSH keys at 600, config files at 640 or 600, web files at 644. Secure defaults beat clever fixes.

Comments

Popular Posts