chmod
who may do whatchmod sets the mode of a file: read, write and execute, once each for the owner, the group and everyone else. It takes the mode as three octal digits, or as letters that change only the bits you name and leave the rest alone.
Each digit is a sum — read 4, write 2, execute 1 — so 640 is "owner reads and writes, group reads, others nothing".
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
6 is 4+2, read and write, for the owner. 4 is read for the group www-data. 0 is nothing for everyone else.
u is the owner, g the group, o others. u+x adds execute for the owner; go-rwx removes everything from group and others.
The owner kept read and write and gained execute. Group and others lost every permission they had.
Every file and directory under shared/ gains group write. Powerful, and rarely what you want for both files and directories alike.
Switches
A switch changes how the command behaves. These are the ones used day to day; man chmod lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
-R- Recursive: apply the change to a directory and everything beneath it.
-v- Verbose: print a line for every file processed.
-c- Changes: print a line only for the files that were actually changed.
-f- Quiet: suppress most error messages.
--reference=FILE- Copy the mode from another file instead of naming one.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
chmod +x script.shMakes a script executable for everyone who can read it.
chmod 600 ~/.ssh/id_ed25519The mode SSH requires on a private key — it refuses to use one that others can read.
chmod -R u=rwX,go=rX public/Capital X adds execute only to directories and to files that already had it — the right way to open up a tree.
stat -c "%a %n" filePrints the current mode in octal, for when the letters are hard to read.
In cybersecurity
Permission bits are the first and most-used access control on the system, and the common mistakes are all in the direction of too much.
It lets every user and every compromised service modify the file. It "fixes" a permission error by removing the protection.
A leading 4 or 2 makes a program run as its owner or group. On a root-owned binary that is a direct path to root; grant it almost never.
"chmod -R 755" marks every file executable; "chmod -R 644" makes every directory unenterable. Use X, or find with -type.