Every file has an owner, a group, and a permission set controlling who can read, write, or execute it — the actual mechanism behind 'Permission denied' errors, and behind why chmod 777 (making a file fully open to everyone) is a red flag, not a quick fix.
3 min read
Running ls -l reveals a file's full permission and ownership information:
$ ls -l script.sh
-rwxr-xr-- 1 alice developers 1024 Sep 11 10:00 script.shBreaking down -rwxr-xr--: the leading character is the file type (- for a
regular file, d for a directory); the next nine characters are three groups
of three — permissions for the owner (alice), the group
(developers), and everyone else, in that order.
cd into) a
directory — for a directory specifically, x is what actually lets you
navigate into it at all, distinct from r (listing what's inside it)chmod sets permissions, either symbolically or numerically. The numeric form
represents each permission group as a single digit (read=4, write=2,
execute=1, summed):
$ chmod +x script.sh # symbolic: add execute permission for everyone
$ chmod 755 script.sh # numeric: owner=rwx(7), group=r-x(5), other=r-x(5)
$ chmod 644 config.txt # owner=rw-(6), group=r--(4), other=r--(4) — a typical config filechmod 777 grants full read, write, and execute permission to literally
everyone — a "fix" often reached for when something fails with "Permission
denied" and the actual cause hasn't been diagnosed. It works, in the narrow
sense that the error goes away — but it also means any user or process on the
system (and on a shared or compromised server, potentially an attacker) can
modify or execute that file freely. The better instinct: figure out the
actual required permission (often much narrower — the specific user or
group that legitimately needs access) rather than reaching for "everyone,
everything" as a shortcut.
chown changes who owns a file — relevant when a file was created by one
user (or process, like a web server running as its own dedicated user) but
needs to be owned by another:
$ chown alice script.sh # change the owner to alice
$ chown alice:developers script.sh # change owner AND group togethersudo ("superuser do") runs a single command with elevated (administrator)
permissions, rather than requiring you to be permanently logged in as an
all-powerful root user for your entire session:
$ sudo apt install curl # this specific command needs elevated permissionUsing sudo per-command rather than working as root all the time is a
deliberate safety practice — it means a mistake in an ordinary command doesn't
have unrestricted system-wide permission to cause damage, only the specific
command you explicitly elevated does.
Check your understanding
A quick comprehension check — not tracked, not graded, just for you.
1. In the permission string 'rwxr-xr--', what does the middle group ('r-x') represent?
2. Why is 'chmod 777' generally considered a red flag rather than a legitimate fix?
3. Why is running commands with `sudo` per-command generally safer than staying logged in as root the whole time?
Linux & the Command Line