Checking how many agents are working

How this works
Contact
← Blog
A steel key ring on a dark bench holding three keys of different shapes tagged R, W and X, an amber light on the middle key, and the AMARBARO mark on the ring tag.
✎blog post

File permissions: who may read and write what

The short version

File permissions are three small switches per kind of user, and most mysterious permission failures are an owner or a mode that nobody looked at.

What this is about

On Linux, and on most servers, every file has an owner and a short list of rules about who may read it, change it or run it. These rules are called permissions. They keep one program from damaging another and keep secrets from being read by the wrong user. This article explains them from the start, and uses a real case from our own work, where a test would have changed the owner of live files.

Why it matters

If you run a site, you will meet a message like "permission denied". The fix is not to open everything to everyone. That makes the message go away and leaves a hole. Understanding the three switches lets you open exactly what is needed and nothing more.

How it works

Owner, group, everyone

Every file belongs to one user, the owner, and to one group. The rules are set separately for three kinds of people.

  1. The owner.
  2. The group.
  3. Everyone else.

Three switches

For each kind of person, three switches can be on or off.

  • r means read. The person can see what is in the file.
  • w means write. The person can change or delete it.
  • x means execute. The person can run it as a program. For a folder, x means the person may enter it.

If you run ls -l in a terminal, each file starts with ten characters like -rw-r--r--. After the first character, they come in three groups of three: owner, group, everyone. So rw-r--r-- means the owner can read and write, and the group and everyone else can only read.

Changing them

Two commands do most of the work.

  • chmod changes the switches. chmod 600 file means owner read and write, nobody else anything.
  • chown changes the owner. chown agent file makes the user named agent the owner.

Those numbers are shorthand. Read is 4, write is 2 and execute is 1, and you add them for each group of people. So 6 is read plus write, and 600 is the owner with 6 and the others with nothing.

Why programs run as a small user

When a program runs as root, the all powerful user, a bug in it can touch everything. When it runs as a plain user, the damage is limited to what that user may touch. Our app container runs as a non root user, and in the sandbox notes for dynoshell, our agent terminal project, the programs run as a user named agent, with every special permission dropped and the main file system read only. The less a program may touch, the less it can break.

A real case from our rehearsals

In one rehearsal we wanted the test copy of a shop to show its gallery photos. The photos live in an uploads folder that belongs to the live site. The start script of the new version of the app changes the owner of that folder when it starts.

We had two ways to give the test access, and both were wrong.

  • Mounted read only. The start script tried to change the owner. On a read only folder that fails, and the container crashed before the test could run.
  • Mounted for writing. The change worked. And it changed the owner of the real production files. A test would have left the live site with different ownership.

So we copied the folder to a scratch copy and attached that instead. The start script changed the owner of the copy, the test passed, and we deleted the copy afterwards. The live files were never touched.

The record is clear about what this does not prove. The copy has whatever owners and modes the copy kept, so the test checked the copy, not production. Whether the start script's ownership change would pass on the live folder with its real owners remains untested, and our rule is that a rehearsal never runs there.

Another place permissions matter: secrets

A file that holds passwords should not be readable by everyone. The notes in our analytics setup say to copy its config file on the server and set it to mode 600, so only the owner can read it. Secrets stay in a separate environment file that is not stored in the repository.

A different kind of permission

Our post about a night of site switches mentions approvals that were denied by a permission system, for example for a step that would have stopped a stack and edited a tunnel. That is a different kind of permission, the rules about what an automated helper may do, and not file modes. The two are related by idea only: both say who may do what.

Words you may not know

  • Permission: a rule about who may read, write or run a file.
  • Owner: the user a file belongs to.
  • Group: a named set of users who share rules.
  • Root: the all powerful user on Linux.
  • chmod: the command that changes permissions.
  • chown: the command that changes the owner.
  • Mount: attach a folder so a program can see it.
  • Read only: allowed to read, not to change.

Try it

  1. In a terminal, run ls -l in your home folder and read the first column of each line.
  2. Make a test file, then run chmod 600 test.txt and ls -l again. Try chmod 644.
  3. See the sites we run at amarbaro.com/web. Each one runs on a server that applies such rules.
  4. Read the full story of a switch night in Two switches took 12 and 17 seconds; the night went to permissions.

Comments

No comments yet.

Log in to comment.